C # Reflection

Source: Internet
Author: User

Reflection definition: The ability to review metadata and collect information about its type, metadata (the edited basic data unit) is a bunch of tables, and the compiler creates a class definition table, a field definition table, a method definition table, and so on. The System.Reflection namespace contains several classes that allow you to reflect (parse) the code for these metadata.
Simple to understand: that is, when the compiler compiles reflection-related code, it creates one table for all classes, methods, properties, and so on, and the elements of each table correspond to the code in part of the metadata. When we use the class name string to create this class, the program finds the class name we provide in the table in the Save class, and then finds the corresponding constructor and executes it.

Types in Reflection

MemberInfo-member of the Reflection class
ConstructorInfo-Constructors for reflection classes
fieldinfo- fields for reflection classes
The method of methodinfo- reflection class's constructor function
PropertyInfo-Properties of a reflection class's constructor
EventInfo-event that reflects the constructor of a class

Code

Using Reflectioninjection.helper;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Reflection;
Using System.Text;
Using System.Threading.Tasks;


Namespace Reflection
{
Class Program
{
static void Main1 (string[] args)
{

Assembly Assembly = Assembly.Load ("TestClass"); TestClass Assembly DLL files
Type[] Types = assembly. GetTypes ();
Type type = Types[0];
String className = "Reflection." + "Student";
String className2 = "Reflection." + "person"; The person represents the class to be looked up in this assembly; Reflection represents the Assembly
String className3 = "Reflectioninjection.helper." + "Macbuttonn";
Student stu= assembly.load ("Reflection"). CreateInstance (ClassName) as Student;
Person person = assembly.load ("Reflection"). CreateInstance (className2) as person;
Macbuttonn Macbutton = Assembly.Load ("Reflectioninjection"). CreateInstance (ClassName3) as Macbuttonn;

Console.readkey ();
}

static void Main (string[] args)
{

Assembly Assembly = Assembly.Load ("TestClass");
Type[] Types = assembly. GetTypes (); Get all the data types in the dataset (that is, the classes we define in the DLL)
Type type = Types[0];
Object obj1 = Activator.CreateInstance (type);
fieldinfo[] Listfinfo = type. GetFields (); Field
foreach (FieldInfo finfo in Listfinfo)
{
var name = Finfo. GetValue (OBJ1); The value of the field
Console.WriteLine ("Name of field" + Finfo. name+ "field value" +name);
}
propertyinfo[] Listpropertyinfo = type. GetProperties (); Property
foreach (PropertyInfo PropertyInfo in Listpropertyinfo) {
var name = Propertyinfo.getvalue (obj1, NULL); The value of the property
Console.WriteLine ("Name of attribute" + propertyinfo.name);
}

methodinfo[] Minfos = type. GetMethods (); Method
foreach (MethodInfo Me in Minfos)
{
if (Me. Name = = "Run")
{
Object O=me. Invoke (obj1, NULL); Calling methods
string ss = "Nihao";

}
}


Console.WriteLine (type. FullName);
Console.readkey ();
}

}

public class Student
{
public string _name = "DSC";
public string Name
{
Get
{
return _name;
}
Set
{
_name = value;
}
}

public string Run ()
{
Return "Hello World";
}
}
}

Namespace TestClass
{
Class Person
{

public string _name = "DSC";
public string Name {get; set;}

public int Age {get; set;}

public string Run ()
{
Return "Hello, I am the test class";
}
}
}

C # Reflection

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.