【发布时间】:2013-11-18 18:27:39
【问题描述】:
我创建了一个名为DataContracts 的可移植类库,其中包含我的项目Messages 和Views。 GetStockItemByIDRequest 和 StockView 等标准内容都包含在其中。
问题在于,当我尝试使用System.ComponentModel.DataAnnotations 为我的一些Views 添加DataAnnotations 时。
[DataContract]
public class StockView
{
[Required]
[DataMember]
public Guid StockID { get; set; }
[Required]
[DataMember]
public string Name { get; set; }
}
我可以成功地将System.ComponentModel.DataAnnotations 添加到我的可移植类库项目中,并且可以在我的 Windows Phone 8 应用程序中成功引用它,甚至可以在我的 Windows Phone 应用程序中创建我的视图的新实例,例如 StockView View = new StockView(); 但是如果我尝试使用Newtonsoft.Json 或System.Net.Http.HttpClient 来做类似的事情
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://myservice.com");
T result = await response.Content.ReadAsAsync<T>();
或
T result = await Newtonsoft.Json.JsonConvert.DeserializeObjectAsync<T>("{}");
即:涉及反序列化的地方...
我遇到了错误Could not load file or assembly 'System.ComponentModel.DataAnnotations, Version=2.0.5.0'。我认为这是因为它似乎没有 System.ComponentModel.DataAnnotations is supported in Windows Phone 8 (但是为什么我可以将它添加为我的 PCL 的引用?)。
所以我的问题是,为什么当我直接创建这些类的新实例时没有调用这个错误,其次我该如何解决这个问题?
【问题讨论】:
-
自定义属性不在创建新类实例的过程中。
-
还不错,但是避免这个问题的正确方法是什么。肯定其他人已经尝试过创建跨项目兼容的便携式类库吗?
标签: c# asp.net-mvc windows-phone-8 data-annotations portable-class-library