C# 泛型列表如何获取T的类型?
我正在开展一个反思项目,但现在我陷入了困境。
如果我有一个可以容纳 List
的 myclass
对象,有谁知道如何获取下面代码中的类型(如果属性 myclass) .SomList
为空?
List<myclass> myList = dataGenerator.getMyClasses();
lbxObjects.ItemsSource = myList;
lbxObjects.SelectionChanged += lbxObjects_SelectionChanged;
private void lbxObjects_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Reflect();
}
Private void Reflect()
{
foreach (PropertyInfo pi in lbxObjects.SelectedItem.GetType().GetProperties())
{
switch (pi.PropertyType.Name.ToLower())
{
case "list`1":
{
// This works if the List<T> contains one or more elements.
Type tTemp = GetGenericType(pi.GetValue(lbxObjects.SelectedItem, null));
// but how is it possible to get the Type if the value is null?
// I need to be able to create a new object of the type the generic list expect.
// Type type = pi.getType?? // how to get the Type of the class inside List<T>?
break;
}
}
}
}
private Type GetGenericType(object obj)
{
if (obj != null)
{
Type t = obj.GetType();
if (t.IsGenericType)
{
Type[] at = t.GetGenericArguments();
t = at.First<Type>();
}
return t;
}
else
{
return null;
}
}
I'm working on a reflection project, and now I'm stuck.
If I have an object of myclass
that can hold a List<SomeClass>
, does anyone know how to get the type as in the code below if the property myclass.SomList
is empty?
List<myclass> myList = dataGenerator.getMyClasses();
lbxObjects.ItemsSource = myList;
lbxObjects.SelectionChanged += lbxObjects_SelectionChanged;
private void lbxObjects_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Reflect();
}
Private void Reflect()
{
foreach (PropertyInfo pi in lbxObjects.SelectedItem.GetType().GetProperties())
{
switch (pi.PropertyType.Name.ToLower())
{
case "list`1":
{
// This works if the List<T> contains one or more elements.
Type tTemp = GetGenericType(pi.GetValue(lbxObjects.SelectedItem, null));
// but how is it possible to get the Type if the value is null?
// I need to be able to create a new object of the type the generic list expect.
// Type type = pi.getType?? // how to get the Type of the class inside List<T>?
break;
}
}
}
}
private Type GetGenericType(object obj)
{
if (obj != null)
{
Type t = obj.GetType();
if (t.IsGenericType)
{
Type[] at = t.GetGenericArguments();
t = at.First<Type>();
}
return t;
}
else
{
return null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更一般地说,要支持任何
IList
,您需要检查接口:More generally, to support any
IList<T>
, you need to check the interfaces:给定一个我怀疑是某种
IList
的对象,我如何确定它是什么它是IList
?这是勇敢的解决方案。 它假设您有要测试的实际对象(而不是
Type
)。用法示例:
打印
Given an object which I suspect to be some kind of
IList<>
, how can I determine of what it's anIList<>
?Here's the gutsy solution. It assumes you have the actual object to test (rather than a
Type
).Example usage:
Prints
Marc 的答案是我为此使用的方法,但为了简单起见(以及更友好的 API?),如果您有以下属性,您可以在集合基类中定义一个属性:
我发现这种方法很有用,并且很容易理解任何仿制药新手。
Marc's answer is the approach I use for this, but for simplicity (and a friendlier API?) you can define a property in the collection base class if you have one such as:
I have found this approach useful, and is easy to understand for any newcomers to generics.
给定一个我怀疑是某种
IList
的对象,我如何确定它是什么它是IList
?这是一个可靠的解决方案。 对于篇幅,我深表歉意 - C# 的内省 API 让这变得异常困难。
用法示例:
返回
Given an object which I suspect to be some kind of
IList<>
, how can I determine of what it's anIList<>
?Here's a reliable solution. My apologies for length - C#'s introspection API makes this suprisingly difficult.
Example usage:
Returns