DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 12.9 Finding All Serializable Types Within an Assembly

Problem

You need to find all the serializable types within an assembly.

Solution

Instead of testing the implemented interfaces and attributes on every type, you can query the Type.Attributes property to determine whether it contains the TypeAttributes.Serializable flag, as the following method does:

public static ArrayList GetSerializableTypeNames(string asmPath)
{
    ArrayList serializableTypes = new ArrayList( );
    Assembly asm = Assembly.LoadFrom(asmPath);

    // look at all types in the assembly
    foreach(Type type in asm.GetTypes( ))
    {
        if ((type.Attributes & TypeAttributes.Serializable) == 
            TypeAttributes.Serializable)
        {
            // add the name of the serializable type
            serializableTypes.Add(type.FullName);
        }
    }

    return (serializableTypes);
}

The GetSerializableTypeNames method accepts the path of an assembly through its asmPath parameter. This assembly is searched for any serializable types, and their full names (including namespaces) are returned in an ArrayList. Note that you can just as easily return an ArrayList containing the Type object for each serializable type by changing the line of code:

serializableTypes.Add(asmType.FullName);

to:

serializableTypes.Add(asmType);

In order to use this method to display the serializable types in an assembly, use the following:

public static void FindSerializable( )
{
    Process current = Process.GetCurrentProcess( );
    // get the path of the current module
    string asmPath = current.MainModule.FileName;
    ArrayList serializable = GetSerializableTypeNames(asmPath);
    // write out the serializable types in the assembly
    if(serializable.Count > 0)
    {
        Console.WriteLine("{0} has serializable types:",asmPath);
        for(int i=0;i<serializable.Count;i++)
        {
            Console.WriteLine("\t{0}",serializable[i]);
        }
    }
}

Discussion

A type may be marked as serializable in one of two different ways:

  • The SerializableAttribute attribute can be added to the type.

  • The type can be marked with the SerializableAttribute and/or implement the ISerializable interface.

Testing for either the SerializableAttribute attribute or the ISerializable interface on a type can turn into a fair amount of work. Fortunately, we do not have to do all of this work; it has been done for us. The Attributes enumeration on the Type class contains several flags that describe the current type, one of which is the Serializable flag. This flag is set when either the SerializableAttribute or the ISerializable interface, or both, are added to a type.

To test for this flag, use the following logic:

(asmType.Attributes & TypeAttributes.Serializable) == TypeAttributes.Serializable

If the Serializable flag is set, this expression will evaluate to true.

See Also

See the "Assembly Class" and "TypeAttributes Enumeration" in the MSDN documentation.

    [ Team LiB ] Previous Section Next Section