代码改变世界

.Net下反射用法

2014-02-07 14:48  chenmenghappy  阅读(216)  评论(0编辑  收藏  举报
                //需要反射的类
                string refClass = info.ChargeClass;
                //当前类所在的dll
                string path = Environment.CurrentDirectory + "\\ActionManager.dll";
                //加载dll
                Assembly t = Assembly.LoadFile(path);
                //创建类的实例
                object obj = t.CreateInstance(refClass);
                 
                Type type = obj.GetType();
                ChargePhoneInfo chargePhoneInfo = ChannelChangeMng.ChargePhoneInfoParse(info);
                CookieContainer cookie = new CookieContainer();
                string errMsg = string.Empty;
                //发射类中方法调用所需传递的参数
                object[] args = new object[] { chargePhoneInfo, cookie, errMsg };
                //SyncOrder 指的是需要调用反射类中的SyncOrder方法
                object objReturn = type.InvokeMember("SyncOrder", BindingFlags.InvokeMethod, null, obj, args);

                ChargeResult cr = (ChargeResult)objReturn;
                return cr;