• C#反射代码


    Object model=Assembly.Load(“程序集”).CreateInstance(命名空间.类名);

    object obj2 = Type.GetType("MyClass").GetMethod("Add").Invoke(obj,null);

    obj2是返回值

    Activator.CreateInstance会调用dll中类的构造函数,如果你的类里面没有提供无参的构造函数

    Assembly _Assembly = Assembly.Load("程序集名");
    Type _Type = _Assembly.GetType("完整类名");
    Form _Form = (Form)Activator.CreateInstance(_Type);
    System.Reflection.EventInfo _EventInfo = _Type.GetEvent("RemoveFormHandler");

    _EventInfo.AddEventHandler(_Form, new Com.Psi.UIBase.RemoveFormDelegate(RemoveForm));

    System.Reflection.FieldInfo _FieldInfoSys = _Type.GetField("m_SysDataSet");
    _FieldInfoSys.SetValue(_Form, this.m_TableDataSet);

    _Form.Show()

    //创建对象
                ////Assembly assembly = Assembly.LoadFile();
                //Assembly assembly = Assembly.Load("Test2");
                //Object obj1 = assembly.CreateInstance("Test2.Reflect.Operate", false);//assembly.CreateInstance内部调用Activator.CreateInstance
                //Object obj2 = assembly.CreateInstance("Test2.Reflect.Operate", false, BindingFlags.Default, Type.DefaultBinder, new Object[] { "123" }, System.Globalization.CultureInfo.CurrentCulture, new Object[] { });

                //Object obj3 = Activator.CreateInstance(Type.GetType("Test2.Reflect.Operate"));
                //Object obj4 = Activator.CreateInstance(Type.GetType("Test2.Reflect.Operate"),"456");

                //Type type = Type.GetType("Test2.Reflect.Operate");
                ////每次调用InvokeMemeber方法时,它必须先绑定到一个特定的成员,然后才能调用它。
                ////如果每次调用一个成员时都让绑定器选择适当的成员,那么将是非常耗时的。。建议用:一次绑定,多次调用
                //Object obj5 = type.InvokeMember("Operate", BindingFlags.CreateInstance, null, null, new Object[] { "789" });
                
                //Assembly assembly = Assembly.Load("Test2");
                //Type t = assembly.GetType("Test2.Reflect.Operate");
                //ConstructorInfo constructorInfo = t.GetConstructor(new Type[] { typeof(string) });
                //Object obj6 = constructorInfo.Invoke(new Object[] { "kkkkkkkk" });

                //AppDomain appDomain = AppDomain.CurrentDomain;  //内部也是调用的Activator.CreateInstance
                //ObjectHandle objHandle = appDomain.CreateInstance("Test2", "Test2.Reflect.Operate");
                //Object obj7 = objHandle.Unwrap();

                //除了数组和委托之外,其他都可以利用上述通过反射创建对象

                //访问变量
                //Assembly assembly = Assembly.Load("Test2");
                //Object obj1 = assembly.CreateInstance("Test2.Reflect.Operate", false);
                //Type type = Type.GetType("Test2.Reflect.Operate");
                //FieldInfo fieldInfo = type.GetField("_Address");  //获取私有字段  ("_Address",BindingFlags.NoPublic|bindingFlags.instance)
                //Object ret= fieldInfo.GetValue(obj1);


         //私有静态字段,BindingFlags的次序必须一致。FieldInfo fieldInfo = type.GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic);


                //属性
                //Assembly assembly = Assembly.Load("Test2");
                //Object obj1 = assembly.CreateInstance("Test2.Reflect.Operate", false);
                //Type type = Type.GetType("Test2.Reflect.Operate");
                //PropertyInfo propertyInfo = type.GetProperty("Val");
                //propertyInfo.SetValue(obj1, 1, null); //为属性赋值
                //Object ret = propertyInfo.GetValue(obj1, null);//获取属性的值

                //索引器
                //Assembly assembly = Assembly.Load("Test2");
                //Object obj1 = assembly.CreateInstance("Test2.Reflect.Operate", false);
                //Type type = Type.GetType("Test2.Reflect.Operate");
                //PropertyInfo propertyInfo = type.GetProperty("Item");
                //Object ret = propertyInfo.GetValue(obj1,new Object[]{2});   //获取索引器的值
                //propertyInfo.SetValue(obj1, 1,new Object[]{6});             //为索引器赋值
                //ret = propertyInfo.GetValue(obj1, new Object[] { 6 });      //获取索引器的值
               

                //访问静态方法
                //Assembly assembly = Assembly.Load("Test2");
                //Type t = assembly.GetType("Test2.Reflect.Operate");
                //MethodInfo met = t.GetMethod("ExcStaticMethod", new Type[] { typeof(string) });
                //Object ret = met.Invoke(null, new Object[] { "参数" });

                //Type t = Type.GetType("Test2.Reflect.Operate");//如果在同一个项目中或已经添加引用
                //MethodInfo met = t.GetMethod("ExcStaticMethod", new Type[] { typeof(string) });
                //Object ret = met.Invoke(null, new Object[] { "传递的参数" });
              
                //访问方法
                //Assembly assembly = Assembly.Load("Test2");
                //Object obj1 = assembly.CreateInstance("Test2.Reflect.Operate", false);
                //Type t = assembly.GetType("Test2.Reflect.Operate");
                //MethodInfo met = t.GetMethod("ExcMethod", new Type[] {typeof(string) });
                //Object ret = met.Invoke(obj1, new Object[] {"参数" });

                //事件
                //Assembly assemblytemp = Assembly.Load("Test2");
                //Object objtemp = assemblytemp.CreateInstance("Test2.Reflect.Operate", false);
                //Type te = Type.GetType("Test2.Reflect.Operate");
                //EventInfo evnetInfo = te.GetEvent("hander");
                ////EventHandler eh = new EventHandler(delegate(Object obj,EventArgs e) { Console.WriteLine("LLLLLLL"); });
                //EventHandler eh = new EventHandler(delegate { Console.WriteLine("LLLLLLL"); });
                //evnetInfo.AddEventHandler(objtemp, eh);
                //MethodInfo met = te.GetMethod("ExcEvent", new Type[] { });
                //Object ret = met.Invoke(objtemp, new Object[] {  });

  • 相关阅读:
    研磨设计模式
    Java 集合类
    晚上提高项目效率,下午安卓又是过
    晚上提高项目效率,下午安卓又是过
    项目已经进行到医生管理,在完成文本框这个导入后就基本上剩下导出表格数据了
    晚上开始就要解决这个查询乱码的问题
    现在不能使用foxmail同步qq记事本功能,可能是对字数的大小有限制
    早上看到一张余票,可是没有等网页进入到结果页面,网络原因就票没了
    早上看到一张余票,可是没有等网页进入到结果页面,网络原因就票没了
    昨天晚上也弄不清楚是自己密码被盗了还是由于ip冲突
  • 原文地址:https://www.cnblogs.com/wupeiqi/p/3357538.html
Copyright © 2020-2023  润新知