Let's see how to use reflection in C#

Reflection in C# is often used to obtain type metadata when the program is running. The information that can be obtained includes the assembly and type information that has been loaded into the process. It is similar to RTTI (Runtime Type Information) in C++. .

Reflection in C# is often used to obtain type metadata when the program is running. The information that can be obtained includes the assembly and type information that has been loaded into the process. It is similar to RTTI (Runtime Type Information) in C++. .

In order to be able to use reflection, you need to reference the System.Reflection namespace in the project. At the beginning of using reflection, you will get an object of type Type, and further obtain assembly, type, module and other information from this object, which can be dynamically reflected Generate an instance of a certain type, and even dynamically call methods on this type.

In the System.Reflection namespace, the following core types are defined.

Assembly
Module
Enum
MethodInfo
ConstructorInfo
MemberInfo
ParameterInfo
Type
FieldInfo
EventInfo
PropertyInfo

Now let's study how to use it together, consider the Customer class defined below.

public class Customer 
    { 
        public int Id { get; set; } 
 
        public string FirstName { get; set; } 
 
        public string LastName { get; set; } 
 
        public string Address { get; set; } 
    } 

The following code snippet shows how to obtain the class name of Customer and the namespace of Customer through reflection.

class Program 
   { 
       static void Main(string[] args) 
       { 
           Type type = typeof(Customer); 
 
           Console.WriteLine("Class: " + type.Name); 
           Console.WriteLine("Namespace: " + type.Namespace); 
       } 
   } 

Let's see how to use reflection in C# Let's see how to use reflection in C#
Let's look at another example, how to get all the attributes under Customer through reflection, and display all the attribute names on the console, as shown in the following code:

static void Main(string[] args) 
        { 
            Type type = typeof(Customer); 
 
            PropertyInfo[] propertyInfo = type.GetProperties(); 
 
            Console.WriteLine("The list of properties of the Customer class are:--"); 
 
            foreach (PropertyInfo pInfo in propertyInfo) 
            { 
                Console.WriteLine(pInfo.Name); 
            } 
        } 

Let's see how to use reflection in C# Let's see how to use reflection in C#
It is worth noting that typeof(Customer).GetProperties() can only get the set of properties marked as public by default, corresponding to the four public properties under the Customer class.

Next, let’s take a look at how to obtain the metadata information of the constructor and public methods under the type through reflection. Here we continue to use the Customer class, and add a constructor and a Validate method to the class. This method is used to verify the input parameters. The legality of the following is the modified Customer class.

public class Customer 
    { 
        public int Id { get; set; } 
 
        public string FirstName { get; set; } 
 
        public string LastName { get; set; } 
 
        public string Address { get; set; } 
 
        public Customer() { } 
 
        public bool Validate(Customer customerObj) 
        { 
            //Code to validate the customer object 
            return true; 
        } 
    } 

Then let's take a look at obtaining all the constructors defined under Customer through reflection, but only one constructor is defined here, so only one can be listed.

class Program 
    { 
        static void Main(string[] args) 
        { 
            Type type = typeof(Customer); 
 
            ConstructorInfo[] constructorInfo = type.GetConstructors(); 
 
            Console.WriteLine("The Customer class contains the following Constructors:--"); 
 
            foreach (ConstructorInfo c in constructorInfo) 
            { 
                Console.WriteLine(c); 
            } 
        } 
    } 

Let's see how to use reflection in C# Let's see how to use reflection in C#
Also note that, by default, the GetConstructors() method can only get all the constructors of Customer that are marked as public.

Next, let's see how to display all public methods in Customer. Because there is only one public method defined in this class, only one should be displayed on the console. The following code is for reference only.

static void Main(string[] args) 
       { 
           Type type = typeof(Customer); 
 
           MethodInfo[] methodInfo = type.GetMethods(); 
 
           Console.WriteLine("The methods of the Customer class are:--"); 
 
           foreach (MethodInfo temp in methodInfo) 
           { 
               Console.WriteLine(temp.Name); 
           } 
 
           Console.Read(); 
       } 

Let's see how to use reflection in C# Let's see how to use reflection in C#
Are you surprised? I said just now that it is one method, but there are a few more methods. You need to know that the more methods come from two aspects.

Public methods inherited from the object type
Let's see how to use reflection in C# Let's see how to use reflection in C#

Attribute method automatically generated by the compiler
Let's see how to use reflection in C# Let's see how to use reflection in C#

If Attribute is marked on the method, it can also be obtained through the GetCustomAttributes method. The reference code is as follows:

static void Main(string[] args) 
        { 
            foreach (MethodInfo temp in methodInfo) 
            { 
                foreach (Attribute attribute in temp.GetCustomAttributes(true)) 
                { 
                    //Write your usual code here 
                } 
            } 
        } 

I believe that in your application, various Attribute characteristics are often used on the domain entity. At this time, you can extract the Attribute information on the method in the domain entity through the above code reflection, so as to execute your specific according to the extracted Attribute Business logic.

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/113804335