2012-01-10 149 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