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

推荐订阅源

S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
Spread Privacy
Spread Privacy
Scott Helme
Scott Helme
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
量子位
Security Latest
Security Latest
P
Proofpoint News Feed
P
Privacy International News Feed
P
Palo Alto Networks Blog
D
DataBreaches.Net
大猫的无限游戏
大猫的无限游戏
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google Online Security Blog
Google Online Security Blog
Webroot Blog
Webroot Blog
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
Vercel News
Vercel News
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Hugging Face - Blog
Hugging Face - Blog
月光博客
月光博客
Hacker News - Newest:
Hacker News - Newest: "LLM"
K
Kaspersky official blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Stack Overflow Blog
Stack Overflow Blog
AWS News Blog
AWS News Blog
博客园 - Franky
爱范儿
爱范儿
T
Tor Project blog
The GitHub Blog
The GitHub Blog
宝玉的分享
宝玉的分享
小众软件
小众软件
L
LINUX DO - 最新话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
W
WeLiveSecurity
SecWiki News
SecWiki News
L
LangChain Blog
I
InfoQ

博客园 - 酷咖啡

收藏:精妙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 Service/Remoting Service,ORM
酷咖啡 · 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.