SoFunction
Updated on 2025-03-07

C# reflection usage method and steps

The use of C# reflection is process and steps for your reference. The specific content is as follows

1. Define the full name of the class to be accessed

2. Get the type of this class

3. Instantiate the class

4. Get the fields, properties, and methods of this class

5. Set the field or attribute content, or call its method

This achieves the purpose of using strings to access the corresponding class.

Example:

1. Generate a new window according to the name of the window class, which is equivalent to the new window class

//1. Define the window class name: (The string name of the window class requires a full path name, otherwise the TYPE cannot be obtained)string customClassName = @"." + ;
 
//2. Get the string customClassName (the string name of a window class) The specified class TypeType customClassType = (customClassName);
                
//3. Generate an instance of the specified Type, equivalent to the new classobject customClassObj = (customClassType);
            
// 4. Operate on the newly generated class. This example assigns the new window to a window variablepanelForm = customClassObj as Form;
 
// This achieves the use of the string name of the class to generate an instance of the class for use by subsequent programs.

2. Assign values ​​to the field or attribute of the class according to the string name of the class

//Use and steps of reflection function: The following example is to generate an instance of the string class name and assign values ​​to the attributes or fields of the class. 
// 1. Define the class name: (The string name of the class is required, the full path name is required, otherwise the TYPE cannot be obtained)string customVaribleName = @"";
 
// 2. Get the Type of the specified class in customVaribleNameType customVaribleType = (customVaribleName);
 
// 3. Generate an instance of the specified Type, equivalent to the new classobject customAaribleObj = (customVaribleType);
 
// 4. Get the field of this class. In this example, assign a value to a common form variable, and this variable is a field here// If you assign values ​​to the attributes of this class, you need to use PropertyInfo pi = ("v" + )// In this class, is a domain a property or a field?  My personal judgment is that if the GET and SET methods are provided in this field, it is a property, otherwise it is a field, and I don’t know if it is correct?FieldInfo pi = ("v" + );
 
// 5. Assign a value to this field. This field is a window class variable. customClassObj is the window class generated based on the string in Example 1.(customAaribleObj, customClassObj);
 
// By the above5Steps,Completed according to the name of the string,Assign values ​​to fields of the corresponding class

3. Read the field value according to the string name of the class and use the field value (in the example, the field value is a form, the example is to destroy the form)

// 1. Define the class name: (The string name of the class is required, the full path name is required, otherwise the TYPE cannot be obtained)string customVaribleName = @"";
 
// 2. Get the Type of the specified class in customVaribleNameType customVaribleType = (customVaribleName);
 
// 3. Generate an instance of the specified Type, equivalent to the new classobject customAaribleObj = (customVaribleType);
 
// 4. Get the field of this class. In this example, assign a value to a common form variable, and this variable is a field here// If you assign values ​​to the attributes of this class, you need to use PropertyInfo pi = ("v" + )// In this class, is a domain a property or a field?  My personal judgment is that if the GET and SET methods are provided in this field, it is a property, otherwise it is a field, and I don’t know if it is correct?FieldInfo pi = ("v" + );
 
// 5. Read the value of this field (in this example, the field value is a form, read the form variable, and destroy the form)((customAaribleObj) as Form).Dispose();
 
// 6. Assign a null value to this field again(customAaribleObj, null);

4. Example 3 turns out that it is necessary to determine which window needs to be closed through switch, so there will be many case statements. Through reflection, the 6-line code in Example 3 is replaced. The code of the original program is posted below, with the purpose of letting everyone understand the role of reflection:

// The original code segment replaced by Example 3 (close the corresponding window according to the value and assign a value to the common variable)switch ()
            {
                case "FrmAccountStatistics":
                    ();
                     = null;
                    break;
                case "frmPositionManager":
                    ();
                     = null;
                    break;
                case "frmTrader":
                    ();
                     = null;
                    break;
                case "frmLog":
                    ();
                    = null;
                    break;
                case "frmDataTransPlant":
                  
                    ();
                     = null;
                    break;
                case "frmAccountTJ":
                    ();
                     = null;
                    break;
                case "frmGridViewConfig":
                    ();
                     = null;
                    break;
                case "frmTaticlistExcel":
                    ();
                     = null;
                    break;
                case "frmQuoteLst":
                    ();
                     = null;
                    break;
                case "frmProduct":
                    ();
                     = null;
                    break;
                default:
                    break;
            }

5. For a class defined by the general type of T, the example code to assign values ​​to attributes is as follows:

public static T ReadClass<T>(string pClassName) where T:class
        {
            //1. Define the class name: (Because there is an incoming T class, the class name is not needed) 
            //2. Get the TYPE of class T. Note that in the previous example, the type is obtained through a string, using ()            Type classType = typeof(T);
 
            //3. Generate an instance of specified T. In the previous example, we used (classType)            T ClassObj = <T>();
            
            //4. Define the attribute variable of the class. Since the program needs to read different attributes multiple times, a local variable is defined here.            PropertyInfo classPropertyInfo = null;
            
            string tableName = "TradeParameters";
            
            //DataTable must be converted to DataView to use RowFilter            DataTable vDt =  (tableName);
            DataView vDv = ;
             = $"ClassName = '{pClassName}'";
 
            if ( > 0)
            {
                string pName, pVaule;
                foreach(DataRow dr in vDv)
                {
                    pName = dr["AttributeName"].ToString();
                    pVaule = dr["AttributeValue"].ToString();
 
                    //5. Get the specified name attribute                    classPropertyInfo = (pName);
                    if (classPropertyInfo != null)
                    {
                        //6. Assign values ​​to the specified attribute                        (ClassObj, pVaule);
                    }
                    
                }
            }
            return ClassObj;
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.