我有名字&對象的類型和方法名稱。 理想情況下,我想實例化調用該方法的對象&。所有這一切都很好,但我正在嘗試處理結果時出錯。Declare&Iterate collection <T>
private void GetData(DropDownList ddl)
{
ObjectDataSource ods = (ObjectDataSource)ddl.DataSourceObject;
System.Reflection.Assembly assembly = typeof(ExistingObject).Assembly;
Type type = assembly.GetType(ods.SelectMethod);
var instance = Activator.CreateInstance(type);
IterateCollection(instance, ddl);
}
private static void IterateCollection<T>(T instance, DropDownList ddl)
{
ObjectDataSource ods = (ObjectDataSource)ddl.DataSourceObject;
Type type = instance.GetType();
MethodInfo methodInfo = type.GetMethod(ods.SelectMethod);
LinkedList<T> col = (LinkedList<T>)methodInfo.Invoke(null, new object[] { (T)instance });
foreach (T o in col)
{
string textfield = Convert.ToString(GetPropValue(o, ddl.DataTextField));
string valuefield = Convert.ToString(GetPropValue(o, ddl.DataValueField));
ListItem li = new ListItem(textfield, valuefield);
if ((bool?)GetPropValue(o, "Active") != true)
li.Attributes.CssStyle.Add("background-color", "gray");
ddl.Items.Add(li);
}
}
我對 「調用」 行話說 System.InvalidCastException : Unable to cast object of type 'System.Collections.Generic.LinkedList``1[Business.Objects.MyType]' to type 'System.Collections.Generic.ICollection
1 System.Object的]」得到一個錯誤。
我想能夠遍歷集合。我怎樣才能做到這一點? 如何創建一個LinkedList或類似的?
由於
我該如何創建'LinkedList'? –
ShrapNull