//1.获取当前程序域中所有的Assembly Assembly[] assArr = AppDomain.CurrentDomain.GetAssemblies(); //2.获取当前 对象 所有在 Assembly Assembly ass = this.GetType().Assembly; //3.根据路径加载Assembly string assPath = @"..modelClassLibrary.dll"; //如果这个*.dll 引用了别一个dll Assembly fromAssembly = Assembly.LoadFrom(assPath);//要加载引用的dll Assembly fileAssembly = Assembly.LoadFile(assPath);//不加载引用的dll Assembly LoadAssembly = Assembly.Load(typeof(ClassLibrary.Libray).Assembly.GetReferencedAssemblies().FirstOrDefault());
//1:通过类 获得对应的 Type Type tClass = typeof(MyClass); //2.通过对象 获得对应的Type Type tNew = new MyClass(1,2).GetType(); Type tClassFullName = Assembly.LoadFrom("").GetType("BLL.MyClass"); //4.获取程序集中定义的所有的 public 类 Type[] types = Assembly.LoadFrom("").GetExportedTypes(); //5.获取程序集中定义的所有的类 Type[] types = Assembly.LoadFrom("").GetTypes(); }
public void TypeProperty() { Type type = typeof(MyClass); //获取type所在的程序集对象 Assembly objAssembly = type.Assembly; //获取type对象对应的类的全名称 string typeClassFullName = type.FullName; //获取type对象对应类的名称 string typeClassName= type.Name; //判断type是否为一个数组类 bool boolArr = type.IsArray; //判断type是否为一个枚举类 bool boolEnum = type.IsEnum;
}
//判断type是否实现了接口 IMyInterface type.IsAssignableFrom(typeof(IMyInterface)); //判断type 是否继承了 fatherClass type.IsSubclassOf(typeof(fatherClass)); //判断 obj 是否为type类的实例 var obj = "";//object obj type.IsInstanceOfType(obj); //获取type中名为gender的字段对象 type.GetField("Gender"); //获取type中名为SayHi的方法对象 type.GetMethod("SayHi"); //获取type中名为Age的属性 type.GetProperty("Age");
class MyClass { int i = 10, j = 100; string Name = "valeb", FullName = "Pu_valeb"; } class fatherClass { public void FieldInfoOperation() { MyClass myClass = new MyClass(); Type type = myClass.GetType();
FieldInfo iField= type.GetField("i");//获取i字段对象 int iValue =(int)iField.GetValue(type);//获取 i 字段在type对象中的值 iField.SetValue(type, 1000);//设置 i字段在type对象中的值 } }
class MyClass { int i = 10, j = 100; string Name = "valeb", FullName = "Pu_valeb"; public int IntProperty { get; set; } public string StringProperty { get; set; } } class fatherClass { public void PropertyInfoOperation() { MyClass myClass = new MyClass(); Type type = myClass.GetType(); PropertyInfo intProperty = type.GetProperty("IntProperty");//获取intProperty属性对象 int iValue = (int)intProperty.GetValue(type);//获取 intProperty 属性在type对象中的值 intProperty.SetValue(type, 1000);//设置 intProperty属性在type对象中的值 } }