Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to find Serializable attribute with reflection?

How to find Serializable attribute with reflection?

Scheduled Pinned Locked Moved C#
debuggingtutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vineas
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • V vineas

      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

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      The behaviour is strange indeed, however, AFAIK, you can use Type.IsSerializable :)

      xacc.ide-0.1.3.2

      V 1 Reply Last reply
      0
      • L leppie

        The behaviour is strange indeed, however, AFAIK, you can use Type.IsSerializable :)

        xacc.ide-0.1.3.2

        V Offline
        V Offline
        vineas
        wrote on last edited by
        #3

        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.

        S 1 Reply Last reply
        0
        • V vineas

          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.

          S Offline
          S Offline
          superpzgpzg
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups