55502f40dc8b7c769880b10874abc9d0

I need a fast method to check to see if a type is XML Serializable, be that with the [Serializable] attribute or the IXmlSerializable interface. This is what I came up with. Can we make it better?

/// <summary>
/// Test to see if the type can be XML serialized
/// </summary>
/// <param name="objectToTest"></param>
/// <returns></returns>
private static bool CanBeXmlSerialized(Type objectToTest)
{
	Type[] interfaces = objectToTest.GetInterfaces();
	bool containsIXmlSerializable = interfaces.Contains(typeof(IXmlSerializable));

	return containsIXmlSerializable || objectToTest.IsSerializable;
}

Refactorings

No refactoring yet !

4d72203c38dd5f3e3d2d446b5888e8a7

Elij

November 20, 2008, November 20, 2008 01:55, permalink

2 ratings. Login to rate!

If you have an instance at run time you could do the following

private static bool canBeXmlSerialized(object @object)
{
	return @object is IXmlSerializable || @object.GetType().IsSerializable;
}
55502f40dc8b7c769880b10874abc9d0

sbehnke.myopenid.com

November 20, 2008, November 20, 2008 16:22, permalink

No rating. Login to rate!

That's an interesting use of @. Since Visual Studio 7 and C# 1.0 I've not thought to call anything @object.

C093cf580ce8d2d2e900d7e017c18eb0

CB

April 22, 2009, April 22, 2009 11:07, permalink

No rating. Login to rate!

How about the IsSerializable function if the Type class?

private static bool canBeXmlSerialized(object @object)
{
	return obj.GetType.IsSerializable();
}

Your refactoring





Format Copy from initial code

or Cancel