如何获取列表中元素的类型 [英] How to obtain type of an elements in list
问题描述
请你建议
我有未知类型的元素列表...比如, Type = typeof(List< ; of_something>)
如何确定它是否是List<>的类型或数组
如何确定所包含元素的类型 - 即 typeof(of_something)
谢谢你转发建议
我尝试过:
我不喜欢我知道如何实现这个
我被困了
Could you please suggest
I have TYPE of list of an elements of "unknown" type ... say, the Type = typeof(List<of_something>)
how to determine whether it's a type of List<> or Array
how to determine the type of an elements included - that is typeof(of_something)
thank you forward for advice
What I have tried:
I don't have an idea how to implement this
I am stuck
推荐答案
是的,你可以做到。
尝试以下扩展方法:
Yes, you can do it.
Try these extension methods:
public static class Extensions
{
public static Type GetContainedType<T>(this List<T> ls)
{
return typeof(T);
}
public static Type GetContainedType(this Array ar)
{
return ar.GetType().GetElementType();
}
}
然后你可以在你的收藏中调用它们:
You can then call them on your collections:
List<string> sl = new List<string>();
string[] sa = new string[10];
List<int> il = new List<int>();
int[] ia = new int[10];
Type tsl = sl.GetContainedType();
Type tsa = sa.GetContainedType();
Type til = il.GetContainedType();
Type tia = ia.GetContainedType();
嗨!
我假设是这样的:
Hi!
I supposed something like that:
class Program
{
static void Main(string[] args)
{
Type t = (new List<MyType>() {new MyType(), new MyType(), new MyType() }).GetType(); //this is just for pupose of getting TYPE of List<string>
//at this position we are interested in Type, not object itself
//there is no object - just "Type" or PropertyInfo
TypeInfo pi = t.GetTypeInfo();
PropertyInfo prop = pi.DeclaredProperties.Where(c => c.Name == "Item").FirstOrDefault();
Type tp = prop.PropertyType;
object obj = tp.InvokeMember("", BindingFlags.CreateInstance, null, null, null);
//now I need to cast the obj to the appropriate type to call the method Name()
}
}
class MyType
{
public string Name()
{
return "It's me";
}
}
现在我需要将obj转换为合适的类型调用方法Name()
class Program
{
static void Main(string[] args)
{
Type t = (new List<MyType>() {new MyType("1"), new MyType("2"), new MyType("3") }).GetType(); //this is just for pupose of getting TYPE of List<string>
//at this position we are interested in Type, not object itself
//this is no object - just "Type" or PropertyInfo
TypeInfo pi = t.GetTypeInfo();
PropertyInfo prop = pi.DeclaredProperties.Where(c => c.Name == "Item").FirstOrDefault(); //no matter how much objects were built and included in List (again - there is no object - just TYPE)
Type tp = prop.PropertyType;
object obj = tp.InvokeMember("", BindingFlags.CreateInstance, null, null, new object[] { "from Invoke" });
//now we need to cast the obj to the appropriate type to call the method Name()
object name = tp.InvokeMember("Name", BindingFlags.InvokeMethod, null, obj, null);
//on my opinion there is just one way to invoke inner method without casting an object "obj" to the type "tp"
//but still it would be better to obtain an object "obj" of properly type.
}
}
class MyType
{
public MyType()
{
Console.Write("I was born => my name is: ");
}
public MyType(string s) : this()
{
Console.WriteLine(s);
}
public string Name()
{
return "It's me " ;
}
}
我认为只有一种方法可以调用内部方法而无需投射一个对象obj到类型tp
但仍然最好获得一个正确类型的对象obj。
这篇关于如何获取列表中元素的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!