【问题标题】:How to get IEnumerable Type of Type [duplicate]如何获取类型的 IEnumerable 类型 [重复]
【发布时间】:2018-11-27 19:13:49
【问题描述】:

我有工厂方法,它返回给定密钥的 POCO 的 Type

    public static Type GetType(string key)
    {
        var name = string.format("MyPOCOClass{0},MyAssembly",key);
        return Type.GetType(name);
    }

假设 Key 的值为 One 并假设 MyPOCOClassOne 类存在于 MyAssembly 中

在上述情况下,我想要IEnumerable<MyPOCOClassOne> 的类型 我该怎么做?

【问题讨论】:

标签: c# .net


【解决方案1】:

您想利用Type.MakeGenericType(MSDN)。在你的情况下:

Type enumerable = typeof(IEnumerable<>); //Unresolved generic!
Type yourType = GetType(someKey); //OP Method

return enumerable.MakeGenericType(yourType);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-20
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多