How to find Serializable attribute with reflection?
-
I have a class with a "Tag" property, similar to a TreeNode - a blank object that can be set to anything, in order to extend the base instance in whatever way an end user wishes. Because my class is serializable, I want to place a restriction on the object that can be set to the "Tag", to make sure it is serializable as well. I tried something similar to the following code, but it does not work:
object[] attributes = value.GetType().GetCustomAttributes(typeof(SerializableAttribute), true);
if (attributes.Length == 0)
throw new ArgumentException("All extender objects must be serializable.", "value");
...In fact, if I do the following on any class I've defined with the Serializable attribute, the serializable attribute is never displayed (but others are):
foreach (Attribute att in value.GetType().GetCustomAttributes(true))
System.Diagnostics.Trace.WriteLine(att.ToString());As far as I've seen so far, the Serializable attribute is the only one this happens to (might be more, but this is the only one I've run into so far), so is there another way to determine if this attribute is defined for a class? ----- In the land of the blind, the one eyed man is king. -- modified at 13:19 Monday 13th March, 2006
-
I have a class with a "Tag" property, similar to a TreeNode - a blank object that can be set to anything, in order to extend the base instance in whatever way an end user wishes. Because my class is serializable, I want to place a restriction on the object that can be set to the "Tag", to make sure it is serializable as well. I tried something similar to the following code, but it does not work:
object[] attributes = value.GetType().GetCustomAttributes(typeof(SerializableAttribute), true);
if (attributes.Length == 0)
throw new ArgumentException("All extender objects must be serializable.", "value");
...In fact, if I do the following on any class I've defined with the Serializable attribute, the serializable attribute is never displayed (but others are):
foreach (Attribute att in value.GetType().GetCustomAttributes(true))
System.Diagnostics.Trace.WriteLine(att.ToString());As far as I've seen so far, the Serializable attribute is the only one this happens to (might be more, but this is the only one I've run into so far), so is there another way to determine if this attribute is defined for a class? ----- In the land of the blind, the one eyed man is king. -- modified at 13:19 Monday 13th March, 2006
The behaviour is strange indeed, however, AFAIK, you can use
Type.IsSerializable
:) -
The behaviour is strange indeed, however, AFAIK, you can use
Type.IsSerializable
:)Wow - that's exactly what I was looking for to begin with, right down to the property name I was expecting for it - only when I didn't find it did I eventually find the GetCustomAttribute method that didn't work for this. How the heck did I miss that?!?!?!? :confused: Thanks for pointing out my blindness, I think I'll go get my eyes checked now! ----- In the land of the blind, the one eyed man is king.
-
Wow - that's exactly what I was looking for to begin with, right down to the property name I was expecting for it - only when I didn't find it did I eventually find the GetCustomAttribute method that didn't work for this. How the heck did I miss that?!?!?!? :confused: Thanks for pointing out my blindness, I think I'll go get my eyes checked now! ----- In the land of the blind, the one eyed man is king.
:) 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); } } }