我通過循環一個對象的屬性,在這裏我要結束了加入的IEnumerable或的ObservableCollection到已有的ObservableCollection屬性(Object類型)。鑄造的ObservableCollection <Object>到的ObservableCollection <Type>哪裏類型是未知
這是我到目前爲止有:
var PropList = TempObject.GetType().GetRuntimeProperties().ToList();
foreach (PropertyInfo ShowProp in PropList)
{
if (ShowProp.GetCustomAttribute<MarkedAttribute>() != null)
{
Type TypeObject = ShowProp.PropertyType.GenericTypeArguments[0];
// GetCollectionMethod returns a ObservableCollection<object>
// The code below obviosuly doesn't work, but that is what i want to achieve.
ObservableCollection<TypeObject> NewList = new ObservableCollection<TypeObject (GetCollectionMethod());
TempObject.GetType().GetRuntimeProperty(ShowProp.Name).SetValue(TempObject, NewList);
}
}
正如你看到的我是卡在鑄造ObservableCollection<object>
到ObservableCollection<TypeObject>
我曾嘗試創建使用Activator類的新的ObservableCollection,這工作精細。代碼如下:
// This creates an ObservableCollection<TypeObject>, but as soon as i fill it
// with a IENumerable<Object> it get's converted into a ObservableCollection<Object>
var listType = typeof(ObservableCollection<>);
var concreteType = listType.MakeGenericType(TypeObject);
var newList = Activator.CreateInstance(concreteType);
newList = GetCollectionMethod();
在上面的例子中,我做了我GetCollectionMethod()
返回IENumerable<Object>
代替,但它只是改變了我的ObservableCollection原<TypeObject>
。
我對此感到非常沮喪,並且使用Google搜索半天以上,如果有人能夠幫助我,我將不勝感激。
'TypeObject'是一個變量,而不是一個類型。它不能用作通用參數。 – Enigmativity 2014-11-23 09:27:12