我如何“具体化” WCF的开放通用? [英] How do I "concretise" an open generic for WCF ?

查看:112
本文介绍了我如何“具体化” WCF的开放通用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(寻找想法...)



所以 - 当你使用[ServiceContract()]和[DataContract()]创建WCF服务时,你可以不要使用开放式泛型 - 我明白为什么。



然而 - 任何人都对如何指定一小组类型以及将泛型变成服务中的具体实现有任何想法? (可能看看codegen ??)



即如果我有像

(Looking for thoughts...)

So - when you create a WCF service with a [ServiceContract()] and [DataContract()] you can't use open generics - which I do understand why.

However - anyone have any ideas about how I could specify a small set of types and have the generics turned into concrete implementations in the service ? (Probably looking at codegen??)

i.e. if I have a function like

Add(Of TParam)(Byval lhs As TParam, ByVal rhs As TParam) As TParam 

我' d喜欢它生成: -



I'd like it to generate:-

AddInteger(Byval lhs As Integer, Byval rhs As Integer) As Integer

AddDouble(Byval lhs As Double, Byval rhs As Double) As Double





(等)



(etc.)

推荐答案

这与WCF无关。您只需要了解泛型如何工作以及它们的声明和实例化的语法,以及类型推断。请参阅:

http://msdn.microsoft.com/en-us /library/ms235246.aspx [ ^ ]。



-SA
This is not related to WCF either. You just need to learn how generics work in general and the syntax for their declaration and instantiation, as well as the type inference. Please see:
http://msdn.microsoft.com/en-us/library/ms235246.aspx[^].

—SA


碰巧我刚刚明确宣布了我想要处理的特定类型的接口,并将它们传递给WCF服务合同另一侧的通用代码,例如



As it happens I have just explicitly declared the interfaces for the specific types I want to handle and passed them on to the generic code the other side of the WCF service contract e.g.

''' <summary>
 ''' Get the active clients on the system
 ''' </summary>
 ''' <param name="query">
 ''' The query specification for getting the active clients
 ''' </param>
 ''' <returns>
 ''' A set of client records in JSON form
 ''' </returns>
 <operationcontract()>
 <webget(responseformat:>
 Function GetActiveClients(ByVal query As GetActiveClientsQueryDefinition) As IReadOnlyList(Of ClientSummary)





,执行方法是: -



and the implementation of this is:-

Public Function GetActiveClients(query As GetActiveClientsQueryDefinition) As IReadOnlyList(Of ClientSummary) Implements IQueryHandlerService.GetActiveClients
    If (m_queryHandler IsNot Nothing) Then
        Return m_queryHandler.Handle(query)
    Else
        'Return an empty list
        Return New List(Of ClientSummary)()
    End If
End Function





其中m_queryHandler.Handle是通用函数: -





where m_queryHandler.Handle is the generic function :-

''' <summary>
''' Handle the given query definition and return the results
''' </summary>
''' <typeparam name="TResult">
''' The type of the result
''' </typeparam>
''' <param name="query">
''' The query definition to handle
''' </param>
Public Function Handle(Of TResult)(query As Query.IQueryDefinition(Of TResult)) As TResult

    Dim handler = GetHandler(Of TResult)(query)

    If (handler IsNot Nothing) Then
        Return handler.Handle(query)
    Else
        ' Could not find and appropriate handler
        Return Nothing
    End If

End Function


这篇关于我如何“具体化” WCF的开放通用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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