C#学习:通过反射设置泛型类型的属性及值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#学习:通过反射设置泛型类型的属性及值相关的知识,希望对你有一定的参考价值。

1.引用

1 IResult result = ExecuteDmsParam<DMSParam, ORD_PurchaseParam>(dmsParam, purchaseParam, "HQBuy.MVC.BLL.Member.ORD.ORD_PurchaseBLL", "GetPurchaseByPNO");

 2.方法

        public IResult ExecuteDmsParam<T1, T2>(T1 t1, T2 t2, string ClassName, string MethodName)
        {
            Type type = t1.GetType();
            if (type.GetProperties() != null && type.GetProperties().Length > 0)
            {
                foreach (var propertie in type.GetProperties())
                {
                    if (!string.IsNullOrEmpty(propertie.Name))
                    {
                        switch (propertie.Name)
                        {
                            case "AssemblyName":
                                t1.GetType().GetProperty(propertie.Name).SetValue(t1, "HQBuy.MVC.BLL.Member.dll");
                                break;
                            case "ClassName":
                                t1.GetType().GetProperty(propertie.Name).SetValue(t1, ClassName);
                                break;
                            case "MethodName":
                                t1.GetType().GetProperty(propertie.Name).SetValue(t1, MethodName);
                                break;
                            case "Param":
                                t1.GetType().GetProperty(propertie.Name).SetValue(t1, t2);
                                break;
                        }
                    }
                }
            }
            IResult result = ExecuteBLL.Execute((IParam)t1);
            if(result.Complete == DMSComplete.Succeed)
            {
                ConditionResult<ORD_Purchase> purchaseCondition = new ConditionResult<ORD_Purchase>();
                if (result.Result != null)
                {
                    purchaseCondition = (ConditionResult<ORD_Purchase>)result.Result;
                    List<ORD_Purchase> purchaseList = (purchaseCondition).ResultList;
                }
            }
            return result;
        }

 

以上是关于C#学习:通过反射设置泛型类型的属性及值的主要内容,如果未能解决你的问题,请参考以下文章

unity的C#学习——泛型的创建与继承泛型集合类泛型中的约束和反射

如何反射获取List<T> 中泛型的类型

c#的泛型,委托,反射是啥?

c# 如何通过反射 获取设置属性值

C# 如何获取泛型集合 的数据

C#反射实例学习及注意内容