Mein erster Beitrag ... Hoffnung nützlich schaut in meine Projektarbeit ...
public dynamic ConvertList(Type CallingType)
{
dynamic DynamicList;
if (CallingType == TypeOfValue)
{
Type d1 = typeof(List<>);
Type[] typeArgs = { TypeOfValue };
Type DynamicListType = d1.MakeGenericType(typeArgs);
object DynamicListObj = Activator.CreateInstance(DynamicListType);
DynamicList = Convert.ChangeType(DynamicListObj, DynamicListType);
foreach (object ValueElement in ValueRange)
{
dynamic el = Convert.ChangeType(ValueElement, TypeOfValue);
DynamicList.Add(el);
}
}
else //retrun empty List but with right type
{
Type d1 = typeof(List<>);
Type[] typeArgs = { CallingType };
Type DynamicListType = d1.MakeGenericType(typeArgs);
object DynamicListObj = Activator.CreateInstance(DynamicListType);
DynamicList = Convert.ChangeType(DynamicListObj, DynamicListType);
}
return DynamicList;
}
Ich denke, dass ich irgendwo auch einen Versuch fangen werde.
, wie Sie testen
if (PropertyType == typeof(UInt32))
{
List<UInt32> UInt32_test = NewProperty.ConvertList(PropertyType);
}
if (PropertyType == typeof(string))
{
List<string> string_test = NewProperty.ConvertList(PropertyType);
}
Dies wird 4 entweder in Version nicht möglich sein, weil es nicht zu ist typsicher entweder „upcast“ oder „gesenkten“ a 'Liste' - es ist unveränderlich, nicht kovariant oder kontravariant. Übrigens habe ich genug von dieser Fehlinformation, die auf jede Frage über die Sammlungs- und Kontravarianz gestellt wird. –