C#反射类型的使用
using System;
using System.Security.Permissions;
using System.Text;
using System.Reflection;
// Download by http://www.codefans.net
namespace UseReflection
{
/// <summary>
/// 这里的特性要求程序的运行需要完全信任的安全级别
/// </summary>
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
partial class UseReflection
{
static void Main(string[] args)
{
//需要根据代码位置进行更新
//对程序集进行遍历分析
Assembly assembly = Assembly.LoadFrom(@"..\..\..\..\6-8 简单程序集\SimpleAssembly\bin\Debug\SimpleAssembly.exe");
AnalyzeAssembly(assembly);
//创建一个程序集中的类型的对象
//这里尝试创建SimpleAssembly对象
Console.WriteLine("利用反射创建类型");
String[] pars={"测试反射"};
Object o = assembly.CreateInstance(assembly.GetModules()[0].GetTypes()[0].ToString(),
true, BindingFlags.CreateInstance, null, pars, null, null);
Console.WriteLine(o);
Console.Read();
}
}
partial class UseReflection
{
/// <summary>
/// 对一个程序集进行分析
/// </summary>
/// <param name="assembly"></param>
private static void AnalyzeAssembly(Assembly assembly)
{
Console.WriteLine("程序集名字:"+assembly.FullName);
Console.WriteLine("程序集位置:" + assembly.Location);
Console.WriteLine("程序集是否在GAC中:" +
assembly.GlobalAssemblyCache.ToString());
Console.WriteLine("包含程序集的模块名" +
assembly.ManifestModule.Name);
Console.WriteLine("运行程序集需要的CLR版本:"+
assembly.ImageRuntimeVersion);
Console.WriteLine("现在开始分析程序集中的模块");
Module[] modules = assembly.GetModules();
foreach (Module module in modules)
AnalyzeModule(module);
////递归分析引用程序集
////这里的代码供读者参考
//Console.WriteLine("开始分析引用的程序集");
//AssemblyName[] refassemblies = assembly.GetReferencedAssemblies();
//foreach (AssemblyName refname in refassemblies)
//{
// Assembly refassembly = Assembly.Load(refname);
// AnalyzeAssembly(refassembly);
//}
}
}
partial class UseReflection
{
/// <summary>
/// 对一个模块进行分析
/// </summary>
/// <param name="module">模块</param>
private static void AnalyzeModule(Module module)
{
Console.WriteLine("模块名:" + module.Name);
Console.WriteLine("模块的UUID:" + module.ModuleVersionId);
Console.WriteLine("开始分析模块下的类型");
Type[] types = module.GetTypes();
foreach (Type type in types)
AnalyzeType(type);
}
}
partial class UseReflection
{
/// <summary>
/// 对一个类型进行分析
/// </summary>
/// <param name="type">类型</param>
private static void AnalyzeType(Type type)
{
Console.WriteLine("类型名字:" + type.Name);
Console.WriteLine("类型的类别是:"+type.Attributes);
if (type.BaseType != null)
Console.WriteLine("类型的基类是:" + type.BaseType.Name);
Console.WriteLine("类型的GUID是:" + type.GUID);
//设置感兴趣的类型成员
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance);
//分析成员
FieldInfo[] fields = type.GetFields(flags);
if (fields.Length > 0)
{
Console.WriteLine("开始分析类型的成员");
foreach (FieldInfo field in fields)
AnalyzeField(field);
}
//分析包含的方法
MethodInfo[] methods = type.GetMethods(flags);
if (methods.Length > 0)
{
Console.WriteLine("开始分析类型的方法");
foreach (MethodInfo method in methods)
AnalyzeMethod(method);
}
//分析属性
PropertyInfo[] properties = type.GetProperties(flags);
if (properties.Length > 0)
{
Console.WriteLine("开始分析类型的属性");
foreach (PropertyInfo property in properties)
AnalyzeProperty(property);
}
}
}
partial class UseReflection
{
/// <summary>
/// 对一个成员进行分析,这里只做简单的分析
/// 可以进一步分析成员的可访问级别,
/// 或通过成员得到包含它的类型、模块等
/// </summary>
/// <param name="field">成员</param>
private static void AnalyzeField(FieldInfo field)
{
Console.WriteLine("成员名字:" + field.Name);
Console.WriteLine("成员的类别:" + field.Attributes);
Console.WriteLine("成员的类型名:" + field.FieldType.Name);
}
/// <summary>
/// 对一个方法进行分析
/// </summary>
/// <param name="method"></param>
private static void AnalyzeMethod(MethodInfo method)
{
Console.WriteLine("方法名字:" + method.Name);
Console.WriteLine("方法的类别:" + method.Attributes);
Console.WriteLine("开始分析方法的参数");
ParameterInfo[] parameters = method.GetParameters();
if (parameters.Length <= 0)
Console.WriteLine("方法没有参数");
foreach (ParameterInfo parameter in parameters)
AnalyzeParameter(parameter);
ParameterInfo retpar = method.ReturnParameter;
Console.WriteLine("分析方法的返回参数");
AnalyzeParameter(retpar);
}
/// <summary>
/// 分析方法参数
/// </summary>
/// <param name="parameter"></param>
private static void AnalyzeParameter(ParameterInfo parameter)
{
Console.WriteLine("参数名字:" + parameter.Name);
Console.WriteLine("参数的类别:" + parameter.Attributes);
Console.WriteLine("参数的类型:" + parameter.ParameterType.Name);
}
/// <summary>
/// 分析属性
/// </summary>
/// <param name="property"></param>
private static void AnalyzeProperty(PropertyInfo property)
{
Console.WriteLine("属性名字:" + property.Name);
Console.WriteLine("属性的类别:" + property.Attributes);
Console.WriteLine("是否可读:" + property.CanRead.ToString());
Console.WriteLine("是否可写:" + property.CanWrite.ToString());
}
}
}