Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Datacontract free serialization (3.5 SP1)

Has anybody got this to actually work? Documentation is non existent on how to enable this feature and I get missing attribute exceptions despite having a 3.5 SP1 project.

like image 638
user7375 Avatar asked Dec 28 '25 05:12

user7375


1 Answers

I found that it doesn't work with internal/private types, but making my type public it worked fine. This means no anonymous types either :(

Using reflector I found the method ClassDataContract.IsNonAttributedTypeValidForSerialization(Type) that seems to make the decision. It's the last line that seems to be the killer, the type must be visible, so no internal/private types 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));

}


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!