.NET reflection learning summary 2

Knowing the assembly name and type name, the common form of calling the method directly after creating the reflection object:

Create a reflection test class:

using System;

namespace DB.MySql
{
    /// <summary>
    /// 反射测试类
    /// </summary>
    public class FlectionTest
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FlectionTest()
        {
            Console.WriteLine("这里是{0}无参构造函数", this.GetType());
        }

        /// <summary>
        /// 无参方法
        /// </summary>
        public void Show1()
        {
            Console.WriteLine("这里是{0}的show1", this.GetType());
        }

        /// <summary>
        /// 有参方法
        /// </summary>
        public void Show2(int id)
        {
            Console.WriteLine("这里是{0}的show2,实参id值为:{1}", this.GetType(), id);
        }

        /// <summary>
        /// 重载方法1
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public void Show3(int id, string name)
        {
            Console.WriteLine("这里是{0}的show3_1", this.GetType());
        }

        /// <summary>
        /// 重载方法2
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        public void Show3(string name, int id)
        {
            Console.WriteLine("这里是{0}的show3_2", this.GetType());
        }

        /// <summary>
        /// 重载方法3
        /// </summary>
        /// <param name="id"></param>
        public void Show3(int id)
        {
            Console.WriteLine("这里是{0}的show3_3", this.GetType());
        }

        /// <summary>
        /// 重载方法4
        /// </summary>
        /// <param name="name"></param>
        public void Show3(string name)
        {
            Console.WriteLine("这里是{0}的show3_4", this.GetType());
        }

        /// <summary>
        /// 重载方法5
        /// </summary>
        public void Show3()
        {
            Console.WriteLine("这里是{0}的show3_5", this.GetType());
        }

        /// <summary>
        /// 私有方法
        /// </summary>
        private void Show4(string name)
        {
            Console.WriteLine("这里是{0}的私有方法show4", this.GetType());
        }

        /// <summary>
        /// 静态方法
        /// </summary>
        /// <param name="name"></param>
        public static void Show5(string name)
        {
            Console.WriteLine("这里是{0}的静态方法show5", typeof(FlectionTest));
        }

        /// <summary>
        /// 泛型方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="W"></typeparam>
        /// <typeparam name="X"></typeparam>
        /// <param name="t"></param>
        /// <param name="w"></param>
        /// <param name="x"></param>
        public string Show6<T, W, X>(T t, W w, X x)
        {
            Console.WriteLine("泛型方法测试:t.type={0},w.type={1},x.type={2}", t.GetType().Name, w.GetType().Name, x.GetType().Name);
            return "我是泛型方法的返回值字符串";
        }
    }

}

Create an instance object: 

Assembly assembly = Assembly.Load("DB.MySql");//程序集的名称
Type type = assembly.GetType("DB.MySql.FlectionTest");//获取类型
object oTest = Activator.CreateInstance(type);//创建对象

 

 

Call a method without parameters:

MethodInfo info1 = type.GetMethod("Show1");
info1.Invoke(oTest, null);

 

Call a method with parameters:

MethodInfo info2 = type.GetMethod("Show2");
info2.Invoke(oTest, new object[] { 111 });

 

 

Call the overloaded method:

MethodInfo info3_1 = type.GetMethod("Show3", new Type[] { typeof(int), typeof(string) });
info3_1.Invoke(oTest, new object[] { 20, "张三" });

MethodInfo info3_2 = type.GetMethod("Show3", new Type[] { typeof(string), typeof(int) });
info3_2.Invoke(oTest, new object[] { "李四", 25 });

MethodInfo info3_3 = type.GetMethod("Show3", new Type[] { typeof(int) });
info3_3.Invoke(oTest, new object[] { 66 });

MethodInfo info3_4 = type.GetMethod("Show3", new Type[] { typeof(string) });
info3_4.Invoke(oTest, new object[] { "测试文字" });

MethodInfo info3_5 = type.GetMethod("Show3", new Type[] { });
info3_5.Invoke(oTest, null);

 

Call the private method:

MethodInfo info4 = type.GetMethod("Show4", BindingFlags.Instance | BindingFlags.NonPublic);
info4.Invoke(oTest, new object[] { "私有方法参数" });

 

Call the static method:

MethodInfo info5_1 = type.GetMethod("Show5");
info5_1.Invoke(oTest, new object[] { "参数1" });//调用静态方法可以传递实例

MethodInfo info5_2 = type.GetMethod("Show5");
info5_2.Invoke(null, new object[] { "参数2" }); //调用静态方法可以不传递实例

 

Call the generic method:

MethodInfo info6 = type.GetMethod("Show6");
var methodNew = info6.MakeGenericMethod(new Type[] { typeof(int), typeof(string), typeof(DateTime) });
object oReturn = methodNew.Invoke(oTest, new object[] { 2333, "泛型参数", DateTime.Now }); 
Console.WriteLine(oReturn.ToString());

 

Generic class + generic method:

Create a generic test class: generic class parameter T, generic method parameters T, W, X, the parameter T in the method comes from the class parameter

using System;

namespace DB.MySql
{
    public class GenericDouble<T>
    {
        public void Show<W, X>(T t, W w, X x)
        {
            Console.WriteLine("泛型类测试:t.type={0},w.type={1},x.type={2}", t.GetType().Name, w.GetType().Name, x.GetType().Name);
        }
    }
}

transfer:

Assembly assembly1 = Assembly.Load("DB.MySql");//程序集的名称
Type type1 = assembly1.GetType("DB.MySql.GenericDouble`1").MakeGenericType(typeof(int));//获取类型
object oObject = Activator.CreateInstance(type1);//创建对象
MethodInfo methodInfo = type1.GetMethod("Show").MakeGenericMethod(typeof(string), typeof(DateTime));
methodInfo.Invoke(oObject, new object[] { 666, "加油", DateTime.Now });

 

 

 

Guess you like

Origin blog.csdn.net/liangmengbk/article/details/112407694