First, load the assembly to be loaded, find the specified type, and then perform dynamic loading.
Content in the Assembly to be loaded:
Console. WriteLine (Console. WriteLine (Name {; Age {; Console. WriteLine (Hoppy {; Console. WriteLine (}Content in the Assembly
Load assembly:
Assembly asm = Assembly.LoadFile();
Obtain all types under the Assembly (including private and internal)
Type[] types = ( item Console.WriteLine( + item.Name + + item.Namespace + + }
Obtain all the "public" types under the Assembly
Type[] ExportedTypes= asm.GetExportedTypes();
Contains a Person class for testing:
Name { ; Age { ; }
Three methods for obtaining Type
Person person = Type type1 = Type type2 = Assembly asm = Assembly.LoadFile( Type type3 = asm.GetType();
Public"Attribute:
Type asmType = asm.GetType(, , );= ( item + item.Name + + item.PropertyType.Name + + item.CanRead + +
Public"Method:
MethodInfo[] asmMenthods = ( asmMethod Console.WriteLine(asmMethod.Name + + asmMethod.DeclaringType.Name + }
The above dynamic results are all "PublicTo obtain private information, you must set it.
PrivateMethod:
Type typePerson= MethodInfo perMethod= typePerson.GetMethod(, BindingFlags.NonPublic | obj = perMethod.Invoke(obj, ); MethodInfo[] perMothods = typePerson.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance);
Dynamic acquisition of other content will not be demonstrated, basically the same.
classTarget = Activator.CreateInstance(asmType);
pror = asmType.GetProperty( pror.SetValue(classTarget, ,); Console.WriteLine(pror.GetValue(classTarget,));
MethodInfo method = asmType.GetMethod( method.Invoke(classTarget, );
Obtain the following types
Assembly asm = Assembly.LoadFile( Type typePerson = asm.GetType( Type typeChinese = asm.GetType( Type typeIXiufuable = asm.GetType( Type typeAbsClass = asm.GetType();
1
Determines whether the type in the method can be assigned to the current type. (Note: Both interfaces and parent classes can be used.)
b1 = typePerson.IsAssignableFrom(typeChinese); b2 = typeIXiufuable.IsAssignableFrom(typeChinese);
Checks whether the Instance Object in brackets is of the current type. ()
objChinese = b1 = typeChinese.IsInstanceOfType(objChinese); b2 = typePerson.IsInstanceOfType(objChinese); b3 = typeIXiufuable.IsInstanceOfType(objChinese);
Determines whether the current type is a subclass of the type in parentheses. ()
b1 = typeChinese.IsSubclassOf(typePerson); b2 = typeChinese.IsSubclassOf(typeIXiufuable);
4 IsAbstract attributes
Determines whether the current type is abstract. (Note: Abstract: As long as it cannot be instantiated, such as static, sealed, and interface .))
Console.WriteLine(typeChinese.IsAbstract); Console.WriteLine(typePerson.IsAbstract); Console.WriteLine(typeIXiufuable.IsAbstract); Console.WriteLine(typeAbsClass.IsAbstract);