Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Can';t访问从泛型类继承的受保护变量_C#_Generics_Inheritance - Fatal编程技术网

C#Can';t访问从泛型类继承的受保护变量

C#Can';t访问从泛型类继承的受保护变量,c#,generics,inheritance,C#,Generics,Inheritance,第一次海报 我是C#和泛型新手,我一直在尝试为只读数据条目创建一系列简单的对象表。 在我的泛型插入例程中,我递增一个静态Id变量,以确保它总是唯一的。为了防止它被修改,我将其设置为protected,但泛型类会抛出一个编译错误,声明无法访问该Id 我正在努力找出为什么正如我所想的“where T:DBEntity”会允许这样做 提前感谢: public class DBEntity { public int Id { get; protected set; } } public cla

第一次海报

我是C#和泛型新手,我一直在尝试为只读数据条目创建一系列简单的对象表。 在我的泛型插入例程中,我递增一个静态Id变量,以确保它总是唯一的。为了防止它被修改,我将其设置为protected,但泛型类会抛出一个编译错误,声明无法访问该Id

我正在努力找出为什么正如我所想的“where T:DBEntity”会允许这样做

提前感谢:

public class DBEntity
{
    public int Id { get; protected set; }
}

public class Table<T> where T : DBEntity
{

    static int _id = 0;
    private readonly List<T> _set = new List<T>();

    public IEnumerable<T> Set() { return _set; }

    public void Insert(T item)
    {
        _id++;
        item.Id = _id; //when set to protected it is inaccessible
        _set.Add(item);
    }
}
公共类DBEntity
{
public int Id{get;protected set;}
}
公共类表,其中T:DBEntity
{
静态int_id=0;
私有只读列表_set=new List();
public IEnumerable Set(){return\u Set;}
公共无效插入(T项)
{
_id++;
item.Id=\u Id;//当设置为受保护时,它是不可访问的
_设置。添加(项目);
}
}

您正在保护ID,因此无法设置它。老实说就这么简单

同样,做一个表的泛型,并将泛型绑定到一个具体的类,也不会给您带来任何好处。考虑一个接口代替.< /P> 您可以通过以下方式解决问题:

public interface IDatabaseItem
{
     int? Id { get; }
     SetID(int value);
}

public class DBEntity : IDatabaseItem
{
    public int? Id { get; private set; }


    public void SetID(int value)
    {
        if (Id == null)
        {
            Id = value;
        }
        else
        {
            throw new Exception("Cannot set assigned Id; can only set Id when it is not assgined.");
        }
    }
}

public class Table<T> where T : IDatabaseItem
{

    static int _id = 0;
    private readonly List<T> _set = new List<T>();

    public IEnumerable<T> Set() { return _set; }

    public void Insert(T item)
    {
        if (item.Id == null)
        {
            _id++;
            item.SetID(_id);
            _set.Add(item);
        }
        else
        {
            //Handle this case. Something else set the ID, yet you're trying to insert it. This would, with your code, imply a bug.
        }
    }
}
公共接口IDatabaseItem
{
int?Id{get;}
SetID(int值);
}
公共类DBEntity:IDatabaseItem
{
公共int?Id{get;private set;}
公共void SetID(int值)
{
if(Id==null)
{
Id=值;
}
其他的
{
抛出新异常(“无法设置分配的Id;只能在未分配的情况下设置Id”);
}
}
}
公共类表,其中T:IDatabaseItem
{
静态int_id=0;
私有只读列表_set=new List();
public IEnumerable Set(){return\u Set;}
公共无效插入(T项)
{
如果(item.Id==null)
{
_id++;
项目设置id(_id);
_设置。添加(项目);
}
其他的
{
//处理这种情况。其他的东西设置了ID,而你却试图插入它。这将意味着,在你的代码中,有一个bug。
}
}
}

Table不继承DBEntity类,因此无法访问Id。Table不会,但不会,对吗?如果我将其保留为公共,它可以访问IdBut,但T只是一个通用参数。从表中使用T不是一种继承。您可以使用“内部设置”将分配限制为当前程序集。您的界面中没有
SetID
。谢谢。。。我显然仍然误解了一些东西,因为我认为“where T:DBEntity”意味着它是新的,无论是什么T,它都继承了DBEntity,因此可以访问Id。我想我需要做更多的阅读!)别担心,@LucaTImp!接口是您想要的(它们比从具体类派生要好),但要确保它们很小,并且行为是定义的;大规模的接口或接口只适用于一个具体的类,可以让你在路上遇到麻烦。因此,考虑到你的更改,你会认为泛型的有效使用吗?这个想法将为学生、教师、教室增加桌子。。。我相信你会明白的。:)再说一次,这只是在考验我的理解力,或者说是欠缺!“接口是您想要的(它们比从具体类派生要好)”,这是因为。。。?这绝对是基于意见的陈述,没有任何证据。