문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top