. NET Reflection (reflection)-One

Source: Internet
Author: User

Reflection reflection needs to refer to using System.Reflection; namespace.

Through the Assembly class of Load (); Load the specified assembly

Assembly is not instantiated, so it can load () by calling the static method, loading the specified assembly.

By the type of Getcond in GetType () Refliuyl

?
1 2 3 Assembly assembly = Assembly.Load("RefLiuyl");           //获取类型,参数(名称空间+类)   Type type = assembly.GetType("RefLiuyl.GetCon");

There is also a getname<t> () when using generics. Gets the type of the TypeOf directly (T);

Let's take a look at the partial internal load prototype static method:

    // Assembly Internal Load prototype-part     Public class Assembly    {        //load static reload prototype         Public Static Assembly Load (AssemblyName assemblyref);          Public Static Assembly Load (string  assemblystring);    }

The AssemblyName here is actually fetching the assembly.
New AssemblyName ("classlibrary"= Assembly.Load (classlibrary);

Assembly can also load an assembly through a path

Assembly Assem = Assembly.LoadFrom ("path");

    // Assembly Internal Load prototype-part     Public class Assembly    {        //         Public Static Assembly LoadFrom (string  path);    

Two. Instance of a constructed type

?
1 2 3 Assembly assembly = Assembly.Load("RefLiuyl");           //获取类型,参数(名称空间+类)   Type type = assembly.GetType("RefLiuyl.GetCon");<br>获取到类型后,我们需要实例化它。<br><br>通过System.Activator的CreateInstance方法来实例化它。
AssemblyName classlibrary =NewAssemblyName ("classlibrary"); //Assembly Assembly = Assembly.Load (classlibrary);Assembly Assembly = Assembly.Load ("classlibrary"); //get type by GetType ()//get type, parameter (Namespace + Class)Type type = assembly. GetType ("classlibrary.student"); //create an instance of the object, object type, parameter (Namespace + Class)            ObjectInstance = assembly. CreateInstance ("classlibrary.student"); //or use the type directly            ObjectInstance1 =activator.createinstance (type); //when using genericsType type =typeof(T); T Destobj= Activator.createinstance<t> ();
Activator.CreateInstance (); Returned is the class instance


GetProperties (); Returns all properties in the assembly type
            // get Type            by GetType () // get type, parameter (Namespace + Class)               Type type = assembly. GetType ("classlibrary.student");             foreach inch type. GetProperties ())            {                string namses= Prop. Name;            }

The property fields of the instance class in the assembly are returned.



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.