什么是等效的方法`GetCustomAttributes`的.NETCore(Windows 8的框架)? [英] What is an equivalent method to `GetCustomAttributes` for .NETCore (Windows 8 Framework)?

查看:1484
本文介绍了什么是等效的方法`GetCustomAttributes`的.NETCore(Windows 8的框架)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我组建一个应用程序接口与协议栈的API,并一直在关注的本教程(虽然旧的API版本,它仍然有效)。我的问题是,当Windows 8 Store应用中使用此我被.NETCore框架contrained不支持 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的约束范围内商店App境界?

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 它给你的原始信息(而非物化属性实例)。

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()不出现,然后添加一个使用的System.Reflection; 指令

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

这篇关于什么是等效的方法`GetCustomAttributes`的.NETCore(Windows 8的框架)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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