Search code examples
c#wcfdatacontractknown-types

Generally accepted way to avoid KnownType attribute for every derived class


Is there a generally accepted way to avoid having to use KnownType attributes on WCF services? I've been doing some research, and it looks like there are two options:

  1. Data contract resolver
  2. NetDataContractSerializer

I'm not a big fan of having to statically add KnownType attributes every time I add a new type, hence wanting to avoid it.

Is there a third option that should be used? If so, what is it? If not, which of the above two options are the right way to go?

Edit - use a method

A third option would be to use reflection

[DataContract]
[KnownType("DerivedTypes")]
public abstract class FooBase
{
    private static Type[] DerivedTypes()
    {
        return typeof(FooBase).GetDerivedTypes(Assembly.GetExecutingAssembly()).ToArray();
    }
}

Solution

  • I wanted to post what seems to be the simplest, most elegant solution that I can think of so far. If another answer comes along that's better, I'll go with that. But for now, this worked well.

    The base class, with only one KnownType attribute, pointing to a method called DerivedTypes():

    [KnownType("DerivedTypes")]
    [DataContract]
    public abstract class TaskBase : EntityBase
    {
        // other class members here
    
        private static Type[] DerivedTypes()
        {
            return typeof(TaskBase).GetDerivedTypes(Assembly.GetExecutingAssembly()).ToArray();
        }
    }
    

    The GetDerivedTypes() method, in a separate ReflectionUtility class:

    public static IEnumerable<Type> GetDerivedTypes(this Type baseType, Assembly assembly)
    {
        var types = from t in assembly.GetTypes()
                    where t.IsSubclassOf(baseType)
                    select t;
    
        return types;
    }