Assembly类
Assembly类在System.Reflection命名空间中定义,它允许访问给定程序集的元数据,它也包含了可以加载和执行程序集。
如何加载程序集?
//1.根据程序集的名字加载程序集,它会在本地目录和全局程序集缓存目录查找符合名字的程序集。
Assembly assembly1 = Assembly.Load("SomeAssembly");
//2.这里的参数是程序集的完整路径名,它不会在其他位置搜索。
Assembly assembly2 = Assembly.LoadFrom(@"c:\xx\xx\xx\SomeAssembly.dll")
Assembly对象的使用
//1,获取程序集的全名
string name = assembly1.FullName;
//2,遍历程序集中定义的类型
Type[] types = theAssembly.GetTypes();
foreach(Type definedType in types){
}
//3,遍历程序集中定义的所有特性(稍后介绍)
Attribute[] definedAttributes = Attribute.GetCustomAttributes(someAssembly);
继续Type类中的示例
Assembly assembly = Assembly.Load ("TypeTest, Version=1.0.6495.19563, Culture=neutral, PublicKeyToken=null");
Type[] types = assembly.GetTypes ();
foreach (Type type in types) {
foreach (FieldInfo fieldInfo in type.GetFields()) {
Console.WriteLine (fieldInfo);
}
foreach (PropertyInfo propertyInfo in type.GetProperties()) {
Console.WriteLine (propertyInfo);
}
foreach (MethodInfo methodInfo in type.GetMethods()) {
Console.WriteLine (methodInfo);
}
Console.WriteLine ("----------------------------");
}
输出:
Void Main(System.String[])
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
System.String ToString()
----------------------------
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
System.String ToString()
----------------------------
System.String Name
System.String Color
System.String get_Name()
Void set_Name(System.String)
System.String get_Color()
Void set_Color(System.String)
Void run()
Void jump()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
System.String ToString()
----------------------------