I have two applications that are communicating through WCF.
On the server the following object exists:
public class MyObject<T>
{
...
public Entry<T> GetValue()
}
Where Entry<T>
is another object with T Data
as a public property. T could be any number of types (string, double, etc)
On the client I have ClientObject<T>
that needs to get the value of Data
from the server (same type).
Since I'm using WCF, I have to define my ServiceContract as an interface, and I can't have ClientObject<T>
call Entry<T> GetMyObjectValue (string Name)
which calls GetValue
on the correct MyObject<T>
because my interface isn't aware of the type information.
I've tried implementing separate GetValue functions (GetMyObjectValueDouble, GetMyObjectValueString) in the interface and then have ClientObject
determine the correct one to call. However, Entry<T> val = (Entry<T>)GetMyObjectValueDouble(...);
doesn't work because it's not sure about the type information.
How can I go about getting a generic object over WCF with the correct type information? Let me know if there are other details I can provide.
Thanks!
I used a combination of methods to get this to work. I implemented several Entry<double> GetMyObjectValueDouble(...)
, Entry<string> GetMyObjectValueString(...)
methods on the server. On the client I check the type of the object and then call the appropriate function:
Entry<T> Data = (Entry<T>)Convert.ChangeType(Client.GetMyObjectValueDouble(...),typeof(Entry<T>));
Hope that helps somebody