Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
    }
}
like image 486
Bob Horn Avatar asked Apr 25 '13 16:04

Bob Horn


3 Answers

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;
}
like image 109
Bob Horn Avatar answered Nov 10 '22 12:11

Bob Horn


The method mentioned by Bob will work as long as all involved classes are in the same assembly.

The following method will work across assemblies:

[DataContract]
[KnownType("GetDerivedTypes")]
public class BaseClass
{
  public static List<Type> DerivedTypes = new List<Type>();

  private static IEnumerable<Type> GetDerivedTypes()
  {
    return DerivedTypes;
  }
}


[DataContract]
public class DerivedClass : BaseClass
{
  //static constructor
  static DerivedClass()
  {
    BaseClass.DerivedTypes.Add(typeof(DerivedClass)); 
  }
}
like image 8
Leon van der Walt Avatar answered Nov 10 '22 12:11

Leon van der Walt


Here's my variant on the accepted answer:

    private static IEnumerable<Type> GetKnownTypes() {
        Type baseType = typeof(MyBaseType);
        return AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(x => x.DefinedTypes)
            .Where(x => x.IsClass && !x.IsAbstract && x.GetCustomAttribute<DataContractAttribute>() != null && baseType.IsAssignableFrom(x));
    }

The differences are:

  1. Looks at all loaded assemblies.
  2. Checks some bits we are interested in (DataContract I think is required if you're using DataContractJsonSerializer) such as being a concrete class.
  3. You can use isSubclassOf here, I tend to prefer IsAssignableFrom in general to catch all overridden variants. In particular I think it works with generics.
  4. Take advantage of KnownTypes accepting an IEnumerable (if it matters in this case, probably not) instead of converting to an array.
like image 2
user169771 Avatar answered Nov 10 '22 13:11

user169771