【问题标题】:typeof(TModels) returns RuntimeTypetypeof(TModels) 返回 RuntimeType
【发布时间】:2012-05-14 12:13:46
【问题描述】:

我需要一种获取对象或类(在本例中为泛型类)的“真实类型”的方法。现在我得到的只是一个 RuntimeType。我已经阅读了整个互联网,但是我只是找不到不只是返回 RuntimeType 的答案..

【问题讨论】:

  • 为什么RuntimeType 不适合你?你认为“true-type”和RuntimeType之间到底有什么区别? RuntimeType“真正的类型”。

标签: .net generics reflection types system.type


【解决方案1】:

System.RuntimeType 是实现抽象System.Type 类的内部具体类。请参阅 the accepted answerWhat's the difference between System.Type and System.RuntimeType in C#? 进行良好的讨论。所以,你看,正如 svick 在他的评论中所说,RuntimeType 真正的类型。

您找不到不返回RuntimeType 对象的方法,因为GetType() 总是返回RuntimeType 对象。您将永远无法获得不是某个派生类的实例的Type 对象,因为Type 是抽象的。

【讨论】:

    【解决方案2】:

    propertyinfo.PropertyType.GetGenericArguments() 应该给你一个泛型参数的列表。
    这是一个列表,因为一个类可以有超过 1 个泛型。
    更多信息:
    http://msdn.microsoft.com/en-us/library/ms173128.aspx
    和这里
    http://msdn.microsoft.com/en-us/library/b8ytshk6.aspx

    【讨论】:

    • 我真的不确定这是什么问题。
    猜你喜欢
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2014-09-22
    • 2011-09-23
    • 1970-01-01
    • 2011-05-22
    相关资源
    最近更新 更多