English 中文(简体)
如何防止在序列化后更改序列
原标题:How to prevent changes in serialized class after serialization

我有这一类:

[Serializable]
public sealed class Broker
{
    public  int Id;
    public  string Name;
    public  string Hosts;
    public  string DefaultValidatorsNameSpace;
    public  string DefaultRendererNameSpace;
    public  bool IsDefault;
    public  CrmCredentials CrmCredentials;
}

当系统通过XmlSerializer从Xml文档中装载时,这一类别正在被淡化。

我不想让任何方案家在装满后改变物体的内容。 一种办法是,通过增加<条码>的公开性;对每个项目的私人编号;

问题回答

You should use DataContractSerializer to serialize your class, because it doesn t limit the serialization to public properties only.

另外,你不需要具体说明<代码>[可扩展]XML序列化特性。

YAXLib是一个XML-serialization的图书馆,请您将任何预期领域按序排列。 你们不需要把你需要加以序列化的领域曝光到公众面前,你只需要制定将田地序列化的备选办法。 这就是:

[YAXSerializableType(FieldsToSerialize=YAXSerializationFields.AttributedFieldsOnly)]
public sealed class Broker
{
    [YAXSerializableField]
    public  int Id { get; private set; }

    [YAXSerializableField]
    public  string Name { get; private set; }

    // or equaly give attribute to a private field
    [YAXSerializableField]
    private string _hosts;

    // 以及 leave the property un-attributed
    public string Hosts { get { return _hosts; } }

}

For more information see:

http://yaxlib.codeplex.com

以及

http://www.codeproject.com/KB/XML/yaxlib.aspx





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签