Wednesday, July 24, 2013

An Alternative To Stacking Many KnownType Attributes

If you find yourself having to stack many KnownType attributes on a class that is sent over the wire for WCF communication, there is a slick alternative.

Instead of something like this:


You can specify a method to reflectively return all of the types for you:


And here is the GetDerivedTypes() extension method:


Now we no longer have to add new KnownType attibutes every time we have a new subtype. Nice, eh?

2 comments:

Unknown said...

What if we have classes in different assemblies ???

Robert Horn said...

Then we would change the GetDerivedTypes() method to do more than just look in the current assembly.