FormatterServices.GetSerializableMembers FormatterServices.GetSerializableMembers FormatterServices.GetSerializableMembers Method

Definition

Returns all the serializable members for a particular class.

Overloads

GetSerializableMembers(Type) GetSerializableMembers(Type) GetSerializableMembers(Type)

Gets all the serializable members for a class of the specified Type.

GetSerializableMembers(Type, StreamingContext) GetSerializableMembers(Type, StreamingContext) GetSerializableMembers(Type, StreamingContext)

Gets all the serializable members for a class of the specified Type and in the provided StreamingContext.

GetSerializableMembers(Type) GetSerializableMembers(Type) GetSerializableMembers(Type)

Gets all the serializable members for a class of the specified Type.

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

Parameters

type
Type Type Type

The type being serialized.

Returns

An array of type MemberInfo of the non-transient, non-static members.

Exceptions

The caller does not have the required permission.

Examples

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);
}

Remarks

Generally, the serializable members of a class are non-transient, non-static members such as fields and properties. To be included, properties must have both a getter and a setter. 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.

Because the GetSerializableMembers method calls the Type.GetFields method, it does not return fields in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which fields are returned.

Security

SecurityPermission
For providing serialization services. Associated enumeration: SerializationFormatter, LinkDemand.

See also

GetSerializableMembers(Type, StreamingContext) GetSerializableMembers(Type, StreamingContext) GetSerializableMembers(Type, StreamingContext)

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);
[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[]

Parameters

type
Type Type Type

The type being serialized or cloned.

context
StreamingContext StreamingContext StreamingContext

The context where the serialization occurs.

Returns

An array of type MemberInfo of the non-transient, non-static members.

Exceptions

The caller does not have the required permission.

Remarks

Generally, the serializable members of a class are non-transient, non-static members such as fields and properties. To be included, properties must have both a getter and a setter. A class that implements ISerializable or has a serialization surrogate does not have to serialize all these members, or can serialize additional members.

If context includes the StreamingContextStates.Clone enumeration value, transient fields are also included in the MemberInfo array returned by this method.

Because the GetSerializableMembers method calls the Type.GetFields method, it does not return fields in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which fields are returned.

Security

SecurityPermission
For providing serialization services. Associated enumeration: SerializationFormatter, LinkDemand.

See also

Applies to