简体   繁体   English

在WCF datacontract中公开扩展方法

[英]Exposing Extension methods in WCF datacontract

Is it possible to expose extension methods on objects which are data contracts in WCF? 是否可以在WCF中的数据协定对象上公开扩展方法?

Here is an example: 这是一个例子:

[DataContract]
public class Customer
{
    [DataMember]
    public int ID { get; set; }

    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public Product CurrentProduct { get; set; }

    public Customer DeepCopy()
    {
        //Do Something else. For example just return a new instance
        return new Customer();
    }      
}

[DataContract]
public class Product
{
    [DataMember]
    public int ID;

    [DataMember]
    public int Name;
}

In the customer class there is a deep copy method. 在客户类中,有一个深层复制方法。 How do I expose this to the client? 我该如何向客户公开?

不可以,您只能在WCF DataContract中公开属性,而不能在方法中公开。

You can almost do this using the "Reuse types" feature. 您几乎可以使用“重用类型”功能执行此操作。 Unfortunately for your case, it only allows the reuse of types which are actually used by the service as input or output parameters. 不幸的是,对于您的情况,它仅允许重用服务实际用作输入或输出参数的类型。 Your class holding the extension methods will be a static class and will therefore not appear as an input parameter nor as a return type. 保存扩展方法的类将是一个静态类,因此不会显示为输入参数或返回类型。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM