SoFunction
Updated on 2025-03-07

Summary of basic application examples of C# reflection

using System;
using ;
using ;
using reflectPrj;
using ;
using ;
 
namespace reflectPrjTest
{
    class MyReflectTest
    {
// Establish a commission
        delegate string TestDelegate (string a,string b);
 
        static void Main(string [] args)
        {
            Assembly assembly= Assembly .Load("reflectPrj" );
 
            foreach (Type var in ())
            {
Console .WriteLine();// Show all classes under dll
            }
 
            //*******************************************************
 
            Module [] modules = ();
 
            foreach (Module module in modules)
            {
Console .WriteLine("module(module/component) name:" +);
            }
 
            //*******************************************************
// Get the specific class type
            Type a = typeof ( );
          
            Console .WriteLine();
 
            //*******************************************************
//A------- Create an instance of type----> Here is an instance obtained from the constructor with arguments
            string [] paras ={"aaa" ,"bbb" };
// Create an instance of this class, the following paras is the parameter of the parameter constructor ----> This obj is an instance of type a
// This instance calls a constructor with arguments
            object obj = Activator .CreateInstance(a,paras);
 
// Get the object's properties
Console .WriteLine(" Gets the property of object a: ");
            foreach (object var in ())
             {
                 Console .WriteLine(());
             }
          
            MethodInfo [] miArr = ();
 
Console .WriteLine(" Show all common methods: " );
// Show all shared methods
            foreach (MethodInfo method in miArr)
            {
                Console .WriteLine();
            }
          
            //************************************************************
// Show specific methods
Console .WriteLine("Show specific method!");
 
//1. Use of methods with parameters
            MethodInfo mi = ("WriteString" );
string miReturn =(string ) (obj, new object [] { "Use a non-static method with parameters" , "2" });
 
Console .WriteLine("---" ++" Return value: " +miReturn);
 
//2. Method calls without parameters
Console .WriteLine(" Call of method without parameters: ");
            MethodInfo miNopara = ("WriteNoPara" );
 
            string miNoparaReturn = (string )(obj, null );
 
            Console .WriteLine("---" +miNoparaReturn);
 
//3. Use of private type methods
Console .WriteLine(" Use of private type methods: ");
            MethodInfo miPrivate = ("WritePrivate" ,BindingFlags .Instance | BindingFlags .NonPublic);
 
            string miPrivateReturn = (string )(obj, null );
 
            Console .WriteLine("---" +miPrivateReturn);
 
Console .WriteLine("*********************************************************" );
 
//4. Get the attributes of the object
            PropertyInfo [] propertys = (BindingFlags .Instance | BindingFlags .NonPublic |BindingFlags .Public);
         
//5. Show all attribute names
Console .WriteLine(" All attribute names of the object are as follows: ");
            foreach (PropertyInfo pro in propertys)
            {
                //();
// Get the initial value of the attribute
                Console .WriteLine(+" :" +(obj,null ));
 
// Reassign attributes
(obj, "Zhang Sanfeng" , null );
                Console .WriteLine( + " :" + (obj, null ));
 
             }
        
//6. Get the specified attribute and assign a value
            PropertyInfo propertyInfo=("WriteA" ,BindingFlags .Instance| BindingFlags .NonPublic|BindingFlags .Public);
 
(obj, " tulip" , null );
 
            Console .WriteLine(+" :" +(obj,null ));
 
Console .WriteLine("******************FieldInfo--- Use of public fields************************************" );
//7. Use of fields ----> Only public fields can be obtained
            FieldInfo f1 = ("writea" ,BindingFlags .Instance| BindingFlags .NonPublic| );
 
            Console .WriteLine((obj));
 
            try
            {
                test = new ReflectTest ("Marry" , "Jack" );
 
                Type myReflect = typeof (ReflectTest );
 
                FieldInfo field1= ("writea" , BindingFlags .Public | BindingFlags .NonPublic |BindingFlags .Instance);
 
                Console .WriteLine((test));
            }
            catch (Exception ex)
             {
                Console .WriteLine();
            }
 
            //*******************************************************
//8. Use of constructors
Console .WriteLine("get the form of the constructor");
            ConstructorInfo [] cis =();
            foreach (ConstructorInfo ci in cis)
            {
Console .WriteLine(" the form of the constructor:" +());// Get the form of the constructor
Console .WriteLine(" Constructor name: " +);
            }
 
// Print form with parameter constructor
            ConstructorInfo ascSingle = (new Type [] { typeof (string ),typeof (string )});
 
            Console .WriteLine(());
 
            //****************************************************
//9. Factory model
            Console .WriteLine();
            reflectPrj.Interface1 reflectObj2 = (Interface1 )("" );
            reflectObj1 = (ReflectTest )("" );
 
Console .WriteLine(());// Typical factory model, in subsequent real applications, methods in interfaces are used (and interfaces are implemented by classes that implement interfaces)
            Console .WriteLine(());
 
//10 Factory mode--- Method reload
            int num = 300;
            Console .WriteLine((300));
 
Console .WriteLine("Re-implementation of factory model!");
            foreach (Type type in ())
            {
                if (("reflectPrj.Interface1" )!=null )
                {
// The interface is implemented by the interface implementation class
                    reflectPrj.Interface1 interfaceObj3 = (Interface1 )Activator .CreateInstance(a);
 
                    Console .WriteLine(());
                    Console .WriteLine((600));
                }
            }
            //****************************************************
//11. A simple example of dynamically creating a delegate ---> Bind the delegate to the target method
            TestDelegate myMothod = (TestDelegate )Delegate .CreateDelegate(typeof (TestDelegate ),obj,mi);
 
            Console .WriteLine(("Tom" , "Jack" ));
 
            //****************************************************
//B------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Console .WriteLine("Use of parameterless constructor!");
 
//objNonePara is an object generated by the constructor of the ReflectTest class
            object objNonePara = Activator .CreateInstance(a);
 
            string info=(( )objNonePara).WriteNoPara();
 
            Console .WriteLine(info);
 
            PropertyInfo proWriteA = (((ReflectTest )objNonePara).WriteA);
            Console .WriteLine((objNonePara,null ));
 
(objNonePara, "Little Tulip" , null );
 
            Console .WriteLine((objNonePara, null ));
 
//C:---------- Use the constructor with parameters to generate the instance
ReflectTest objPara =(ReflectTest ) Activator .CreateInstance(a, new object [] {" Xiaoyu" ," Come on" });
            Console .WriteLine(+"/t" +);
            Console .WriteLine(());
 
// Call method with parameters
Console .WriteLine(("Dayu", "Go home!" ));
            MethodInfo myMi=("WriteString" );
 
// Use the designated method to bind the delegate dynamically
            myMothod=(TestDelegate )Delegate .CreateDelegate(typeof (TestDelegate ),objPara,myMi);
Console .WriteLine(("New Year is coming", "Happy Year of the Ox!" ));
 
// The screen pauses
            Console .ReadLine();
        }
     }
}