首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

反射如何传类型参数

2012-09-13 
反射怎么传类型参数?比如我已知反射类里面有个Class 想传这个类型的参数,本地该怎么去做呢C# codepublic c

反射怎么传类型参数?
比如我已知反射类里面有个Class 想传这个类型的参数,本地该怎么去做呢

C# code
    public class RelevanceGoodsItems    {        public RelevanceGoodsItems(string _totaltitle, string _morelink)        {            Items = new List<RelevanceGoodsItem>();            TotalTitle = _totaltitle;            MoreLink = _morelink;        }        public string TotalTitle;        public string MoreLink;        public List<RelevanceGoodsItem> Items;    }


完整代码, 反射里面有个方法
C# code
namespace ADO{    public class Test    {        //这个是我要反射的方法        public static string GetMethodByRef(RelevanceGoodsItems InputValue)        {            return InputValue.TotalTitle;        }        public class RelevanceGoodsItems        {            public RelevanceGoodsItems(string _totaltitle, string _morelink)            {                Items = new List<RelevanceGoodsItem>();                TotalTitle = _totaltitle;                MoreLink = _morelink;            }            public string TotalTitle;            public string MoreLink;            public List<RelevanceGoodsItem> Items;        }    }}//下面是我本地的代码public class LocalTest{    RelevanceGoodsItems items = new RelevanceGoodsItems("我是标题", "");            MethodInfo mi = _Compiled.GetType().GetMethod("GetMethodByRef", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);             return mi.Invoke(_Compiled, new Object[1] { items }).ToString();    //我本地同时建立了一个跟反射类参数同样的类    public class RelevanceGoodsItems    {        public RelevanceGoodsItems(string _totaltitle, string _morelink)        {            Items = new List<RelevanceGoodsItem>();            TotalTitle = _totaltitle;            MoreLink = _morelink;        }        public string TotalTitle;        public string MoreLink;        public List<RelevanceGoodsItem> Items;    }}


我这样调用会提示类型“RelevanceGoodsItems”的对象无法转换为类型“ADOGuy._Evaluator+RelevanceGoodsItems”。

请问该怎么传参呢?

[解决办法]
探讨
引用:

完整代码
C# code
var tt = typeof(Test);
var rgit = tt.GetNestedType("RelevanceGoodsItems", BindingFlags.Public);
var rgic = rgit.GetConstructor(new Type[] { typeof(string), typeof(stri……

[解决办法]
探讨

引用:

你只能传入一样的对象(Type),别听他忽悠,根本弄不了的

比如方法要求的是 参数是Namespace Namespace1.Class1
Namespane 为 Namespace1
public class Class1
{
public int X{get;set;}
}




那你就不能传入Namespace为Namespac……

热点排行