Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C:[非序列化]在实现ISerializable时_C#_Serialization_Iserializable - Fatal编程技术网

C# C:[非序列化]在实现ISerializable时

C# C:[非序列化]在实现ISerializable时,c#,serialization,iserializable,C#,Serialization,Iserializable,我不理解在类上实现ISerializable时[NonSerialized]属性的用法。我参加了“C#编程”(Microsoft 20-483)课程,该课程仅在少数示例中使用,但没有详细介绍。 参加本课程: [可序列化] 公共类TestNonSerializable:ISerializable { 公共字符串名{get;set;} 公共字符串LastName{get;set;} [非串行化] 私人互联网; 公共信息 { 获取{返回此。_Age;} 设置{this.\u Age=value;} }

我不理解在类上实现
ISerializable
[NonSerialized]
属性的用法。我参加了“C#编程”(Microsoft 20-483)课程,该课程仅在少数示例中使用,但没有详细介绍。
参加本课程:

[可序列化]
公共类TestNonSerializable:ISerializable
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
[非串行化]
私人互联网;
公共信息
{
获取{返回此。_Age;}
设置{this.\u Age=value;}
}
公共TestNonSerializable()
{ }
公共TestNonSerializable(SerializationInfo信息、StreamingContext上下文)
{
FirstName=info.GetValue(“Name”,typeof(string))作为字符串;
LastName=info.GetValue(“LastName”,typeof(string))作为字符串;
//我希望这会引发异常,因为该值不存在。
//但它确实存在!
年龄=(int)info.GetValue(“年龄”,typeof(int));
}
public void GetObjectData(SerializationInfo信息、StreamingContext上下文)
{
info.AddValue(“名称”,FirstName);
info.AddValue(“LastName”,LastName);
//我希望这是空的
信息增值(“年龄”,年龄);
}
}
我评论了我所期望的:
\u Age
是一个我不想序列化的私有字段。我专门编写了
GetObjectData
来序列化它。这是一件很奇怪的事情,但我想了解如何处理
[非序列化]

如果我在
主程序中运行类似的操作

时代在那里。。。在序列化对象所在的文件和重新水化对象中。我的问题是:为什么?
[NonSerialized]
属性,因为我们不必在
GetObjectData
方法中添加
Age
? 很明显我遗漏了什么,但我不知道是什么。 谢谢

编辑:课程中的示例:


好的,我在微软网站上发现了一些有趣的东西:

NonSerializedAttribute属性的目标对象是可序列化类的公共和私有字段。默认情况下,类不可序列化,除非它们用SerializableAttribute标记。在序列化过程中,默认情况下会序列化类的所有公共和私有字段。在序列化过程中,将排除标有NonSerializedAttribute的字段。如果使用XmlSerializer类序列化对象,请使用XmlIgnoreAttribute类获得相同的功能或者,实现ISerializable接口以显式控制序列化过程。注意,实现ISerializable的类仍然必须用SerializableAttribute标记


所以,基本上,这就是为什么我在实现
ISerializable
时不理解
[NonSerialized]
的用法:它们不应该一起工作。

是因为您将此属性应用于私有字段,而序列化程序实际上是序列化属性吗?它不会阻止通过在备份文件上应用[NonSerialized]来序列化属性。也请检查谢谢,但链接并没有真正帮助我:(我在微软网站上找到了答案,我想。。。
class Program
{
    static void Main(string[] args)
    {
        var myObject = new TestNonSerializable()
        {
            FirstName = "Foo",
            LastName = "Bar",
            Age = 32,
        };

        // Instanciate a SOAP formatter
        IFormatter soapFormat = new SoapFormatter();

        // Serialize to a file
        using (FileStream buffer = File.Create(@"D:\temp\TestNonSerializable.txt"))
        {
            // In the file generated, I expect the age to be empty. But the value
            // is set to 32
            soapFormat.Serialize(buffer, myObject);
        }

        // Deserialize from a file
        using (FileStream buffer = File.OpenRead(@"D:\temp\TestNonSerializable.txt"))
        {
            // The age is being deserialized
            var hydratedObject = soapFormat.Deserialize(buffer);
        }
    }
}
[Serializable]
public class ServiceConfiguration : ISerializable
{
    [NonSerialized]
    private Guid _internalId;
    public string ConfigName { get; set; }
    public string DatabaseHostName { get; set; }
    public string ApplicationDataPath { get; set; }
    public ServiceConfiguration()
    {
    }
    public ServiceConfiguration(SerializationInfo info, StreamingContext ctxt)
    {
        this.ConfigName
           = info.GetValue("ConfigName", typeof(string)).ToString();
        this.DatabaseHostName
           = info.GetValue("DatabaseHostName", typeof(string)).ToString();
        this.ApplicationDataPath
           = info.GetValue("ApplicationDataPath", typeof(string)).ToString();
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("ConfigName", this.ConfigName);
        info.AddValue("DatabaseHostName", this.DatabaseHostName);
        info.AddValue("ApplicationDataPath", this.ApplicationDataPath);
    }
}