这如何作为 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
我的代码使用 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/