Recipe13.9.Finding All Serializable Types Within an Assembly


Recipe 13.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.IsSerialized property to determine whether it is marked as serializable, as the following method does:

 public static Type[] GetSerializableTypes(Assembly asm) {     List<Type> serializableTypes = new List<Type>();     // Look at all types in the assembly.     foreach(Type type in asm.GetTypes())     {         if (type.IsSerializable)         {             // Add the name of the serializable type.             serializableTypes.Add(type);         }     }     return (serializableTypes.ToArray()); } 

The GetSerializableTypes method accepts an Assembly through its asm parameter. This assembly is searched for any serializable types, and their full names (including namespaces) are returned in a Type[].

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

 public static void FindSerializable() {     Assembly asm = Assembly.GetExecutingAssembly();     Type[] serializable = GetSerializableTypes(asm);     // Write out the serializable types in the assembly.     if(serializable.Length > 0)     {         Console.WriteLine("{0} has serializable types:",asm.Location);         foreach (Type t in serializable)         {             Console.WriteLine("\t{0}", t.FullName);         }     } } 

The output of this method is shown here:

 C:\CSharp Recipes 2nd Edition\Code\CSharpRecipes\bin\Debug\CSharpRecipes.exe has serializable types:         CSharpRecipes.ExceptionHandling+RemoteComponentException         CSharpRecipes.DelegatesEventsAnonymousMethods+HashtableEventHandler         CSharpRecipes.Collections+MaxMinSizeDictionary`2         CSharpRecipes.Collections+MaxMinValueHashtable         CSharpRecipes.DataStructsAndAlgorithms+DblQueue`1         CSharpRecipes.ClassAndStructs+DeepClone         CSharpRecipes.ClassAndStructs+MultiClone         CSharpRecipes.ClassAndStructs+Serializer`1 

Discussion

A type may be marked as serializable using the SerializableAttribute attribute. Testing for the SerializableAttribute attribute on a type can turn into a fair amount of work. This is because the SerializableAttribute is a magic attribute that the C# compiler actually strips off your code at compile time. Using ildasm you will see that this custom attribute just isn't therenormally you see a .custom enTRy for each custom attribute, but not with SerializableAttribute. The C# compiler removes it, and instead sets a flag in the metadata of the class. In source code, it looks like a custom attribute, but it compiles into one of a small set of attributes that gets a special representation in metadata. That's why it gets special treatment in the reflection APIs. Fortunately, you do not have to do all of this work. The IsSerializable property on the Type class returns a true if the current type is marked as serializable with the SerializableAttribute; otherwise, this property returns false.

See Also

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



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net