Tag: 序列化

EF中的可序列化类和动态代理 – 如何?

在[之前的post]中 ,我被设置为必须克隆我的实体的路径。 我尝试使用[codeproject]中的序列化方法。 因为这些类是由Entity Framework生成的,所以我在自定义.cs中单独标记它们,如下所示: [Serializable] public partial class Claims { } 但是,当检查时(在克隆方法中): if (Object.ReferenceEquals(source, null)) { 被击中,我收到错误: System.ArgumentException was unhandled by user code Message=The type must be serializable. Parameter name: source Source=Web ParamName=source StackTrace: at .Web.Cloner.Clone[T](T source) in C:\Users\.\Documents\Visual Studio 2010\Projects\.\Website\Extensions.Object.cs:line 49 at .Web.Models.Employer..ctor(User u) in C:\Users\.\Documents\Visual Studio 2010\Projects\.\Website\Models\EF.Custom.cs:line 121 at .Web.Controllers.AuthController.Register(String Company, String […]

文件存在时FileMode.Open和FileMode.OpenOrCreate的区别? c#bug?

我写了那段代码: public void Save() { using (FileStream fs = new FileStream(Properties.Settings.Default.settings_file_path, FileMode.Open)) { XmlSerializer ser = new XmlSerializer(typeof(MySettings)); ser.Serialize(fs, this); } } 当我使用FileMode.Open一切都很好,输出是这样的: 12 A0 MEASUREMENT 5000 C0 MEASUREMENT 但当我将其更改为FileMode.OpenOrCreate输出将更改为: 12 A0 MEASUREMENT 5000 C0 MEASUREMENT > 是什么让整个xml文件因为最后添加>符号而损坏。 这是可解释的还是它的c#bug?

.NET 4.5中的序列化中断

我们有一个只在.NET 4.5中发生的序列化问题 – 相同的代码在.NET 4中工作正常。我们试图用几个字段序列化一个inheritance的类型,基类和inheritance类都用SerializableAttribute标记。 我们在Web服务的客户端获得一个exception,说服务器中有一个MethodAccessException ,服务器本身不会抛出任何exception,这似乎是客户端序列化过程中的一个问题。 重要的是要注意我们在.NET 4-而不是.4.5中进行编译 更新:实现ISerailize并忽略“Value”属性后,程序运行正常,但这意味着我们不得不放弃序列化此字段。 非常感激任何的帮助。 谢谢,奥梅尔 例外细节: System.Web.Services.Protocols.SoapException occurred HResult=-2146233087 Message=System.Web.Services.Protocols.SoapException: Server was unable to process request. —> System.InvalidOperationException: There was an error generating the XML document. —> System.MethodAccessException: Attempt by method ‘Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write88_DeviceSiteTypeInfo(System.String, System.String, IOSIGHT.Info.DeviceSiteTypeInfo, Boolean, Boolean)’ to access method ‘IOSIGHT.Info.DeviceSiteTypeInfo.get_Value()’ failed. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write88_DeviceSiteTypeInfo(String n, String ns, DeviceSiteTypeInfo o, […]

无法将JSON数组反序列化为类型–Json.NET

我试图将json数据反序列化为模型类,但我失败了。 这是我做的: public CountryModel GetCountries() { using (WebClient client = new WebClient()) { var result = client.DownloadString(“http://api.worldbank.org/incomeLevels/LIC/countries?format=json”); var output = JsonConvert.DeserializeObject<List>(result); return output.First(); } } 这就是我的模型的样子: public class CountryModel { public int Page { get; set; } public int Pages { get; set; } public int Per_Page { get; set; } public int Total { […]

将特定枚举反序列化为Json.Net中的system.enum

我有一个相当通用的“规则”类,我用它来驱动我正在编写的分析引擎的行为: public class Rule { /// /// The general rule type. /// public RuleType RuleType { get; set; } /// /// The human-readable description of the rule. /// public string RuleDescription { get; set; } /// /// The integer magnitude of the rule, if applicable. /// public int? RuleInt { get; set; } /// /// […]

ef4导致Web服务中的循环引用

我有一个Reason对象: public class Reason { public virtual long Id { get; set; } public virtual string Name { get; set; } public virtual Company Company {get;set;} } 我使用entity framework4,公司是公司的导航属性。 我还使用webservices将数据返回给客户端。 我有web方法返回原因: [WebMethod] public Reason[] GetCallReasons() { IReasonRepository rep = ObjectFactory.GetInstance(); return rep.GetReasonsList().ToArray(); } 由于ef4,我在执行web方法时遇到以下exception: A circular reference was detected while serializing an object of type […]

如何通过XML序列化来了解何时加载?

我正在尝试通过XML序列化加载一个对象树,此时它将加载对象,并非常愉快地创建树。 我的问题围绕着这些类支持一定级别的审计这一事实。 我希望能够做的是在每个对象加载完成后调用一些方法。 为了论证,假设我有一个相当通用的对象树,在不同的级别有不同的类,如: 123 Any Street 456 High Street 有没有办法使用默认的序列化器(以类似的方式创建像ShouldSerializeFoo这样的方法)来确定每个对象的加载何时完成? 编辑:我应该指出,暴露类似于反序列化后我可以调用的OnLoaded()方法的明显案例,让我觉得这是一件“坏事”。 Edit2:为了讨论起见,这是我当前的hack “方法”,它适用于基本级别,但子City节点仍然认为需要保存更改(在现实世界中,对象模型要复杂得多) ,但这至少会编译,而不需要完整的源代码) public class Office { [XmlAttribute(“IsHq”)] public bool IsHeadquarters { get; set; } [XmlElement] public string Street { get; set; } [XmlElement] public Town Town { get; set; } protected virtual void OnLoaded() {} public static OfficeCollection Search() { OfficeCollection retval […]

使用时,JSON.Net抛出StackOverflowException

我将这个简单的代码编写为Serialize类为flatten,但是当我使用[JsonConverter(typeof(FJson))]注释时,它会抛出StackOverflowException 。 如果我手动调用SerializeObject ,它可以正常工作。 如何在注释模式下使用JsonConvert: class Program { static void Main(string[] args) { A a = new A(); a.id = 1; abname = “value”; string json = null; // json = JsonConvert.SerializeObject(a, new FJson()); without [JsonConverter(typeof(FJson))] annotation workd fine // json = JsonConvert.SerializeObject(a); StackOverflowException Console.WriteLine(json); Console.ReadLine(); } } //[JsonConverter(typeof(FJson))] StackOverflowException public class A { public […]

如何在序列化json时忽略JsonProperty(PropertyName =“someName”)?

我有一些使用ASP.Net MVC的C#代码,它使用Json.Net来序列化一些DTO。 为了减少有效负载,我使用[JsonProperty(PropertyName =“shortName”)]属性在序列化期间使用较短的属性名称。 当客户端是另一个.Net应用程序或服务时,这非常有用,因为反序列化将对象层次结构重新组合在一起,使用更长的更友好的名称,同时保持实际的传输负载低。 当客户端通过浏览器javascript / ajax时,问题就出现了。 它发出请求,并获取json …但是json正在使用缩短的不太友好的名称。 如何让json.net序列化引擎以编程方式忽略[JsonProperty(PropertyName =“shortName”)]属性? 理想情况下,我的MVC服务将在那里运行,并且通常使用缩短的属性名称进行序列化。 当我的代码检测到特定参数时,我想使用较长的名称序列化数据并忽略[JsonProperty()]属性。 有什么建议? 谢谢, 凯文

如何将XML映射到C#对象

我有一个XML,我想加载到对象,操纵这些对象(设置值,读取值),然后保存这些XML。 对我来说,在我创建的结构(xsd)中包含XML非常重要。 一种方法是编写我自己的序列化程序,但它是否内置支持它或C#中的开源我可以使用?