4

I have a base class that I use in WCF service calls,

[KnownType(typeof(MyDerivedClass))]
public abstract class MyBaseClass {
   //some properties
}

I derive from it and every time I derive I have to add the [KnownType(typeof(MyDerivedClass))] attribute and every time I do I violate the Open/Closed principle. Is there anyway to derive classes like this for use in WCF and not have to add attributes to the parent class each time?

Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228

1 Answers1

2

You can use a static method which will return known types :

[DataContract]
[KnownType("GetKnownType")]
public class MyBaseClass
{
    //some properties

    private static Type[] GetKnownType()
    {
        return KnownTypesHelper.GetKnownTypes<MyBaseClass>();
    }
}

Now create a static class KnownTypesHelper that will return an array of known types (by scanning assemblies to find implementations of base class for example...)

Romain Meresse
  • 3,044
  • 25
  • 29