惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Blog — PlanetScale
Blog — PlanetScale
博客园 - Franky
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
D
Docker
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
H
Help Net Security
B
Blog
宝玉的分享
宝玉的分享

博客园 - 酷咖啡

收藏:精妙SQL语句 用户中心 - 博客园 博文阅读密码验证 - 博客园 web2.0配色收集 也谈C#中的反射用法 C#访问修饰符 C#中相等的判断 [LoveCherry]一步一步学Linq to sql系列文章 [LoveCherry]无废话C#设计模式系列文章 .Net常用资源收集 Some word in English about Company website 收藏:笔记本得理器型号规格 如何让虚拟目录里面的webconfig不继承网站[转] 刚刚用上了百度Hi 自定义可绑定数据的业务对象实体和强类型 Asp.net 2.0 网站首页生成静态的方法 数据库设计的5种常见关系 [收集]visual studio文件扩展名 说说最近的工作
Reflection,Regular Expression,Threading,IO,AppDomain,Web ...
酷咖啡 · 2008-04-07 · via 博客园 - 酷咖啡


Reflection,Regular Expression,Threading,IO,AppDomain,Web Service/Remoting Service,ORM
先开个头,慢慢完善!

Reflection allows you to programmatically inspect and get information about an assembly, including all object types contained within it. This information includes the attributes you have added to those types. The reflection objects reside within the System.Reflection namespace.

In addition to reading the types defined within a given assembly, you can also generate (emit) your own assemblies and types using the services of System.Reflection.Emit or System.CodeDom. This topic is a little too hectic for a beginning book on C#, but if you are interested, then MSDN contains some information on emitting dynamic assemblies.

The first example in this section inspects an assembly and displays a list of all attributes defined on the assembly — this should produce a list similar to that shown earlier.

Note

In this chapter, I'm going to be a bit more relaxed about the format of the code examples, since if you've gotten to this point in the book you must be pretty confident of what you're doing! All of the code can be found in the Chapter27 folder of the code download — some examples in this chapter might show you only the most important parts of the code, so don't forget to look through the downloaded code to see the whole picture.

This first example can be found in the Chapter27\FindAttributes directory. The entire source file is reproduced here:

// Import types from the System and System.Reflection assemblies
using System;
using System.Reflection;

namespace FindAttributes
{
class Program
{
/// <summary>
/// Main .exe entry point
/// </summary>
/// <param name="args">Command line args - the name of an assembly</param>
static void Main(string[] args)
{
// Output usage information if necessary.
if (args.Length == 0)
Usage();
else if ((args.Length == 1) && (args[0] == "/?"))
Usage();
else
{
// Load the assembly.
string assemblyName = null;

// Loop through the arguments passed to the console application.
// I'm doing this as if you 
// spaces you end up with several arguments - 
// them back together again to make one filename...
foreach (string arg in args)
{
if (assemblyName == null)
assemblyName = arg;
else
assemblyName = string.Format("{0} {1}", assemblyName, arg);
}

try
{
// Attempt to load the named assembly.
Assembly a = Assembly.LoadFrom(assemblyName);

// Now find the attributes on the assembly.
// The parameter is ignored, so I chose true.
object[] attributes = a.GetCustomAttributes(true);

// If there were any attributes defined...
if (attributes.Length > 0)
{
Console.WriteLine("Assembly attributes for 
assemblyName);

// Dump them out...
foreach (object o in attributes)
Console.WriteLine("  {0}", o.ToString());
}
else
Console.WriteLine("Assembly {0} contains no Attributes.", 
assemblyName);
}
catch (Exception ex)
{
Console.WriteLine("Exception thrown loading assembly {0}...", 
assemblyName);
Console.WriteLine();
Console.WriteLine(ex.ToString());
}
}
}

/// <summary>
/// Display usage information for the .exe.
/// </summary>
static void Usage()
{
Console.WriteLine("Usage:");
Console.WriteLine("  FindAttributes <Assembly>");
}
}
}

Now, build the executable in Visual Studio 2005, or if you prefer use the command-line compiler:

>csc FindAttributes.cs

This will compile the file and produce a console executable, which you can then call.

To run the FindAttributes application, you need to supply the name of an assembly to inspect. For now, you can use the FindAttributes.exe assembly itself, which is shown in Figure 27-3.


Figure 27-3

The example code first checks the parameters passed to the command line — if none are supplied, or if the user types FindAttributes /? then the Usage() method will be called, which will display a simple command usage summary:

if (args.Length == 0)
Usage ();
else if ((args.Length == 1) && (args[0] ==  "/?"))
Usage ();

Next, reconstitute the command-line arguments into a single string. The reason for this is that it's common to have spaces in directory names, such as Program Files, and the space would cause it to be considered as two arguments. So, iterate through all the arguments, stitching them back into a single string, and use this as the name of the assembly to load:

foreach (string arg in args)
{
if (assemblyName == null)
assemblyName = arg;
else
assemblyName = string.Format ("{0} {1}" , assemblyName , arg);
}

Then attempt to load the assembly and retrieve all custom attributes defined on that assembly with the GetCustomAttributes() method:

Assembly a = Assembly.LoadFrom (assemblyName);
// Now find the attributes on the assembly.
object[] attributes = a.GetCustomAttributes(true);

Any attributes found are output to the console. When you tested the program against the FindAttributes.exe file, an attribute called DebuggableAttribute was displayed. Although you have not specified the DebuggableAttribute, it has been added by the C# compiler, and you will find that most of your executables have this attribute.