我希望能够创建一个基类型约束的静态泛型类型
public static class Manager<T> where T : HasId
{
public static T GetSingleById(ref List<T> items, Guid id)
{
// the Id is a property provided by HasId
return (from i in items where i.Id == id select i).SingleOrDefault();
}
}
Run Code Online (Sandbox Code Playgroud)
然后添加另一种方法
...
public static IEnumerable<T> GetManyByParentId(ref List<T> items, Guid parentId) where T : HasIdAndParentId
{
// the parentId is a property of HasIdAndParentId which subclasses HasId
return from i in items where i.ParentId == parentId select i;
}
... …
Run Code Online (Sandbox Code Playgroud) 这段代码
public class Entity
{
public string First { get; }
public string Last { get; }
public Entity()
{
this.First = "First name";
this.Last = "Last name";
}
}
Run Code Online (Sandbox Code Playgroud)
将在VS2015中编译而VS2013会出现错误"属性或索引器'ScottRickman.Entity.First'无法分配给它 - 它是只读的"
这是VS2015中的错误吗?