C # reflection technology

Source: Internet
Author: User
Introduction to reflection

Reflection: reflection is a common term that describes the function of checking and processing program elements during running. That is, the program can be controlled while the program is running, not during compilation. You can start to learn reflection and learn the Dong class.

Type

This class can be used to access information about any data type. The type class only refers to the reference of the storage type and is the entry to many reflection functions.

The method of instantiating an object obtains the data type.
<span style="font-size:18px;">           Type t1=typeof(int);            Type t2=typeof(double);</span>
Method 2: obtain from the variable

<span style="font-size:18px;">      int n = 1;            double d = 0.1;            Type t3 = n.GetType();            Type t4 = d.GetType();</span>


Object Attributes and method attributes

The common attribute is to judge the data type and return the bool type.

Isclass, isarray, isenum, isvaluetype

Method

Obtain all methods of the type:

<span style="font-size:18px;">  MethodInfo[] methods =t1.GetMethods();            foreach (MethodInfo method in methods)            {                Console.WriteLine(method.Name);            }</span>


You can also obtain all attribute field events of the class.

Assembly class

Class allows access to the Assembly metadata, which contains methods that can load and execute the assembly. Obtain Dynamic types from dll library

Create a class library
<span style="font-size:18px;">namespace Test{    classPerson    {        privatestring _name;         publicstring Name        {            get { return _name; }            set { _name = value; }        }        privatestring _address;         publicstring Address        {            get { return _address; }            set { _address = value; }        }        privateint _age;         publicint Age        {            get { return _age; }            set { _age = value; }        }        public Person(string name, string address, int age)        {            this._name = name;            this.Address = address;            this._age = age;        }        publicstring SayHi()        {            return"Hello ,My nameis" + this._name;        }    }}</span>


Stores the generated DLL class library in a specific location,

Creation type
<Span style = "font-size: 18px;"> Assembly ASSE = assembly. loadFile (@ "F: \ c ++ \ demo \ refelectdemo \ bin \ debug \ person. DLL "); Type T5 = ASSE. getType ("test. person "); console. writeline ("method of the person class"); methodinfo [] methods2 = t5.getmethods (); foreach (methodinfo m in methods2) {console. writeline (M. name) ;}</span>


Dynamic Object Creation

Create an object of Type dynamically obtained from the Foundation

<span style="font-size:18px;">  DateTime dt = (DateTime)Activator.CreateInstance(typeof(DateTime));</span>


Summary

Dynamic Control brings a lot of convenience to programmers. A specific application instance

Http://www.csharp-examples.net/reflection-examples/

C # reflection technology

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.