0

We have a server that can return a "configuration", this configuration is a big and complex hierachy of classes.

Our application works by "packages", meaning that we have a "common" base, and then there is part of the application that will not be even installed.

It means that the configurations classes are not all in the same project, and we often have structure like:

public class SomeObject{
   public IEnumerable<ISomeObjectChild> Childs{get;set;}
}
public interface SomeObjectChild{
   //[...]
}

And in some other project, have some class that implement them:

public class SomeChildImplementation: ISomeObjectChild{
   //[...]
}

There is factory to instantiate those kind of childs and everything is working fine.

Now we have to transfer over the network this kind of configuration, using WCF.

My issue is that I know I should indicate what could be the types potentially implementing the ISomeObjectChild. I know that usually, we should declare them with [KnownType(typeof(SomeChildImplementation))], but it is not possible:

  1. This class doesn't know all its possible childs
  2. This class cannot even reference thoses childs

So how could I indicate what are the possible implementations?

(in this case it's with an interface, but I've some others cases with abstract classes).

J4N
  • 19,480
  • 39
  • 187
  • 340
  • Not a duplicate but a answer to your question: https://stackoverflow.com/a/41460207/6666799 – Rabban Jun 28 '17 at 12:36

1 Answers1

0

KnownTypesAttribute has another constructor that can be used to specify a method that will return an array of KnownTypes, see here

The implementation of this method can depend on how dynamic you want to be. Either configure the types you want to support in some configuration file or use System.Reflection to scan for classes that implement ISomeObjectChild

Amith Sewnarain
  • 655
  • 6
  • 11
  • This is interesting. I'm curious, performance wise, what would you recommend? How would you do this "configuration"? – J4N Jul 05 '17 at 06:04
  • @J4N Are you referring to the configuration file or reflection approach? In terms of performance the method you use will only be invoked once, at least for the `DataContractSerializer`. – Amith Sewnarain Jul 14 '17 at 08:28