将泛型类型定义传递给asmx webservice的任何方式 [英] Any way of passing generic type definition to asmx webservice

查看:145
本文介绍了将泛型类型定义传递给asmx webservice的任何方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery ajax调用ASP.Net Web服务(ASMX文件)来更新网页数据。

I use jQuery ajax calls to ASP.Net web services (ASMX files) to update the web page data.

我有数据库存储代码与域分离对象类代码(在下面的示例代码中,有一个Animal类和一个AnimalRepository类)。我实现了一个约定,即所有类似的域对象库类都有一个Store方法。

I have the database storing code seperate from the domain object class code (in the example code below, there is an Animal class and an AnimalRepository class). I'm implememting a convention that all similar domain object repository classes will have a Store method .

我想创建一个可以调用Store方法的Web服务用于这些存储库类中的任何一个。为了得到这个工作,在下面的代码示例中,List(Of Animal)参数需要是一个泛型类型。

I would like to create a web service that can cater for calling the Store method for any one of these repository classes. To get this to work, in the below code sample, the List(Of Animal) parameter would need to be a generic type.

我尝试了一个List(Of Object) ,但Store命令错误不能转换类型。我不认为我可以将List(Of Object)转换为正确的类型,因为直到运行时才知道。

I tried a List(Of Object), but the Store command errors on not able to convert types. I don't think I can cast the List(Of Object) to the right type, as it's not known until runtime.

    <WebMethod(True)> _
Public Sub StoreAnimals(ByVal _animals As List(Of Animal), ByVal _type As String)
    Dim classType As Type = Type.GetType(_type & "Repository, MyCompany.Assembly")
    Dim instanceOfClass = Activator.CreateInstance(classType)
    Dim method As MethodInfo = classType.GetMethod("Store")
    method.Invoke(instanceOfClass, New Object() {_animals})
End Sub

理想情况下,我想:

Ideally, I would like:

    <WebMethod(True)> _
Public Sub StoreData(Of T)(ByVal _data As List(Of T), ByVal _type As String)
    Dim classType As Type = Type.GetType(_type & "Repository, MyCompany.Assembly")
    Dim instanceOfClass = Activator.CreateInstance(classType)
    Dim method As MethodInfo = classType.GetMethod("Store")
    method.Invoke(instanceOfClass, New Object() {_data})
End Sub

webservice方法定义中没有泛型的空间,但有没有一个技巧达到相同的最终结果。
否则,我将创建大量这些仅针对第一个参数不同的Store * webservices。

There's no room for generics in the webservice method definition, but is there a trick to achieve the same end result. Otherwise, I'm going to be creating lots of these Store* webservices that only differ on the first parameter.

推荐答案

<泛型在Web服务领域没有意义。

Generics have no meaning in the web services world.

您无法通过ASMX来到任何地方 close 。使用WCF,您可能可以执行某些操作,但不能在操作合约中使用开放的泛型类型。

You can't come anywhere close to this with ASMX. With WCF, you might be able to do something, but you can't use an open generic type in an operation contract.

这篇关于将泛型类型定义传递给asmx webservice的任何方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆