Problem: I have a WCF service that invokes methods via reflection and sends the results. Like so:
[OperationContract]
Object InvokeMemberByReflection(string typeName, string name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args);
I do this because I have an existing API X which I want to invoke. I don’t want to write wrappers for all of methods.
I also don’t use explicit DataContracts because both the server and client reference the same set of dlls.
However, for the InvokeMemberByReflection method above, since I’m sending method parameters as an array of type object, it looks like I have to add all possible method signatures of API X as KnownTypeAttributes for the DataContractSerializer to work.
Is there a work around for this? The CLR basically knows the types of the elements that are contained in the object array. Can it not serialize/deserialize based on this (granted it’s a different type all together)?
Also I would appreciate any other patterns for addressing such a problem. Reflection is too slow for obvious reasons.
If the client and the server are using the same DLL (i.e., the types are exactly the same), then you can use the
NetDataContractSerializer, which doesn’t need the known types attribute (it emits the CLR type information when serializing). This post has an example on how to use that serializer.