C#反思 - 找到一个集合的泛型类型
问题描述:
我反映一个属性'胡说'类型为ICollection的
I'm reflecting a property 'Blah' its Type is ICollection
public ICollection<string> Blah { get; set; }
private void button1_Click(object sender, RoutedEventArgs e)
{
var pi = GetType().GetProperty("Blah");
MessageBox.Show(pi.PropertyType.ToString());
}
这给了我(你会期望!)的ICollection<串>
...
This gives me (as you'd expect!) ICollection<string>
...
但我真的想要得到的集合类型,即的ICollection
(而不是的ICollection<串>
) - ?没有人知道如何我会做到这一点,请
But really I want to get the collection type i.e. ICollection
(rather than ICollection<string>
) - does anyone know how i'd do this please?
答
您正在寻找的 GetGenericTypeDefinition
方式:
You're looking for the GetGenericTypeDefinition
method:
MessageBox.Show(pi.PropertyType.GetGenericTypeDefinition().ToString());