C# 获取从列表派生的列表中的元素类型<;T>;在C中#
假设我有从C# 获取从列表派生的列表中的元素类型<;T>;在C中#,c#,generics,types,C#,Generics,Types,假设我有从列表派生的类: public类StringList:List{} 公共类名称列表:StringList{} 公共类IntList:List{} 现在我有了一个通用方法,它需要typeList: 公共静态方法(){…} 如何确定此方法中列表中包含的元素类型,即如何在派生类中获取泛型参数类型 对于基类,我可以调用typeof(T>.GetGenericArguments(),但对于派生类,它返回零大小 PS:在我的具体情况下,方法所期望的类型并不完全是List,而是IList您可以这样
列表
派生的类:
public类StringList:List{}
公共类名称列表:StringList{}
公共类IntList:List{}
现在我有了一个通用方法,它需要typeList
:
公共静态方法(){…}
如何确定此方法中列表中包含的元素类型,即如何在派生类中获取泛型参数类型
对于基类,我可以调用typeof(T>.GetGenericArguments()
,但对于派生类,它返回零大小
PS:在我的具体情况下,方法所期望的类型并不完全是
List
,而是IList
您可以这样编写方法:
public static void Method<T>(List<T> thing) (or IList<T>)
{
//Here, `T` is the type of the elements in the list
}
public static void Method<T, E>(T list) where T : List<E>
{
// example1
// T is List<int> and E is int
// example2
// T is NameList and E is String
}
Method<List<int>, int>(new List<int>()); //example1
Method<NameList, string>(new NameList()); //example2
公共静态无效方法(列表对象)(或IList)
{
//这里,`T`是列表中元素的类型
}
如果需要基于反射的检查,请执行以下操作:
public static void Method(Type myType)
{
var thing = myType.GetInterfaces()
.Where(i => i.IsGenericType)
.Where(i => i.GetGenericTypeDefinition() == typeof(IList<>))
.FirstOrDefault()
.GetGenericArguments()[0];
}
publicstaticvoid方法(类型myType)
{
var thing=myType.GetInterfaces()
.Where(i=>i.IsGenericType)
.Where(i=>i.GetGenericTypeDefinition()==typeof(IList))
.FirstOrDefault()
.GetGenericArguments()[0];
}
请注意,此处需要适当的健全性检查(而不是
FirstOrDefault()
和0索引)您可以这样编写方法:
public static void Method<T>(List<T> thing) (or IList<T>)
{
//Here, `T` is the type of the elements in the list
}
public static void Method<T, E>(T list) where T : List<E>
{
// example1
// T is List<int> and E is int
// example2
// T is NameList and E is String
}
Method<List<int>, int>(new List<int>()); //example1
Method<NameList, string>(new NameList()); //example2
公共静态无效方法(列表对象)(或IList)
{
//这里,`T`是列表中元素的类型
}
如果需要基于反射的检查,请执行以下操作:
public static void Method(Type myType)
{
var thing = myType.GetInterfaces()
.Where(i => i.IsGenericType)
.Where(i => i.GetGenericTypeDefinition() == typeof(IList<>))
.FirstOrDefault()
.GetGenericArguments()[0];
}
publicstaticvoid方法(类型myType)
{
var thing=myType.GetInterfaces()
.Where(i=>i.IsGenericType)
.Where(i=>i.GetGenericTypeDefinition()==typeof(IList))
.FirstOrDefault()
.GetGenericArguments()[0];
}
请注意,此处需要进行适当的健全性检查(而不是
FirstOrDefault()
和0索引)如果在编译时同时需要列表的类型和列表的元素类型,则方法必须有两个类似的通用定义:
public static void Method<T>(List<T> thing) (or IList<T>)
{
//Here, `T` is the type of the elements in the list
}
public static void Method<T, E>(T list) where T : List<E>
{
// example1
// T is List<int> and E is int
// example2
// T is NameList and E is String
}
Method<List<int>, int>(new List<int>()); //example1
Method<NameList, string>(new NameList()); //example2
公共静态无效方法(T list),其中T:list
{
//例1
//T是List,E是int
//例2
//T是名称列表,E是字符串
}
方法(new List());//示例1
方法(新名称列表());//示例2
如果您希望在编译时同时获得列表的类型和列表的元素类型,则您的方法必须具有两个类似以下的通用定义:
public static void Method<T>(List<T> thing) (or IList<T>)
{
//Here, `T` is the type of the elements in the list
}
public static void Method<T, E>(T list) where T : List<E>
{
// example1
// T is List<int> and E is int
// example2
// T is NameList and E is String
}
Method<List<int>, int>(new List<int>()); //example1
Method<NameList, string>(new NameList()); //example2
公共静态无效方法(T list),其中T:list
{
//例1
//T是List,E是int
//例2
//T是名称列表,E是字符串
}
方法(new List());//示例1
方法(新名称列表());//示例2
作为旁白,将列表子类化通常不是一个好主意。请参见。否则,您可以检查接口并查找IList
,以获取string@Rob好的,我会记住这一点。顺便说一句,将列表
子类化通常不是一个好主意。请参见。否则,您可以检查接口和d查找IList
以获取string@Rob很好,我会记住这一点。再次感谢,反射是我所需要的。再次感谢,反射是我所需要的。如果我们选择不使用反射,这是一个很好的解决方案,但在我的情况下,一个额外的参数很重要(没有我没有的方法参数)。如果我们选择不使用反射,这是一个很好的解决方案,但在我的情况下,一个额外的参数很重要(没有我没有的方法参数)。