views:

375

answers:

2

I was wondering if anyone knows of a quick way or if anyone has written a reflection tool to tell which objects in a solution are not marked as serializable. I'm switching a site over to a StateServer and i need to have all objects marked as serializable. I don't want to miss any.

Also, second part do enums have to be serializable?

The website is an ASP.NET 1.1 site built with VS 2003

A: 

Enum require to be serializable.

To find out what is not serializable, I do have unit test to these object that simply call a method to know if it's serializable. This method try to serialize, if fail, the object is not...

    public static Stream serialize<T>(T objectToSerialize)
    {
        MemoryStream mem = new MemoryStream();
        BinaryFormatter b = new BinaryFormatter();
        b.Serialize(mem, objectToSerialize);
        return mem;
    }

In you unittest you need to call serialize(objectToCheck); if not serizlisable, an exception will raise.

Daok
Two things, one is there a way to do this where it searches the whole solution? two, my solution is an ASP.NET 1.1 site, using vs 2003, I've never built tests in this old environment, can I?
bendewey
The whole solution shouldn't be all serialize. You should be able to select what class should or not. ANd VS2003 allow testing... unit testing is not in VS (like Nunit)
Daok
+2  A: 

Enums are inherently serialisable.

I wrote this helper for getting attributes off objects, you could add a line to the top of your application that calls the following code:

var assemblies = GetTheAssembliesFromYourApp();
foreach (var assembly in assemblies)
{
    var types = assembly.GetTypes ();
    foreach (var type in types)
    {
        if (AttributeHelper.GetAttrbiute<Serializable> (type) == null)
        {
            // Log somewhere - this type isn't serialisable...
        }
    }
}


static class AttributeHelper
{
 #region Static public methods

 #region GetAttribute

 static public T GetAttribute<T> (object obj)
  where T : Attribute
 {
  if (obj == null)
   throw new ArgumentNullException ("obj");

                    // If the object is a member info then we can use it, otherwise it's an instance of 'something' so get it's type...
  var member = (obj is System.Reflection.MemberInfo) ? (System.Reflection.MemberInfo)obj : obj.GetType ();

  return GetAttributeImpl<T> (member);
 }

 #endregion GetAttribute

 #endregion Static public methods

 #region Static methods

 #region GetAttributeImpl

 static T GetAttributeImpl<T> (System.Reflection.MemberInfo member)
  where T : Attribute
 {
  var attribs = member.GetCustomAttributes (typeof (T), false);
  if (attribs == null || attribs.Length == 0)
   return null;

  return attribs[0] as T;
 }

 #endregion GetAttributeImpl

 #endregion Static methods
}
Kieron
Just seen your comment about the site being a 1.1 app, the code above uses generics, but it won't be hard to remove the generics from it.
Kieron
I was able to write a 2.0 console app and load the 1.1 dlls thanks. this worked. you may want to update you code, your missing a foreach(type in types) on the top.
bendewey
Is it bad to mark all objects as serializable just in case they get stored in the session?
bendewey