.NETCore(Windows 8 Framework)的“GetCustomAttributes"的等效方法是什么?

What is an equivalent method to `GetCustomAttributes` for .NETCore (Windows 8 Framework)?(.NETCore(Windows 8 Framework)的“GetCustomAttributes的等效方法是什么?)
本文介绍了.NETCore(Windows 8 Framework)的“GetCustomAttributes"的等效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个与 Stack API 接口的应用程序,并且一直在关注 本教程 (尽管旧 API 版本仍然有效).我的问题是,在 Windows 8 商店应用程序中使用它时,我受到 .NETCore 框架的限制,它不支持下面的 GetCustomAttributes 方法:

I'm putting together an app that interfaces with Stack API and have been following this tutorial (although old API version it still works). My problem is that when using this within the Windows 8 Store App I'm contrained by the .NETCore Framework which doesn't support the GetCustomAttributes method found below:

    private static IEnumerable<T> ParseJson<T>(string json) where T : class, new()
    {
        var type = typeof (T);
        var attribute = type.GetCustomAttributes(typeof (WrapperObjectAttribute), false).SingleOrDefault() as WrapperObjectAttribute;
        if (attribute == null)
        {
            throw new InvalidOperationException(
                String.Format("{0} type must be decorated with a WrapperObjectAttribute.", type.Name));
        }

        var jobject = JObject.Parse(json);
        var collection = JsonConvert.DeserializeObject<List<T>>(jobject[attribute.WrapperObject].ToString());
        return collection;
    }

我的问题有两个.GetCustomAttributes 究竟做了什么,在 Windows 8 应用商店应用领域的约束中是否有与此方法等效的方法?

My question is two-fold. What exactly does the GetCustomAttributes do and is there an equivalent to this method within the constrains of Windows 8 Store App realm?

推荐答案

你需要使用type.GetTypeInfo(),然后有各种GetCustomAttribute方法(通过扩展方法),或者有 .CustomAttributes 可以为您提供原始信息(而不是具体化的 Attribute 实例).

You need to use type.GetTypeInfo(), which then has various GetCustomAttribute methods (via extension methods), or there is .CustomAttributes which gives you the raw information (rather than materialized Attribute instances).

例如:

var attribute = type.GetTypeInfo().GetCustomAttribute<WrapperObjectAttribute>();
if(attribute == null)
{
    ...
}
...

GetTypeInfo() 对于库作者来说是 .NETCore 的痛点;p

GetTypeInfo() is the pain of .NETCore for library authors ;p

如果 .GetTypeInfo() 没有出现,则添加 using System.Reflection; 指令.

If .GetTypeInfo() doesn't appear, then add a using System.Reflection; directive.

这篇关于.NETCore(Windows 8 Framework)的“GetCustomAttributes"的等效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)