.NETCore(Windows 8 框架)的“GetCustomAttributes"的等效方法是什么? [英] What is an equivalent method to `GetCustomAttributes` for .NETCore (Windows 8 Framework)?
问题描述
我正在构建一个与 Stack API 交互的应用程序,并且一直在关注 本教程(虽然旧的 API 版本仍然有效).我的问题是,在 Windows 8 Store 应用程序中使用它时,我受到 .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 Store 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
为您提供原始信息(而不是具体化的 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 框架)的“GetCustomAttributes"的等效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!