开发者

Get generic type object (e.g. MyObject<T>) from remote machine

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


SOAP Web Services are based on WSDL and XML Schema. The WSDL describes the services and operations, and XML Schema defines the data that the operations work with.

XML Schema cannot describe generics. Therefore, there are no generics in web services.


You can not user generics in WCF. The service must know the type of object during transmission. But is not a specific type. What you can do is you can define a custom class that contains the definition of all other entities in your app. And you can set a specific entity what ever you need and then transmit that class. By this way you can transfer multiple objects as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜