C # reflection Summary

Source: Internet
Author: User

 

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);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.