c# - WCF 泛型类

标签 c# wcf generics .net-4.0

这如何作为 WCF 服务工作?

public class BusinessObject<T> where T : IEntity
{
    public T Entity { get; set; }

    public BusinessObject(T entity)
    {
        this.Entity = entity;
    }
}

public interface IEntity { }

public class Student : IEntity
{
    public int StudentID { get; set; }
    public string Name { get; set; }
}

我想在 WCF 服务中公开 BusinessObject 类和所有继承 IEntity 接口(interface)的类。

我的代码使用 C#、.NET Framework 4.0,在 Visual Studio 2010 Pro 中构建。

最佳答案

在通过 WCF 向客户端公开 BusinessObject 时,您必须使用封闭的泛型来实现。

[DataContract]
public class BusinessObject<T> where T : IEntity
{
    [DataMember]
    public T Entity { get; set; }

    public BusinessObject(T entity)
    {
        this.Entity = entity;
    }
}  

[ServiceContract]
interface IMyContract {
[OperationContract]
BusinessObject<Student> GetStudent(...) // must be closed generic
}

关于c# - WCF 泛型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8997346/

相关文章:

c# - 无法使用 Xunit 测试端点 - StatusCode : 400, ReasonPhrase: 'Bad Request'

c# - 如何在 Outlook 中为自定义按钮添加图像图标

c# - 限项 list

c++ - 错误 : ISO C++ forbids declaration of ‘iterator’ with no type

c++ - 如何在 C++ 中使用 override 关键字进行多重泛型继承?

c# - 在输入文本中有多个 % 运算符的 LINQ 中等效的 SQL LIKE 运算符是什么?

c# - 数据绑定(bind)到附加属性

c# - 从浏览器运行 WCF 方法

wcf - 将 WCF 服务中的派生类型反序列化为基类型,但保留类型信息

c# - 处理 WCF 服务中的异常?