Joh*_*ona 2 c# generics wcf .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; }
}
Run Code Online (Sandbox Code Playgroud)
我想公开BusinessObject <T>类以及在WCF服务中继承IEntity接口的所有类.
我的代码在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
}
Run Code Online (Sandbox Code Playgroud)