Jam*_*rtz 34 .net c# windows-8 windows-runtime .net-core
我正在整理一个与Stack API接口的应用程序,并且一直在关注本教程(虽然旧的API版本仍然有效).我的问题是,在Windows 8商店应用程序中使用它时,我受.NETCore Framework的限制,它不支持GetCustomAttributes下面找到的方法:
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;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是双重的.GetCustomAttributes在Windows 8 Store App领域的限制范围内,该方法究竟做了什么以及与此方法等效?
Mar*_*ell 65
你需要使用type.GetTypeInfo(),然后有各种GetCustomAttribute方法(通过扩展方法),或者有.CustomAttributes给你原始信息(而不是物化Attribute实例).
例如:
var attribute = type.GetTypeInfo().GetCustomAttribute<WrapperObjectAttribute>();
if(attribute == null)
{
...
}
...
Run Code Online (Sandbox Code Playgroud)
GetTypeInfo() 是图书馆作者的.NETCore的痛苦; p
如果.GetTypeInfo()没有出现,则添加using System.Reflection;指令.
| 归档时间: |
|
| 查看次数: |
13007 次 |
| 最近记录: |