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

推荐订阅源

美团技术团队
T
The Blog of Author Tim Ferriss
C
Check Point Blog
博客园_首页
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
L
LangChain Blog
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
Vercel News
Vercel News
博客园 - Franky
V
V2EX
IT之家
IT之家
U
Unit 42
N
Netflix TechBlog - Medium
腾讯CDC
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
博客园 - 叶小钗
H
Help Net Security
V
Visual Studio Blog
GbyAI
GbyAI

博客园 - 酷咖啡

收藏:精妙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.