【问题标题】:Type.GetInterfaces() workaround for .NET Standard 1.x.NET Standard 1.x 的 Type.GetInterfaces() 解决方法
【发布时间】:2017-05-25 22:59:31
【问题描述】:

.NET Core 1.1 支持Type.GetInterfaces() 方法,该方法提供在给定类型上实现的接口列表。不幸的是,Type.GetInterfaces().NET Standard 1.x 中尚不可用。

好消息是它应该包含在.NET Standard 2.0 中。

与此同时,有没有人知道我可以用来获取类型上的接口列表和/或实现.NET Standard 1.x 中给定接口的类列表的解决方法?

非常感谢!

【问题讨论】:

    标签: c# reflection .net-core asp.net-core-1.0 .net-standard


    【解决方案1】:

    这应该可以解决问题。 GetTypeInfo()System.Reflection 命名空间中的扩展方法,是InstrospectionExtensions 的一部分。

    using System.Reflection;
    var interfaces = typeof({SOME_TYPE}).GetTypeInfo().GetInterfaces();
    

    【讨论】:

    • 我相信 GetInterfaces() 没有在 .NET Standard 1.x 中实现。我的理解有误吗?
    • @AnthonyGatlin Type.GetInterfaces() 不是,但 TypeInfo.GetInterfaces() 是。该代码只是使用扩展方法访问TypeTypeInfo 使用GetTypeInfo() 扩展方法。这适用于我使用 .NET Standard 1.6 库和 .NETCoreApp1.1 控制台应用程序。如果它不适合你,请告诉我。
    • 罗伯托,这就像一个魅力。非常感谢你! :)
    • 请注意,通过安装 System.Reflection.TypeExtensions NuGet Package,这些扩展可用于 .NET Standard 1.x。为我开发 .NET Standard 1.3。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多