SoFunction
Updated on 2025-03-07

C# uses reflection mechanism to achieve delayed binding

Reflection allows us to obtain the metadata of the assembly during the compilation period or at runtime, and through reflection, we can do:

● Create an instance of type
● Trigger method
● Get attribute and field information
● Delayed binding
......

If you use reflection during the compilation period, you can obtain the assembly Type type in two ways:

  • 1. Static methods of Type class

Type type = ("");

  • 2. Through typeof

Type type = typeof(someclass);

If reflection is used at runtime, get the Type type through the runtime Assembly instance method:

Type type = ("");

Get reflection information

There is a class like this:

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public float Score { get; set; }

        public Student()
        {
             = -1;
             = ;
             = 0;
        }

        public Student(int id, string name, float score)
        {
             = id;
             = name;
             = score;
        }

        public string DisplayName(string name) 
        {
            return ("Student name:{0}", name);
        }

        public void ShowScore()
        {
            ("Student scores are:" + );
        }
    }

Reflection information is obtained by:

        static void Main(string[] args)
        {
            Type type = ("");
            //Type type = typeof (Student);

            ();
            ();
            ();

            //Get attribute            PropertyInfo[] props = ();
            foreach (PropertyInfo prop in props)
            {
                ();
            }

            //Get method            MethodInfo[] methods = ();
            foreach (MethodInfo method in methods)
            {
                ();
                ();
            }
            ();
        }

Delayed binding

In general, assigning values ​​to an object instance occurs during the compilation period, as follows:

Student stu = new Student();
 = "somename";

"Delayed binding" assigns values ​​to object instances or calls its methods happen at runtime, and requires the assembly, Type type, method, attribute, etc. to be obtained at runtime.

            //Get the runtime assembly            Assembly asm = ();

            //Get the runtime Type type            Type type = ("");

            //Get the object instance at runtime            object stu = (type);

            //Get the runtime specified method            MethodInfo method = ("DisplayName");
            object[] parameters = new object[1];
            parameters[0] = "Darren";

            // Method to trigger the runtime            string result = (string)(stu, parameters);
            (result);
            ();

This is all about this article about C#’s use of reflection mechanism to achieve delayed binding. I hope it will be helpful to everyone's learning and I hope everyone will support me more.