2012-01-10 96 views
0
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); 

Activator.CreateInstance(objclass, paraObj)引发错误:C#反射 - 类型错误

System.Reflection.RuntimeParameterInfo can't be implicitly convert into Ektron.Cms.Search.Expresions.PropertyExpression

+0

什么是'paraObj'在上面的代码片段? – abhilash 2012-01-10 13:14:58

+0

@ABKolan一个'object []' – 2012-01-10 13:16:58

回答

1

存储在paraObj[0]值是RuntimeParameterInfo类型的,而对于EqualsExpression构造期望PropertyExpression类型的对象。您需要确保paraObj中的对象类型可以绑定到适合Activator的构造函数,以便能够实例化新对象。

要解决你的问题,你需要创建的PropertyExpression一个实例,并在您的paraObj阵列使用此为第一要素:

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); 
+0

EqualsExpression(PropertyExpression,long),long属性是可以接受的,但CreateInstance不能接受通过反射获取的PropertyExpression值。请给它一些解决方案。 – 2012-01-10 13:21:14

+0

@VinodKannan答案更新。 – 2012-01-10 13:40:29

0

您没有提供构造函数是从你的代码期望的类型,它的明确表示您通过PropertyInfo

如果你需要从PropertyInfo指向你的属性值必须使用 PropertyInfo.GetValue

我猜测(因为我没有晔代码)从您的代码段,你应该做的类似的东西 -

var propInfo = run_obj.GetType(
        "Ektron.Cms.Search.SearchContentProperty", 
        true,true).GetProperty("Language"); 

paraObj[0] = propInfo.GetValue(null,null) //depending on the requirement