SoFunction
Updated on 2025-03-07

Review of C# basic knowledge--Reflection (II)


using System;
using ;
using ;
using ;
using ;
namespace Reflection
{
    class Program
    {
        static void Main(string[] args)
        {
           // Demo1();
            InvokeMethDemo();
            ();
        }

        static void InvokeMethDemo()
        {
//Get MyClass type formation
            Type t = typeof(MyClass);
            MyClass reflectOb = new MyClass(10, 20);
("Class name: {0}", );
("The methods provided in this class are:");
            MethodInfo[] mi = ();
            int val;
            foreach (MethodInfo m in mi)
            {
                ();

//Display parameters
                ParameterInfo[] pi = ();
                if ( == "Set" && pi[0].ParameterType == typeof(int))
                {
                    ("Set(int,int)  ");
                    object[] args = new object[2];
                    args[0] = 9;
                    args[1] = 18;
                    (reflectOb, args);
                }
                else if ( == "Set" && pi[0].ParameterType == typeof(double))
                {
                    ("Set(double,double)  ");
                    object[] args = new object[2];
                    args[0] = 2.34;
                    args[1] = 13.56;
                    (reflectOb, args);
                }
                else if (("Sum") == 0) {
                    ("Sum() ");
                    val = (int)(reflectOb, null);
                    ("Sum is {0}",val);
                }
                else if(("IsBetween")==0)
                {
                    object[] args = new object[1];
                    args[0] = 17;
                    if ((bool)(reflectOb, args))
                    {
("{0} is between x and y",args[0]);
                    }
                }
                ();
            }
        }
    }
}
class MyClass
{
    int x;
    int y;
    public MyClass(int i, int j)
    {
        x = i;
        y = j;
    }
    public int Sum()
    {
        return x + y;
    }
    public bool IsBetween(int i)
    {
        if (x < i && i < y)
            return true;
        else
            return false;
    }
    public void Set(int a, int b)
    {
        x = a;
        y = b;
        Show();
    }
    public void Set(double a, double b)
    {
        x = (int)a;
        y = (int)b;
        Show();
    }
    public void Show()
    {
        ("x:{0},y:{1}", x, y);
    }
}