简体   繁体   English

获取通用类型对象(例如MyObject <T> )从远程机器

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

I have two applications that are communicating through WCF. 我有两个通过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. 其中Entry<T>是另一个具有T Data作为公共属性的对象。 T could be any number of types (string, double, etc) T可以是任意数量的类型(字符串,双精度型等)

On the client I have ClientObject<T> that needs to get the value of Data from the server (same type). 在客户端上,我具有ClientObject<T> ,该对象需要从服务器(相同类型)获取Data的值。

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. 由于我使用的是WCF,因此必须将ServiceContract定义为接口,并且我无法让ClientObject<T>调用Entry<T> GetMyObjectValue (string Name)来在正确的MyObject<T>上调用GetValue 。不知道类型信息。

I've tried implementing separate GetValue functions (GetMyObjectValueDouble, GetMyObjectValueString) in the interface and then have ClientObject determine the correct one to call. 我尝试在接口中实现单独的GetValue函数(GetMyObjectValueDouble,GetMyObjectValueString),然后让ClientObject确定要调用的正确函数。 However, Entry<T> val = (Entry<T>)GetMyObjectValueDouble(...); 但是, 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? 如何通过WCF使用正确的类型信息获取通用对象? 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. 我在服务器上实现了几个Entry<double> GetMyObjectValueDouble(...)Entry<string> GetMyObjectValueString(...)方法。 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. SOAP Web服务基于WSDL和XML Schema。 The WSDL describes the services and operations, and XML Schema defines the data that the operations work with. WSDL描述了服务和操作,而XML Schema定义了操作所使用的数据。

XML Schema cannot describe generics. XML模式无法描述泛型。 Therefore, there are no generics in web services. 因此,Web服务中没有泛型。

You can not user generics in WCF. 您不能在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. 通过这种方式,您也可以传输多个对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法反序列化当前JSON对象(例如{“ name”:“ value”})为类型&#39;System.Collections.Generic.List - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List 无法将当前 JSON object(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1 - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型&#39;System.Collections.Generic.List`1&#39; - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1' 转换通用 Span<t> 到一个特定的实例化(例如 Span<int> ) 如果它在运行时实际上是那种类型</int></t> - Converting a generic Span<T> to a specific instantiation (e.g. Span<int>) if it actually of that type at runtime JSON.Net - 无法将当前json对象(例如{“name”:“value”})反序列化为类型&#39;system.collections.generic.list`1 - JSON.Net - cannot deserialize the current json object (e.g. {“name”:“value”}) into type 'system.collections.generic.list`1 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型“ System.Collections.Generic.ICollection`1 [System.Double]” - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.ICollection`1[System.Double]' 无法将当前 JSON 对象(例如 {&quot;name&quot;:&quot;value&quot;})反序列化为类型 &#39;System.Collections.Generic.IList 问题 - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList problem 错误:无法将当前 JSON object(例如 {“name”:“value”})反序列化为类型 'System.Collections.Generic.List`1 - Error: Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型“ System.Collections.Generic.IEnumerable`1 [WebApplication1.UserInfo]” - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.IEnumerable`1[WebApplication1.UserInfo]' JSON .NET-将当前JSON对象(例如{“ name”:“ value”})反序列化为类型&#39;System.Collections.Generic.List`1 - JSON .NET - annot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM