C# 反射获取列表<&燃气轮机;从列表<;T>;(容器类型来自泛型容器类型)
我有一个特殊的情况,在其中一个反射器中,我可以得到不同类型的容器,我需要重新充气(比如克隆)。这是在引入新类型的容器时开始发生的(C# 反射获取列表<&燃气轮机;从列表<;T>;(容器类型来自泛型容器类型),c#,generics,reflection,collections,.net-4.5,C#,Generics,Reflection,Collections,.net 4.5,我有一个特殊的情况,在其中一个反射器中,我可以得到不同类型的容器,我需要重新充气(比如克隆)。这是在引入新类型的容器时开始发生的(ObservableCollection) 在克隆机制中,我发现: if (property.PropertyType.FullName.Contains(ReflectorResources.ListName) || property.PropertyType.FullName.Contains("ConcurrentBag")) { var listEle
ObservableCollection
)
在克隆机制中,我发现:
if (property.PropertyType.FullName.Contains(ReflectorResources.ListName) || property.PropertyType.FullName.Contains("ConcurrentBag"))
{
var listElementType = property.PropertyType.GenericTypeArguments[0];
var newList = (property.PropertyType.FullName.Contains(ReflectorResources.IncidentListName))
? Activator.CreateInstance(typeof(Definitions.Session.Products.Motor.IncidentList<>).MakeGenericType(listElementType))
: property.PropertyType.FullName.Contains("ConcurrentBag") ? Activator.CreateInstance(typeof(ConcurrentBag<>).MakeGenericType(listElementType)) : Activator.CreateInstance(typeof(List<>).MakeGenericType(listElementType));
var oneItem = Activator.CreateInstance(listElementType);
}
但是,它在以下行出现:var constructedListType=listType.MakeGenericType(listElementType)代码>有错误
System.InvalidOperationException:只能对Type.IsGenericParameter为true的类型调用方法
我猜我需要从List
中提取List
类型
如何从泛型容器类型中获取容器类型?我将引用其中的内容,这可能回答了您关于反射和泛型的任何问题:
要在运行时从构造的类型获取未绑定的类型,可以
使用
Type listofit=typeof(List);
类型列表=listOfInt.GetGenericTypeDefinition();//=类型(列表)
与此相反:
var listElementType = property.PropertyType.GenericTypeArguments[0];
var listType = property.PropertyType;
var constructedListType = listType.MakeGenericType(listElementType);
试试这个:
Type listElementType = property.PropertyType.GenericTypeArguments[0];
Type constructedListType;
if (! property.PropertyType.IsGenericTypeDefinition)
constructedListType = property.PropertyType;
else
{
// Depending on where your property comes from
// This should not work in the case the property type is List<T>
// How listElementType should allow you to instantiate your type ?
var listType = property.PropertyType.GetGenericTypeDefinition();
constructedListType = listType.MakeGenericType(listElementType);
}
Type listlementtype=property.PropertyType.GenericTypeArguments[0];
类型constructedListType;
如果(!property.PropertyType.IsGenericTypeDefinition)
constructedListType=property.PropertyType;
其他的
{
//取决于你的财产来自哪里
//在属性类型为List的情况下,这不应起作用
//listElementType应该如何允许您实例化您的类型?
var listType=property.PropertyType.GetGenericTypeDefinition();
constructedListType=listType.MakeGenericType(listElementType);
}
我还说你应该看看GetGenericTypeDefinition()
方法,但在我写完这篇文章之前,已经有了AakashM的答案。
然后你应该看看他的答案。快速评论:为什么不直接调用property.PropertyType上的CreateInstance?
var listElementType = property.PropertyType.GenericTypeArguments[0];
var listType = property.PropertyType;
var constructedListType = listType.MakeGenericType(listElementType);
Type listElementType = property.PropertyType.GenericTypeArguments[0];
Type constructedListType;
if (! property.PropertyType.IsGenericTypeDefinition)
constructedListType = property.PropertyType;
else
{
// Depending on where your property comes from
// This should not work in the case the property type is List<T>
// How listElementType should allow you to instantiate your type ?
var listType = property.PropertyType.GetGenericTypeDefinition();
constructedListType = listType.MakeGenericType(listElementType);
}