Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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
.net索引类继承_.net_Inheritance_Indexer - Fatal编程技术网

.net索引类继承

.net索引类继承,.net,inheritance,indexer,.net,Inheritance,Indexer,我尝试为项目中的所有类创建索引类 例如 无论什么类别: public class cWhatEver : cBase { public string Description { get; set; } public string Label { get; set; } public string Name { get; set; } } 继承cBase类的类: public abstract class cBase : IBase, System.ICloneabl

我尝试为项目中的所有类创建索引类

例如

无论什么类别:

public class cWhatEver : cBase
{

    public string Description { get; set; }
    public string Label { get; set; }
    public string Name { get; set; }

}
继承cBase类的类:

public abstract class cBase : IBase, System.ICloneable
{

    private static Int64 _Count;
    private Int64 _ID;

    [ReadOnly(true)]
    public Int64 ID {
        get { return _ID; }
    }

    [ReadOnly(true)]
    public Int64 Count {
        get { return _Count; }
    }

    public cBase()
    {
        cBase._Count += 1;
        this._ID = cBase._Count - 1;
    }

    public object Clone()
    {
        cBase newClone = (cBase)base.MemberwiseClone();
        cBase._Count += 1;
        newClone._ID = cBase._Count - 1;
        return newClone;
    }

    protected override void Finalize()
    {
        _Count -= 1;
    }
}
但是当创建类时,我总是得到所有类中所有项的计数,它继承cBase,而不是类(T)或id的计数

假设class which有5个项目,class which_1有10个项目,那么计数是15,而不是5和10,并且id也计算在所有项目上

那么,我该如何为其编制索引呢。也许有人能帮我


谢谢

如果我理解正确,您希望维护层次结构中每个类的实例计数。问题在于
\u Count
字段在
cBase
的所有实例之间共享,包括派生类

一个可能的解决方案是利用这样一个事实,即泛型类的不同实现有其自己的静态字段的不同副本,即,如果您有这个类:

class Foo<T>
{
    private static int _count;
}
(注意如何将类型参数约束为从泛型类型本身派生)

然后您可以创建如下派生类:

abstract class Base<T> where T : Base<T>
{
    private static long _count;
    public static long Count { get { return _count; } }

    public Base()
    {
        _count++;
    }
}
class Derived1 : Base<Derived1> { }
class Derived2 : Base<Derived2> { }
您将获得所需的结果:

Derived1: 4
Derived2: 2


(*)实际上这个模式最初来自C++,是基于模板,而不是泛型,但是它很好地翻译成支持C语言和其他支持泛型的语言。

< P>如果我理解正确,你要保持层次结构中每个类的实例计数。问题在于
\u Count
字段在
cBase
的所有实例之间共享,包括派生类

一个可能的解决方案是利用这样一个事实,即泛型类的不同实现有其自己的静态字段的不同副本,即,如果您有这个类:

class Foo<T>
{
    private static int _count;
}
(注意如何将类型参数约束为从泛型类型本身派生)

然后您可以创建如下派生类:

abstract class Base<T> where T : Base<T>
{
    private static long _count;
    public static long Count { get { return _count; } }

    public Base()
    {
        _count++;
    }
}
class Derived1 : Base<Derived1> { }
class Derived2 : Base<Derived2> { }
您将获得所需的结果:

Derived1: 4
Derived2: 2


(*)实际上这个模式最初来自C++,是基于模板,而不是泛型,但是它很好地翻译成支持C语言和其他支持泛型的语言。

为什么你想做这样的事我真的无法理解,但基本上问题是**计数*字段只存在一次,从它派生的每个类类型都没有一个计数器

一种可能的替代方法是使用
字典
,该字典为创建的每个类类型保存一个不同的计数器,然后仅递增相关的计数器:

public abstract class cBase : IBase, System.ICloneable
{
    private static Dictionary<Type,Int64> _counts;

    [ReadOnly(true)]
    public Int64 Count
    {
        get { return _counts[this.GetType()]; }
    }

    public cBase()
    {
        if(this._counts.Contains(this.GetType())
            this._counts[this.GetType()] = this._counts[this.GetType()]  +1;
        else
            this._counts.Add(this.GetType(),1);

        this._ID = this._count[this.GetType()] - 1;
    }

    protected override void Finalize()
    {
        this._counts[this.GetType()] = this._counts[this.GetType()] - 1;
    }
}
公共抽象类cBase:IBase,System.ICloneable
{
私有静态字典计数;
[只读(正确)]
公共Int64计数
{
获取{return _counts[this.GetType()];}
}
公共cBase()
{
if(this.\u counts.Contains)(this.GetType())
this._计数[this.GetType()]=this._计数[this.GetType()]+1;
其他的
this.u counts.Add(this.GetType(),1);
this.\u ID=this.\u count[this.GetType()]-1;
}
受保护的覆盖void Finalize()
{
this._计数[this.GetType()]=this._计数[this.GetType()]-1;
}
}

我真的不明白为什么要做这样的事情,但基本上问题是*\u count*字段只存在一次,而不是从它派生的每个类类型都有一个计数器

一种可能的替代方法是使用
字典
,该字典为创建的每个类类型保存一个不同的计数器,然后仅递增相关的计数器:

public abstract class cBase : IBase, System.ICloneable
{
    private static Dictionary<Type,Int64> _counts;

    [ReadOnly(true)]
    public Int64 Count
    {
        get { return _counts[this.GetType()]; }
    }

    public cBase()
    {
        if(this._counts.Contains(this.GetType())
            this._counts[this.GetType()] = this._counts[this.GetType()]  +1;
        else
            this._counts.Add(this.GetType(),1);

        this._ID = this._count[this.GetType()] - 1;
    }

    protected override void Finalize()
    {
        this._counts[this.GetType()] = this._counts[this.GetType()] - 1;
    }
}
公共抽象类cBase:IBase,System.ICloneable
{
私有静态字典计数;
[只读(正确)]
公共Int64计数
{
获取{return _counts[this.GetType()];}
}
公共cBase()
{
if(this.\u counts.Contains)(this.GetType())
this._计数[this.GetType()]=this._计数[this.GetType()]+1;
其他的
this.u counts.Add(this.GetType(),1);
this.\u ID=this.\u count[this.GetType()]-1;
}
受保护的覆盖void Finalize()
{
this._计数[this.GetType()]=this._计数[this.GetType()]-1;
}
}

我知道您可能来自Java或其他使用
camelCase
作为类名的公司,但声明类应该是
PascelCase
@ScottChamberlain:
cBase
-我打赌您有C/++背景。这是您的实际代码吗?我不这么认为,因为覆盖Finalize是不合法的;您必须声明一个析构函数代替了.thx的答案…试着适应我的需要,并精确一点…sry我是新来的,和所有的.net东西斗争一点:)目标是为项目中的所有类获取一个indexer类,T的每个项都应该有一个ID,这样我就可以获取ID上的每个类项,以及自动增加/减少的类(T)项的计数。所以我需要T.count和T(index)对于所有类。我知道您可能来自Java或其他使用
camelCase
作为类名的类,但声明类应该是
PascelCase
@ScottChamberlain:
cBase
-我打赌是C/++背景。这是您的实际代码吗?我不这么认为,因为覆盖Finalize是不合法的;您有要声明析构函数而不是.thx来获取答案…尝试根据我的需要调整它,并稍微精确一点…sry我对这一点不熟悉,并与所有.net内容进行了一些斗争:)目标是为项目中的所有类获得一个索引器类T的每个项都应该有一个ID,这样我就可以在ID上获得类的每个项,以及自动增加/减少的(T类)项目数。所以我需要所有类的T.count和T(index)。