WCF Derived types and violation of the Open/Closed principle

215 views Asked by At

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?

1

There are 1 answers

0
Romain Meresse On BEST ANSWER

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...)