C#泛型类包含<;T>;但我无法访问<;T>;班

C#泛型类包含<;T>;但我无法访问<;T>;班,c#,generics,C#,Generics,我有一个通用类,DisplayGrid class DisplayGrid<T> where T : class foreach (T r in this.allRows) { if (!(r.isAValidRow)) { blnRowsValid = false; break; } } 。。。在DisplayGrid的属性中,我想迭代allRows并访问的方法 class DisplayGrid<T> where T : class

我有一个通用类,
DisplayGrid

class DisplayGrid<T> where T : class 
foreach (T r in this.allRows)
{
  if (!(r.isAValidRow))
  {
    blnRowsValid = false;
    break;
  }
}
。。。在
DisplayGrid
的属性中,我想迭代
allRows
并访问
的方法

class DisplayGrid<T> where T : class 
foreach (T r in this.allRows)
{
  if (!(r.isAValidRow))
  {
    blnRowsValid = false;
    break;
  }
}
。。。但是,尝试使用
上定义的
isAValidRow
方法会生成编译器错误

error CS1061: 'T' does not contain a definition for 'isAValidRow' and no extension method 'isAValidRow' accepting a first argument of type 'T' could be found
回顾过去的问题,这些年来,这个领域似乎发生了变化。这样的基本要求似乎不太可能仍然是不可能的


编辑1:在这个问题的原始版本中,我可能在简化示例代码方面做得太过分了。我只包含一个
类,但我的意思是,在实例化
DisplayGrid
类时,有多个类可以用作“行”

现在,我已经修改了下面的类,以便有两个不同的类可以用作
DisplayGrid
类的

在这样做的过程中,我希望解决一些正确的意见,这些意见建议不需要使用泛型,或者“where”指令可以限制为我在示例中提供的唯一类,
Row

在对示例的更改中,我定义了类
RowA
RowB
,并且我确实理解,如图所示,它们在功能上是相同的,但这只是一个简单的示例-在实际使用中
RowA
RowB
将充分区分,以证明它们不是同一类


全班 上面提到的所有两个类别如下所示:

class DisplayGrid<T> where T : class  
{
    public List<T> allRows;

    public DisplayGrid()
    {
        this.allRows = new List<T>();
    }

    public bool hasRowMsgs 
    {
        get
        {
            bool blnRowsValid = true;
            foreach (T r in this.allRows)
            {
                if (!(r.isAValidRow))
                {
                    blnRowsValid = false;
                    break;
                }
            }
            return blnRowsValid;
        }
    }
    public bool isAValidTable
    {
        get {
            if (this.hasRowMsgs == false)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
class RowA
{
    public List<string> rowMsgs;

    public RowA()
    {
        rowMsgs = new List<string>();
    }

    public bool isAValidRow
    {
        get {
            return (this.rowMsgs.Count == 0) ? true : false;
        }
    }
}
class RowB
{
    public List<string> rowMsgs;

    public RowB()
    {
        rowMsgs = new List<string>();
    }

    public bool isAValidRow
    {
        get {
            return (this.rowMsgs.Count == 0) ? true : false;
        }
    }
}

class显示网格,其中T:class
{
公开列出所有行;
公共显示网格()
{
this.allRows=新列表();
}
公共图书馆
{
得到
{
bool blnRowsValid=true;
foreach(在这个.allRows中为tr)
{
如果(!(r.isAValidRow))
{
blnRowsValid=false;
打破
}
}
返回blnRowsValid;
}
}
公共布尔值是有效的
{
得到{
if(this.hasRowMsgs==false)
{
返回true;
}
其他的
{
返回false;
}
}
}
}
罗瓦级
{
公开名单;
公营机构
{
rowMsgs=新列表();
}
公共图书馆
{
得到{
返回(this.rowMsgs.Count==0)?真:假;
}
}
}
B类
{
公开名单;
公共资源组B()
{
rowMsgs=新列表();
}
公共图书馆
{
得到{
返回(this.rowMsgs.Count==0)?真:假;
}
}
}

如果将第一行更改为

class DisplayGrid<T> where T : Row  
类显示网格,其中T:行
那么它应该会起作用


说明:您尝试使用的方法是一行的属性;它不是“类”的方法,因此无法识别。

如果将第一行更改为

class DisplayGrid<T> where T : Row  
类显示网格,其中T:行
那么它应该会起作用


说明:您尝试使用的方法是一行的属性;它不是“类”的方法,因此不被识别。

在阅读并理解了Lee的评论的意义后,我现在有了一个工作版本。使其工作的关键是定义一个接口,
IRow
并将其应用于“Row”类、
RowA
RowB

为了将来的读者,这里有一些现在有用的课程

两点

  • 我在属性/方法中对前面的示例代码做了一些修改,使它们更加简洁,并解决了一个不相关的缺陷
  • RowA
    RowB
    是不现实的相似,这只是为了简单而简要地说明问题,显然在实际实现中,它们不太相似

类DisplayGrid,其中T:IRow
{
公开列出所有行;
公共显示网格()
{
this.allRows=新列表();
}
公共图书馆
{
得到
{
bool-blnHasRowMsgs=false;
foreach(在这个.allRows中为tr)
{
如果(!(r.isAValidRow))
{
blnHasRowMsgs=真;
打破
}
}
返回BLNHASROWMSG;
}
}
公共布尔值是有效的
{
得到{
返回(!(this.hasRowMsgs));
}
}
}
接口IRow
{
布尔·伊萨瓦利德罗{get;}
}
公共课罗华:伊罗
{
公开名单;
公共图书馆{
得到{
返回this.rowMsgs.Count>0?false:true;
}
}
公营机构
{
rowMsgs=新列表();
}
}
公共类RowB:IRow
{
公开名单;
公共图书馆{
得到{
返回this.rowMsgs.Count>0?false:true;
}
}
公共资源组B()
{
rowMsgs=新列表();
}
}

在阅读并理解了Lee的评论的意义之后,我现在有了一个工作版本。使其工作的关键是定义一个接口,
IRow
并将其应用于“Row”类、
RowA
RowB

为了将来的读者,这里有一些现在有用的课程

两点

  • 我在属性/方法中对前面的示例代码做了一些修改,使它们更加简洁,并解决了一个不相关的缺陷
  • RowA
    RowB
    是不现实的相似,这只是为了简单、简要、清晰地说明问题