FormatterServices::GetSerializableMembers Method (Type^)

 

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

Namespace:   System.Runtime.Serialization
Assembly:  mscorlib (in mscorlib.dll)

public:
[SecurityCriticalAttribute]
static array<MemberInfo^>^ GetSerializableMembers(
	Type^ type
)

Parameters

type
Type: System::Type^

The type being serialized.

Return Value

Type: array<System.Reflection::MemberInfo^>^

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

Exception Condition
ArgumentNullException

The type parameter is null.

SecurityException

The caller does not have the required permission.

Generally, the serializable members of a class are the 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.

The following code example demonstrates implementing the GetSerializableMembers method. This code example is part of a larger example provided for the FormatterServices class.

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

For providing serialization services. Associated enumeration: SecurityPermissionFlag::SerializationFormatter, SecurityAction::LinkDemand.

.NET Framework
Available since 1.1
Return to top
Show: