I found that it does not work with internal / private types, but at the same time my public type works fine. This means that anonymous types will not: (
Using a reflector, I found the ClassDataContract.IsNonAttributedTypeValidForSerialization (Type) method, which seems to make a decision. This is the last line that seems to be a killer, the type should be visible, so internal / private types are not allowed :(
internal static bool IsNonAttributedTypeValidForSerialization(Type type)
{
if (type.IsArray)
{
return false;
}
if (type.IsEnum)
{
return false;
}
if (type.IsGenericParameter)
{
return false;
}
if (Globals.TypeOfIXmlSerializable.IsAssignableFrom(type))
{
return false;
}
if (type.IsPointer)
{
return false;
}
if (type.IsDefined(Globals.TypeOfCollectionDataContractAttribute, false))
{
return false;
}
foreach (Type type2 in type.GetInterfaces())
{
if (CollectionDataContract.IsCollectionInterface(type2))
{
return false;
}
}
if (type.IsSerializable)
{
return false;
}
if (Globals.TypeOfISerializable.IsAssignableFrom(type))
{
return false;
}
if (type.IsDefined(Globals.TypeOfDataContractAttribute, false))
{
return false;
}
if (type == Globals.TypeOfExtensionDataObject)
{
return false;
}
if (type.IsValueType)
{
return type.IsVisible;
}
return (type.IsVisible && (type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, Globals.EmptyTypeArray, null) != null));
}
Jacob Stanley
source
share