C#反思 - 找到一个集合的泛型类型 [英] c# Reflection - Find the Generic Type of a Collection

查看:94
本文介绍了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());

这篇关于C#反思 - 找到一个集合的泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆