What is an equivalent method to `GetCustomAttributes` for .NETCore (Windows 8 Framework)?(.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"的等效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:.NETCore(Windows 8 Framework)的“GetCustomAttributes"的等效方法是什么?


基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01