當前位置: 首頁>>代碼示例>>C#>>正文


C# SerializationInfoEnumerator類代碼示例

本文整理匯總了C#中System.Runtime.Serialization.SerializationInfoEnumerator的典型用法代碼示例。如果您正苦於以下問題:C# SerializationInfoEnumerator類的具體用法?C# SerializationInfoEnumerator怎麽用?C# SerializationInfoEnumerator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


SerializationInfoEnumerator類屬於System.Runtime.Serialization命名空間,在下文中一共展示了SerializationInfoEnumerator類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Person

//引入命名空間
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Security.Permissions;

// Person is a serializable base class.
[Serializable]
public class Person
{
    private String title;

    public Person(String title)
    {
        this.title = title;
    }

    public override String ToString()
    {
        return String.Format("{0}", title);
    }
}

// Employee is a serializable class derived from Person.
[Serializable]
public class Employee : Person
{
    private String title;

    public Employee(String title) : base("Person")
    {
        this.title = title;
    }

    public override String ToString()
    {
        return String.Format("{0} -> {1}", title, base.ToString());
    }
}

// Manager is a serializable and ISerializable class derived from Employee.
[Serializable]
public class Manager : Employee, ISerializable
{
    private String title;

    public Manager() : base("Employee") 
    {
        this.title = "Manager";
    }

    [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {

        // Serialize the desired values for this class.
        info.AddValue("title", title);

        // Get the set of serializable members for the class and base classes.
        Type thisType = this.GetType();
        MemberInfo[] mi = FormatterServices.GetSerializableMembers(thisType, context);

        // Serialize the base class's fields to the info object.
        for (Int32 i = 0; i < mi.Length; i++)
        {
            // Do not serialize fields for this class.
            if (mi[i].DeclaringType == thisType) continue;

            // Skip this field if it is marked NonSerialized.
            if (Attribute.IsDefined(mi[i], typeof(NonSerializedAttribute))) continue;
         
            // Get the value of this field and add it to the SerializationInfo object.
            info.AddValue(mi[i].Name, ((FieldInfo) mi[i]).GetValue(this));
        }

        // Call the method below to see the contents of the SerializationInfo object.
        DisplaySerializationInfo(info);
    }
 
    private void DisplaySerializationInfo(SerializationInfo info)
    {
        SerializationInfoEnumerator e = info.GetEnumerator();
        Console.WriteLine("Values in the SerializationInfo:");
        while (e.MoveNext())
        {
            Console.WriteLine("Name={0}, ObjectType={1}, Value={2}", e.Name, e.ObjectType, e.Value);
        }
    }

    [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.SerializationFormatter)]
    protected Manager(SerializationInfo info, StreamingContext context) : base(null)
    {

        // Get the set of serializable members for the class and base classes.
        Type thisType = this.GetType();
        MemberInfo[] mi = FormatterServices.GetSerializableMembers(thisType, context);

        // Deserialize the base class's fields from the info object.
        for (Int32 i = 0; i < mi.Length; i++)
        {
            // Do not deserialize fields for this class.
            if (mi[i].DeclaringType == thisType) continue;

            // For easier coding, treat the member as a FieldInfo object
            FieldInfo fi = (FieldInfo) mi[i];

            // Skip this field if it is marked NonSerialized.
            if (Attribute.IsDefined(mi[i], typeof(NonSerializedAttribute))) continue;

            // Get the value of this field from the SerializationInfo object.
            fi.SetValue(this, info.GetValue(fi.Name, fi.FieldType));
        }

        // Deserialize the values that were serialized for this class.
        title = info.GetString("title");
    }

    public override String ToString()
    {
        return String.Format("{0} -> {1}", title, base.ToString());
    }
}

public sealed class App
{
    public static void Main()
    {
        Run();
    }

    public static void Run()
    {
        using (Stream stream = new MemoryStream())
    {
            IFormatter formatter = new BinaryFormatter();
            Manager m = new Manager();
            Console.WriteLine(m.ToString());
            formatter.Serialize(stream, m);

            stream.Position = 0;
            m = (Manager) formatter.Deserialize(stream);
            Console.WriteLine(m.ToString());
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.Runtime.Serialization,代碼行數:148,代碼來源:SerializationInfoEnumerator

輸出:

Manager -> Employee -> Person
Values in the SerializaitonInfo:
Name=title, ObjectType=System.String, Value=Manager
Name=Employee+title, ObjectType=System.String, Value=Employee
Name=Person+title, ObjectType=System.String, Value=Person
Manager -> Employee -> Person


注:本文中的System.Runtime.Serialization.SerializationInfoEnumerator類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。