WCF通用类 [英] WCF Generic Class

查看:223
本文介绍了WCF通用类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这怎么能作为一个WCF服务?

How can this work as a WCF Service?

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; }
}

我想公开BusinessObject< T> class和继承WCF服务中的IEntity接口的所有类。

I want to expose the BusinessObject <T> class and the all the class that inherits the IEntity interface in the WCF Service.

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

My code is in C#, .NET Framework 4.0, build in Visual Studio 2010 Pro.

推荐答案

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

While exposing BusinessObject to the clients via WCF, you must do that by using closed generic type.

[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
}

这篇关于WCF通用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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