C#当类的类型为泛型时,如何访问类的元素?
可能重复:C#当类的类型为泛型时,如何访问类的元素?,c#,generics,types,comparison,compare,C#,Generics,Types,Comparison,Compare,可能重复: 我有一个通用函数 class Something { public int found; public Something() { this.found = true; } } List<Something> something; public int countFound<T>(List<T> Organisms) { if (typeof(T) == typeof(Something))
我有一个通用函数
class Something {
public int found;
public Something() {
this.found = true;
}
}
List<Something> something;
public int countFound<T>(List<T> Organisms)
{
if (typeof(T) == typeof(Something))
{
foreach (T organism in Organisms)
{
// here i want to check something like organism.found == true how do i do it?
}
}
return 0;
}
分类{
找到公共int;
公开某事{
this.found=true;
}
}
列出某事;
找到公共整数计数(列出生物)
{
如果(typeof(T)=typeof(某物))
{
foreach(生物体中的T生物体)
{
//这里我想检查一些东西,比如Organic.found==true,我该怎么做?
}
}
返回0;
}
提前感谢所有的帮助 必须将泛型限制为一个(或多个)接口,该接口指示实现泛型所需的属性 假设接口IFound实现了要检查的属性:
public int countFound<T>(List<T> Organisms) where T : IFound
{
if (typeof(T) == typeof(Something))
{
foreach (T organism in Organisms)
{
if(organism.found)) // done because IFound tells T has a property with this name
}
}
return 0;
}
您的类必须实现以下内容:
class Something : IFound
{
public bool Found
{
get { return true; } // implement your condition in a method called here
}
}
然后,您可以根据需要调用您的方法:
int a = countFound<Something>(List<Something> parameter);
int a=countFound(列表参数);
必须将泛型限制为一个(或多个)接口,该接口指示实现泛型所需的属性
假设接口IFound实现了要检查的属性:
public int countFound<T>(List<T> Organisms) where T : IFound
{
if (typeof(T) == typeof(Something))
{
foreach (T organism in Organisms)
{
if(organism.found)) // done because IFound tells T has a property with this name
}
}
return 0;
}
您的类必须实现以下内容:
class Something : IFound
{
public bool Found
{
get { return true; } // implement your condition in a method called here
}
}
然后,您可以根据需要调用您的方法:
int a = countFound<Something>(List<Something> parameter);
int a=countFound(列表参数);
在您的场景中,您似乎不想尝试实现相等功能,因为相等总是在您正在比较的类型的上下文中定义的(每个类型的特定代码用于进行比较)。如果所有的T
都是公共类型(基类),那么这将对您有效相等条件可以用基类的公共属性等表示。在您的场景中,您似乎不想尝试实现相等函数,因为相等总是在您正在比较的类型的上下文中定义的(每个类型的特定代码用于进行比较)。如果所有的T
都是一个公共类型(基类),并且相等条件可以用基类的公共属性等表示,那么这将对您有效。您可能需要这样的内容:
class Organism
{
public bool Found
{
get;
set;
}
}
class Something : Organism
{
public Something()
{
this.Found = true;
}
}
public class Program
{
public int countFound<T>(List<T> Organisms)
where T : Organism
{
foreach (T organism in Organisms)
{
if (organism.Found)
{
// Do something with the organism
}
}
return 0;
}
}
类生物
{
公共布尔发现
{
得到;
设置
}
}
类别:有机体
{
公开某事
{
这个.Found=true;
}
}
公共课程
{
找到公共整数计数(列出生物)
其中T:生物体
{
foreach(生物体中的T生物体)
{
如果(发现生物体)
{
//对有机体做点什么
}
}
返回0;
}
}
这里的要点是:
- 您有一个名为Organic的公共基类,它定义了Found属性
- Something类派生自有机体,在构造时设置为true
- CountFound方法在T上有一个泛型约束(where子句),指定它必须派生自有机体(Something满足此条件)。这样,您就可以使用Organic在方法中提供的任何方法或属性—在本例中为Organic.Found
class Organism
{
public bool Found
{
get;
set;
}
}
class Something : Organism
{
public Something()
{
this.Found = true;
}
}
public class Program
{
public int countFound<T>(List<T> Organisms)
where T : Organism
{
foreach (T organism in Organisms)
{
if (organism.Found)
{
// Do something with the organism
}
}
return 0;
}
}
类生物
{
公共布尔发现
{
得到;
设置
}
}
类别:有机体
{
公开某事
{
这个.Found=true;
}
}
公共课程
{
找到公共整数计数(列出生物)
其中T:生物体
{
foreach(生物体中的T生物体)
{
如果(发现生物体)
{
//对有机体做点什么
}
}
返回0;
}
}
这里的要点是:
- 您有一个名为Organic的公共基类,它定义了Found属性
- Something类派生自有机体,在构造时设置为true
- CountFound方法在T上有一个泛型约束(where子句),指定它必须派生自有机体(Something满足此条件)。这样,您就可以使用Organic在方法中提供的任何方法或属性—在本例中为Organic.Found
countFound
函数必须采用所有类型T
,但当T
是(或继承自)某物时,需要特殊情况,则可以使用以下方法:
public int countFound<T>(List<T> Organisms)
{
if (typeof(T) == typeof(Something) || typeof(T).IsSubclassOf(typeof(Something)))
{
foreach (T organism in Organisms)
{
Something s = (Something)(object)organism;
// do whatever you like with s
}
}
return 0;
}
根据您希望函数执行的操作,此处有两个选项:
如果countFound
函数必须采用所有类型T
,但当T
是(或继承自)某物时,需要特殊情况,则可以使用以下方法:
public int countFound<T>(List<T> Organisms)
{
if (typeof(T) == typeof(Something) || typeof(T).IsSubclassOf(typeof(Something)))
{
foreach (T organism in Organisms)
{
Something s = (Something)(object)organism;
// do whatever you like with s
}
}
return 0;
}
我认为title是误导性的,问题是如何处理泛型类型方法来访问某些属性。让我说,如果你在泛型类型参数上进行分支,你所做的可能不是很一般。我认为title是误导性的,问题是如何处理泛型类型方法以访问某些属性。我想说的是,如果您在泛型类型参数上进行分支,您所做的可能不是很泛型。什么是IFound?请参阅我的编辑。你可以选择任何你认为合适的名字。此外,您希望与countFound()方法一起使用的每个类都必须实现这一点,也就是说您的接口。接口上不能有字段。但更重要的是,@softplaza应该让它成为一处房产。不要公开公共字段。现在如何使用countFound函数?我怎么称呼它?我发现(某物)对我不起作用me@Martinho费尔南德斯:是的,我指的是一个接口,但没有正确记住语法;)@softplaza I进一步更新了示例。请阅读界面的主题。什么是IFound对我不起作用?请参阅我的编辑。你可以选择任何你认为合适的名字。每节课你也一样