C#反射类型错误
C#反射类型错误,c#,reflection,ektron,C#,Reflection,Ektron,Activator.CreateInstance(objclass,paraObj)引发错误: 无法隐式转换System.Reflection.RuntimeParameterInfo 进入Ektron.Cms.Search.Expresions.PropertyExpression paraObj[0]中存储的值的类型为RuntimeParameterInfo,而equalExpression的构造函数需要类型为PropertyExpression的对象。您需要确保paraObj中的对象类型可
Activator.CreateInstance(objclass,paraObj)
引发错误:
无法隐式转换System.Reflection.RuntimeParameterInfo
进入Ektron.Cms.Search.Expresions.PropertyExpression
paraObj[0]
中存储的值的类型为RuntimeParameterInfo
,而equalExpression
的构造函数需要类型为PropertyExpression
的对象。您需要确保paraObj
中的对象类型可以绑定到合适的构造函数,以便Activator能够实例化新对象
要解决问题,您需要创建PropertyExpression
的实例,并将其用作paraObj
数组中的第一个元素:
string assembly = "Ektron.Cms.ObjectFactory.dll";
string asspath = path + "bin\\" + assembly;
Assembly run_obj = Assembly.LoadFrom(@asspath);
paraObj[0] = run_obj.GetType(
"Ektron.Cms.Search.SearchContentProperty",
true,
true
).GetProperty("Language");
string equalExp = "Ektron.Cms.Search.Expressions.EqualsExpression";
Type objclass = run_obj.GetType(equalExp, true, true);
object objObj = Activator.CreateInstance(objclass, paraObj);
您没有提供构造函数从代码中期望的类型,很明显,您正在传递
PropertyInfo
如果需要PropertyInfo
指向的属性的值,则必须使用
我从您的代码片段推测(因为我没有Ektron代码),您应该做类似的事情-
string assembly = "Ektron.Cms.ObjectFactory.dll";
string asspath = path + "bin\\" + assembly;
Assembly run_obj = Assembly.LoadFrom(@asspath);
PropertyInfo propertyInfo = run_obj.GetType("Ektron.Cms.Search.SearchContentProperty", true, true).GetProperty("Language");
PropertyExpression propertyExpression = new PropertyExpression(propertyInfo); // create the property expression here, I am unsure how to instantiate it.
paraObj[0] = propertyExpression;
paraObj[1] = longValue;
string equalExp = "Ektron.Cms.Search.Expressions.EqualsExpression";
Type objclass = run_obj.GetType(equalExp, true, true);
object objObj = Activator.CreateInstance(objclass, paraObj);
在上面的代码片段中,
paraObj
是什么?equalExpression(PropertyExpression,long),long属性是可接受的,而CreateInstance不接受反射获取的PropertyExpression值。请给出一些解决办法。
var propInfo = run_obj.GetType(
"Ektron.Cms.Search.SearchContentProperty",
true,true).GetProperty("Language");
paraObj[0] = propInfo.GetValue(null,null) //depending on the requirement