I. IN. NET, an assembly is a basic unit for deployment, version control. The assembly type in the System.Reflection namespace, which represents an assembly. and contains information about the assembly.
Two. Get information about an assembly:
Create a console project and add a class library project named model with a user class.
static void Main (string[] args) { //1. Use LoadFrom, if the current project has a reference to the assembly Assembly asm = Assembly.LoadFrom (" Model.dll "); 2. Using Load, if the current project has a reference to the assembly Assembly asm1 = Assembly.Load ("Model"); 3. Load the assembly that does not belong to the current item, you need to give the full path Assembly asm2 = assembly.loadfrom (AppDomain.CurrentDomain.BaseDirectory + "\ \ Model.dll "); //4. After the active type instance, you can use the Assembly property of the type instance to get the assembly User U = new user (); Type t = U.gettype (); Assembly asm3 = t.assembly; }
F5 breakpoint Debugging, the Exporttypes attribute is found below a System.Type object for the user class.
To modify a class in model:
public class Department {public string dname {get; set;} Public Department () { dname = "Development department"; } public void Getshow () { Console.WriteLine ("I am the Getshow method in department"); } }
public class User {public string name {get; set;} Public User () { name = "Millet"; } public void Getshow () { Console.WriteLine ("I am the Getshow method in user"); } }
static void Main (string[] args) {//1. Use LoadFrom, if the current project has a reference to the assembly Assembly ASM = Assemb Ly. LoadFrom ("Model.dll"); 2. Using Load, if the current project has a reference to the assembly Assembly ASM1 = Assembly.Load ("Model"); 3. Load the assembly that does not belong to the current item, you need to give the full path Assembly asm2 = Assembly.LoadFrom (AppDomain.CurrentDomain.BaseDirectory + "\\MODEL.D ll "); 4. After the active type instance, you can use the Assembly property of the type instance to get the assembly user U = New user (); Type t = U.gettype (); Assembly asm3 = t.assembly; Console.WriteLine ("FullName (full name):" +ASM3. FullName); Console.WriteLine ("Location (path):" +ASM3. Location); module[] Modules = asm3. GetModules (); foreach (module model in modules) {Console.WriteLine ("Modules:" +model); Type[] types = model. GetTypes (); foreach (type type in types) {Console.WriteLine ("Type:" + type); } } } Gets the module information in the Assembly.
Three. Member information and Memverinfo type:
Modify the above code:
static void Main (string[] args) {//1. Use LoadFrom, if the current project has a reference to the assembly Assembly ASM = ASSEMBLY.LOADF Rom ("Model.dll"); 2. Using Load, if the current project has a reference to the assembly Assembly ASM1 = Assembly.Load ("Model"); 3. Load the assembly that does not belong to the current item, you need to give the full path Assembly asm2 = Assembly.LoadFrom (AppDomain.CurrentDomain.BaseDirectory + "\\MODEL.D ll "); 4. After the active type instance, you can use the Assembly property of the type instance to get the assembly user U = New user (); Type t = U.gettype (); Assembly asm3 = t.assembly; Console.WriteLine ("FullName (full name):" +ASM3. FullName); Console.WriteLine ("Location (path):" +ASM3. Location); module[] Modules = asm3. GetModules (); foreach (module model in modules) {Console.WriteLine ("Modules:" +model); Type[] types = model. GetTypes (); foreach (type type in types) {Console.WriteLine ("Type:" + type); Memberinfo[]Menberinfo = type. GetMembers (); foreach (MemberInfo mi in menberinfo) {Console.WriteLine ("Member:" +mi. ToString () + "type:" +mi. MemberType); } Console.WriteLine (); } } }
Four. Field information and FieldInfo type:
In department, add the public int level; Rank field; In user, add the public int ages; age field; Fix the above code:
static void Main (string[] args) {//1. Use LoadFrom, if the current project has a reference to the assembly Assembly ASM = Assemb Ly. LoadFrom ("Model.dll"); 2. Using Load, if the current project has a reference to the assembly Assembly ASM1 = Assembly.Load ("Model"); 3. Load the assembly that does not belong to the current item, you need to give the full path Assembly asm2 = Assembly.LoadFrom (AppDomain.CurrentDomain.BaseDirectory + "\\MODEL.D ll "); 4. After the active type instance, you can use the Assembly property of the type instance to get the assembly user U = New user (); Type t = U.gettype (); Assembly asm3 = t.assembly; Console.WriteLine ("FullName (full name):" +ASM3. FullName); Console.WriteLine ("Location (path):" +ASM3. Location); module[] Modules = asm3. GetModules (); foreach (module model in modules) {Console.WriteLine ("Modules:" +model); Type[] types = model. GetTypes (); foreach (type type in types) {Console.WriteLine ("Type:" + type); Memberinfo[] Menberinfo = type. GetMembers (); foreach (MemberInfo mi in menberinfo)//{//Console.WriteLine ("Member:" +mi. ToString () + "type:" +mi. MemberType); }//console.writeline (); fieldinfo[] fields = type. GetFields (); foreach (FieldInfo fi in fields) {Console.WriteLine ("Name:" +fi. name+ "type:" +fi. Fieldtype+ "feature:" +fi. Attributes); } Console.WriteLine (); } } }
V: Attribute information and PropertyInfo type:
Modify the above code:
fieldinfo[] fields = type. GetFields (); foreach (FieldInfo fi in fields) //{ // Console.WriteLine ("Name:" +fi. name+ "type:" +fi. Fieldtype+ "feature:" +fi. Attributes); } //console.writeline (); propertyinfo[] PiS = type. GetProperties (); foreach (PropertyInfo pi in PiS) { Console.WriteLine ("Name:" + pi.) Name + "type:" + pi. PropertyType + "Features:" + pi. attributes+ "readable:" +pi. Canread+ "Writable" +PI. CanWrite); }
Six. Method information and MethodInfo type:
To modify a polygon code:
static void Main (string[] args) {//1. Use LoadFrom, if the current project has a reference to the assembly Assembly ASM = ASSEMBLY.LOADF Rom ("Model.dll"); 2. Using Load, if the current project has a reference to the assembly Assembly ASM1 = Assembly.Load ("Model"); 3. Load the assembly that does not belong to the current item, you need to give the full path Assembly asm2 = Assembly.LoadFrom (AppDomain.CurrentDomain.BaseDirectory + "\\MODEL.D ll "); 4. After the active type instance, you can use the Assembly property of the type instance to get the assembly user U = New user (); Type t = U.gettype (); Assembly asm3 = t.assembly; Console.WriteLine ("FullName (full name):" +ASM3. FullName); Console.WriteLine ("Location (path):" +ASM3. Location); module[] Modules = asm3. GetModules (); foreach (module model in modules) {Console.WriteLine ("Modules:" +model); Type[] types = model. GetTypes (); foreach (type type in types) {Console.WriteLine ("Type:" + type); memberinfo[] Menberinfo = type. GetMembers (); foreach (MemberInfo mi in menberinfo)//{//Console.WriteLine ("Member:" +mi. ToString () + "type:" +mi. MemberType); }//console.writeline (); fieldinfo[] fields = type. GetFields (); foreach (FieldInfo fi in fields)//{//Console.WriteLine ("Name:" +fi. name+ "type:" +fi. Fieldtype+ "feature:" +fi. Attributes); }//console.writeline (); propertyinfo[] PiS = type. GetProperties (); foreach (PropertyInfo pi in PiS)//{//Console.WriteLine ("Name:" + pi.) Name + "type:" + pi. PropertyType + "Features:" + pi. attributes+ "readable:" +pi. Canread+ "Writable" +PI. CanWrite); } methodinfo[] Meis = type. GetMethods (); foreach (MethodInfo mei in Meis) {Console.WriteLine ("Name:" +mei. name+ "Signature:" +mei. ToString () + "return value type:" +mei. ReturnType); } } } }
There are also constructorinfo types and eventinfo types, which encapsulate the constructor and event information for the type. Viewing this information is similar to the above method.
"C # reflected assemblies"