FormatterServices.GetSerializableMembers Método

Definição

Retorna todos os membros serializáveis para uma classe específica.Returns all the serializable members for a particular class.

Sobrecargas

GetSerializableMembers(Type)

Obtém todos os membros serializáveis para uma classe do Type especificado.Gets all the serializable members for a class of the specified Type.

GetSerializableMembers(Type, StreamingContext)

Obtém todos os membros serializáveis para uma classe do Type especificado e no StreamingContext fornecido.Gets all the serializable members for a class of the specified Type and in the provided StreamingContext.

GetSerializableMembers(Type)

Obtém todos os membros serializáveis para uma classe do Type especificado.Gets all the serializable members for a class of the specified Type.

public:
 static cli::array <System::Reflection::MemberInfo ^> ^ GetSerializableMembers(Type ^ type);
public static System.Reflection.MemberInfo[] GetSerializableMembers (Type type);
[System.Security.SecurityCritical]
public static System.Reflection.MemberInfo[] GetSerializableMembers (Type type);
static member GetSerializableMembers : Type -> System.Reflection.MemberInfo[]
[<System.Security.SecurityCritical>]
static member GetSerializableMembers : Type -> System.Reflection.MemberInfo[]
Public Shared Function GetSerializableMembers (type As Type) As MemberInfo()

Parâmetros

type
Type

O tipo que está sendo serializado.The type being serialized.

Retornos

MemberInfo[]

Uma matriz do tipo MemberInfo dos membros não transitórios e não estáticos.An array of type MemberInfo of the non-transient, non-static members.

Atributos

Exceções

O parâmetro type é null.The type parameter is null.

O chamador não tem a permissão necessária.The caller does not have the required permission.

Exemplos

O exemplo a seguir chama o GetSerializableMembers método de uma implementação do ISerializable.GetObjectData método.The following example calls the GetSerializableMembers method from an implementation of ISerializable.GetObjectData method.

public:
    [SecurityPermission(SecurityAction::Demand, SerializationFormatter = true)]
    virtual 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();
        array<MemberInfo^>^ serializableMembers =
            FormatterServices::GetSerializableMembers(thisType, context);

        // Serialize the base class's fields to the info object.
        for each (MemberInfo^ serializableMember in serializableMembers)
        {
            // Do not serialize fields for this class.
            if (serializableMember->DeclaringType != thisType)
            {
                // Skip this field if it is marked NonSerialized.
                if (!(Attribute::IsDefined(serializableMember,
                    NonSerializedAttribute::typeid)))
                {
                    // Get the value of this field and add it to the
                    // SerializationInfo object.
                    info->AddValue(serializableMember->Name,
                        ((FieldInfo^)serializableMember)->GetValue(this));
                }
            }
        }

        // Call the method below to see the contents of the
        // SerializationInfo object.
        DisplaySerializationInfo(info);
    }
[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);
}

Comentários

Em geral, os membros serializáveis de uma classe são membros não transitórios e não estáticos, como campos e propriedades.Generally, the serializable members of a class are non-transient, non-static members such as fields and properties. Para ser incluído, as propriedades devem ter um getter e um setter.To be included, properties must have both a getter and a setter. Uma classe que implementa a ISerializable interface ou tem um substituto de serialização não precisa serializar todos esses membros ou pode serializar membros adicionais.A class that implements the ISerializable interface or has a serialization surrogate does not have to serialize all these members, or can serialize additional members.

Como o GetSerializableMembers método chama o Type.GetFields método, ele não retorna campos em uma ordem específica, como ordem alfabética ou declaração.Because the GetSerializableMembers method calls the Type.GetFields method, it does not return fields in a particular order, such as alphabetical or declaration order. Seu código não deve depender da ordem na qual os campos são retornados.Your code must not depend on the order in which fields are returned.

Confira também

Aplica-se a

GetSerializableMembers(Type, StreamingContext)

Obtém todos os membros serializáveis para uma classe do Type especificado e no StreamingContext fornecido.Gets all the serializable members for a class of the specified Type and in the provided StreamingContext.

public:
 static cli::array <System::Reflection::MemberInfo ^> ^ GetSerializableMembers(Type ^ type, System::Runtime::Serialization::StreamingContext context);
public static System.Reflection.MemberInfo[] GetSerializableMembers (Type type, System.Runtime.Serialization.StreamingContext context);
[System.Security.SecurityCritical]
public static System.Reflection.MemberInfo[] GetSerializableMembers (Type type, System.Runtime.Serialization.StreamingContext context);
static member GetSerializableMembers : Type * System.Runtime.Serialization.StreamingContext -> System.Reflection.MemberInfo[]
[<System.Security.SecurityCritical>]
static member GetSerializableMembers : Type * System.Runtime.Serialization.StreamingContext -> System.Reflection.MemberInfo[]
Public Shared Function GetSerializableMembers (type As Type, context As StreamingContext) As MemberInfo()

Parâmetros

type
Type

O tipo sendo serializado ou clonado.The type being serialized or cloned.

context
StreamingContext

O contexto em que a serialização ocorre.The context where the serialization occurs.

Retornos

MemberInfo[]

Uma matriz do tipo MemberInfo dos membros não transitórios e não estáticos.An array of type MemberInfo of the non-transient, non-static members.

Atributos

Exceções

O parâmetro type é null.The type parameter is null.

O chamador não tem a permissão necessária.The caller does not have the required permission.

Comentários

Em geral, os membros serializáveis de uma classe são membros não transitórios e não estáticos, como campos e propriedades.Generally, the serializable members of a class are non-transient, non-static members such as fields and properties. Para ser incluído, as propriedades devem ter um getter e um setter.To be included, properties must have both a getter and a setter. Uma classe que implementa ISerializable ou tem um substituto de serialização não precisa serializar todos esses membros ou pode serializar membros adicionais.A class that implements ISerializable or has a serialization surrogate does not have to serialize all these members, or can serialize additional members.

Se context incluir o StreamingContextStates.Clone valor de enumeração, os campos transitórios também serão incluídos na MemberInfo matriz retornada por esse método.If context includes the StreamingContextStates.Clone enumeration value, transient fields are also included in the MemberInfo array returned by this method.

Como o GetSerializableMembers método chama o Type.GetFields método, ele não retorna campos em uma ordem específica, como ordem alfabética ou declaração.Because the GetSerializableMembers method calls the Type.GetFields method, it does not return fields in a particular order, such as alphabetical or declaration order. Seu código não deve depender da ordem na qual os campos são retornados.Your code must not depend on the order in which fields are returned.

Confira também

Aplica-se a