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. assemblies
using System;
using System.;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.

转载于:https://www.cnblogs.com/microci/archive/2008/04/07/1141131.html

Reflection,Regular Expression,Threading,IO,AppDomain,Web Service/Remoting Service,ORM相关推荐

  1. 【论文阅读】ReDoSHunter: A Combined Static and Dynamic Approach for Regular Expression DoS Detection

    TODO 引文信息 [1] LI Y, CHEN Z, CAO J, 等. ReDoSHunter: A Combined Static and Dynamic Approach for Regula ...

  2. 使用jMeter的regular expression extract提取SSO form的XSRF protection token

    在基于SAML的Authentication流程里,IDP返回给客户端的html form里包含了很多用于认证的信息,比如XSRF Token. 一个例子可以如下图所示: 我现在想把这个字段提取在jM ...

  3. Regular Expression Matching

    正则匹配 Regular Expression Matching Implement regular expression matching with support for '.' and '*'. ...

  4. java正则表达式及api_JAVA常用API:正则表达式regular expression

    一.正则表达式的概念 正则表达式,regular expression,在代码中通常简写成regex 正则表达式是一个字符串,使用每单个字符串来描述.定义匹配规则,匹配一系列符合某个语法规则的字符串. ...

  5. leetcode 10 Regular Expression Matching

    题目连接 https://leetcode.com/problems/regular-expression-matching/ Regular Expression Matching Descript ...

  6. 【Nginx】错误: [emerg] “proxy_pass“ cannot have URI part in location given by regular expression,...

    前言 nginx 1.15.11(下面的代码在此版本测试通过) win10 phpstudy 8.1.1.3 错误 nginx: [emerg] "proxy_pass" cann ...

  7. Search Engine —— Regular Expression(Spider)

    Regular Expression,即正则表达式:用来查找符合某些负责规则的字符串的需要.它真是用于描述这些规则的工具. 1. \b 是一个元字符,用来匹配一个位置,代表着单词的开头或结尾,也就是单 ...

  8. 初步了解并使用正则表达式 (REGular EXPression)

    正则表达式(REGular EXPression),正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串.在很多文本编辑器里,正则表达式通常被用来检索.替换那些符合某个模式的文本.只有掌 ...

  9. [正则表达式] 可以解析HTML/XHTML页面的所有元素和结构的Regular Expression![ZT]

    ZT: http://www.cnblogs.com/Laser_Lu/archive/2005/04/21/142605.html 哈哈,继 昨天的那个正则表达式 之后又写了一个更长的Regular ...

最新文章

  1. ubuntu 14.04 16.04 18.04使用阿里源
  2. Hibernate映射配置方法
  3. Java 多条件复杂排序小结
  4. GCD介绍(二): 多核心的性能
  5. Go程序设计语言 练习题
  6. 我是如何完美解决WIN10崩溃无法自动恢复启动问题的
  7. springboot基于java的校园二手书籍交易平台毕业设计源码131558
  8. 【mysql】批量更新数据
  9. 夜读 | 读书和不读书的人生,差别有多大
  10. 提取LSV中的高程数据在CAD中进行道路的方案设计流程
  11. Conflux CTO 伍鸣博士出席2019北京国际金融安全论坛
  12. 计算机基本原理问答题及答案
  13. html5 无插件视频播放器,多功能流媒体播放器网页无插件直播EasyPlayer.js如何实现播放完自动循环播放...
  14. hadoop错误org.apache.hadoop.yarn.exceptions.YarnException Unauthorized request to start container
  15. 磁盘分区后取得整数大小
  16. seo原创文章五种方法迎合搜索引擎收录和排名
  17. UC研发团队——遇见你,不离不弃(10月18日更新版)
  18. 十进制转二进制C语言版
  19. 画图实现考试成绩管理系统
  20. realme怎么互传_魅族、一加、realme、黑鲨同时宣布加入互传联盟

热门文章

  1. Windows GPT磁盘GUID结构详解
  2. LVS DR模型配置示例
  3. 自定义类模板 重载遇到的问题
  4. 基于 Android NDK 的学习之旅-----数据传输二(引用数据类型)(附源码)
  5. php数字加零,php实现数字补零的两种方法
  6. Hibernate映射详解(二)--多对一,一对多单向,一对多双向关联映射
  7. Android ViewFlipper滑动屏幕切换
  8. Mac下使用OpenCV
  9. Hadoop1.2.1伪分布模式安装指南
  10. 系统程序员成长计划-组合的威力