C# 反射实用

在.NET中的反射,常用的有三个方法:

Assembly.Load()

Assembly.Load(“程序集”)  //也就dll的命名空间
Assembly.LoadFrom()
Assembly.LoadFile()

动态加载ddl后开始操作,简单的写一下

 public class Class1 : IClass1
    { 
        public int Sum(int a, int b)
        {
            return a + b;
        }
    }
public interface IClass1
{
  int Sum(int a, int b);
}
Assembly assembly = Assembly.Load("FsTest");
IClass1 aa = (IClass1)assembly.CreateInstance("Class1");
int res = aa.Sum(11, 22);
             

  //1、获取指定路径的程序集对象
        Assembly assembly = Assembly.LoadFile(Path.Combine(Environment.CurrentDirectory, "CalculatorTest.dll"));
        //2、根据程序集对象和 类型名称创建对象,并且可以传递构造参数,然后可以用 objAny.GetType() 获取 Type
        object jiafaObj = assembly.CreateObject("CalculatorTest.JiaFaClass", 3, 4);
                    if (jiafaObj != null)
                    {
                        Type objType = jiafaObj.GetType();
        #region 第二种方式 创建Type和Type的object
        ////2、根据程序集对象和 命名空间.类全名  创建类型
        //Type objType = assembly.GetType("CalculatorDll.JiaFaClass");
        ////2.1、根据类型创建 类型的实例
        //object jiafaObj = Activator.CreateInstance(objType);
        ////2.2、构造函数方式赋值
        //ConstructorInfo ctor = objType.GetConstructor(new Type[] { typeof(double), typeof(double) });
        //ctor.Invoke(jiafaObj, new object[] { 3, 4 });
        #region 通过 属性或字段方式 而不是 在构造函数中 传值
        ////2.2属性方式赋值
        //PropertyInfo num1 = calculatorType.GetProperty("Number1");
        //PropertyInfo num2 = calculatorType.GetProperty("Number2");
        //if (num1 != null && num2 != null)
        //{
        //    num1.SetValue(obj, 3, null);
        //    num2.SetValue(obj, 4, null);
        //}
        ////2.2字段方式赋值
        //FieldInfo num1 = calculatorType.GetField("number1");
        //FieldInfo num2 = calculatorType.GetField("number1");
        //if (num1 != null && num2 != null)
        //{
        //    num1.SetValue(obj, 3);
        //    num2.SetValue(obj, 4);
        //} 
        #endregion
        #endregion
        //3、根据函数名称创建方法元数据
        MethodInfo jiSuanMethod = objType.GetMethod("JiSuan");
                        if (jiSuanMethod!=null)
                       {
                           //4、执行函数,以及获取函数返回值
                           object jiSuanReturn = jiSuanMethod.Invoke(jiafaObj, new object[] { });   //没参数用new object[] {}或 null 有参数用 new object[] {1,"str"} 这样

        Console.WriteLine(jiSuanReturn.ToString());
                       }
}

接下里是个人觉得比较好用的部分

OptimizeReflection 这个类库提供了一些扩展方法,它们用于优化常见的反射场景

 // 对于属性的读写操作、方法的调用操作,还提供了性能更好的强类型(泛型)版本 

            //Type instanceType = typeof(DemoClass);
            //PropertyInfo propertyInfo = instanceType.GetProperty("Id");
            //FieldInfo fieldInfo = instanceType.GetField("Name");
            //MethodInfo methodInfo = instanceType.GetMethod("Add");

            //// 1. 创建实例对象
            //DemoClass obj = (DemoClass)Activator.CreateInstance(instanceType);

            //// 2. 写属性
            //propertyInfo.SetValue(obj, 123);
            //propertyInfo.SetValue(obj, 123);

            //// 3. 读属性
            //int a = (int)propertyInfo.GetValue(obj);
            //int b = (int)propertyInfo.GetValue(obj);

            //// 4. 写字段
            //fieldInfo.SetValue(obj, "Fish Li");

            //// 5. 读字段
            //string s = (string)fieldInfo.GetValue(obj);

            //// 6. 调用方法
            //int c = (int)methodInfo.Invoke(obj, new object[] { 1, 2 });
            //int d = (int)methodInfo.Invoke(obj, new object[] { 3, 4 });

            //richTextBox1.AppendText(string.Format("a={0}; b={1}; c={2}; d={3}; s={4}", a, b, c, d, s));

猜你喜欢

转载自blog.csdn.net/weixin_42780928/article/details/91842411