Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.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#通用表_C#_Generics - Fatal编程技术网

C#通用表

C#通用表,c#,generics,C#,Generics,我有多个类,它们有一个名为“rows”的fled,但是rows字段对于每个表是不同的类类型 在下面的例子中,我有一张鱼桌和一张鸟桌。table类本质上是相同的,但是rows字段是特定的类类型,这使得人们很容易引用该对象上的属性 三十、 行[0]。如果canfly是一只鸟,它将很容易被引用 所以现在我正在尝试编写一个通用方法,可以获取FishTable或BirdTable的多个实例。。做一些魔术,将它们合并到一个该类型的表中并返回结果。该方法需要知道的只是我们所讨论的表的类型和一些其他基本参数 我

我有多个类,它们有一个名为“rows”的fled,但是rows字段对于每个表是不同的类类型

在下面的例子中,我有一张鱼桌和一张鸟桌。table类本质上是相同的,但是rows字段是特定的类类型,这使得人们很容易引用该对象上的属性

三十、 行[0]。如果canfly是一只鸟,它将很容易被引用

所以现在我正在尝试编写一个通用方法,可以获取FishTable或BirdTable的多个实例。。做一些魔术,将它们合并到一个该类型的表中并返回结果。该方法需要知道的只是我们所讨论的表的类型和一些其他基本参数

我知道我做得不对,但我很清楚我在哪里跌倒了

10次中有9次我遇到这样的事情:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0311  The type 'GenericTest.FishTable<GenericTest.fish>' cannot be 
used as type parameter 'T' in the generic type or method 'Tester.test<T>()'. 
There is no implicit reference conversion from 
'GenericTest.FishTable<GenericTest.fish>' to 
'GenericTest.ITable<GenericTest.FishTable<GenericTest.fish>>'.  GenericTest  
C:\Users\WarrickF\source\repos\EOSTools\GenericTest\Program.cs  14  Active
严重性代码描述项目文件行抑制状态
错误CS0311“GenericTest.FishTable”类型不能为空
用作泛型类型或方法“Tester.test()”中的类型参数“T”。
不存在来自的隐式引用转换
“GenericTest.FishTable”到
“GenericTest.ITable”。一般测试
C:\Users\WarrickF\source\repos\EOSTools\GenericTest\Program.cs 14处于活动状态
我知道我需要去真正理解泛型,因为我真的没有必要在没有血统理解的情况下编写这样的泛型,但是。。这是我正在研究的一个真实的例子

interface ITable<T>
{
    List<T> rows { get; set; }
}




public class BirdTable<T> : ITable<T>
{
    public List<T> rows { get; set; }
}

public class FishTable<T> : ITable<T>
{
    public List<T> rows { get; set; }
}


public abstract class animal {
    public int eyeCount;
}

public class bird : animal
{
    public int featherCount;
    public bool canFly;
}

public class fish : animal
{
    public int numberOfFins;
    public bool depth;
}
interface-ITable
{
列出行{get;set;}
}
公共类鸟类:ITable
{
公共列表行{get;set;}
}
公共类FishTable:ITable
{
公共列表行{get;set;}
}
公共抽象类动物{
公众眼睛计数;
}
公营鸟类:动物
{
公共整数羽毛计数;
公共图书馆能飞;
}
公营鱼类:动物
{
公共国际号码;
公共布尔深度;
}

切坦·兰帕里亚的评论是正确的。无论如何,我可以试着帮点忙,但这部分是猜测。您希望合并多个表,其中的行类型为
T
,但错误消息似乎表明您的代码在某个地方需要单独的
T
s

There is no implicit reference conversion from 
'GenericTest.FishTable<GenericTest.fish>' to 
'GenericTest.ITable<GenericTest.FishTable<GenericTest.fish>>'.

您还需要共享有错误的代码。谢谢您,先生。虽然我没能提供所有的代码(已经晚了),但你的回答是完美的。当你看到答案时,答案总是那么简单。。。然而我却把自己搞得一团糟。再次感谢你!很高兴我能帮忙。但我花了一段时间才把这些弄对,别着急(如果你在冒汗的话)。我并不是要阻止您使用接口或类族,顺便说一句,它们只是没有很好地与泛型混合。
class Program
{
    static string _animal;

    static void Main(string[] args) {
        TestTable<Bird> birds1 = new TestTable<Bird>();
        birds1.Rows.Add(new Bird());
        birds1.Rows.Add(new Bird());

        TestTable<Bird> birds2 = new TestTable<Bird>();
        birds2.Rows.Add(new Bird());
        birds2.Rows.Add(new Bird());

        TestTable<Bird> allBirds = MergeTestTables<Bird>(birds1, birds2);

        int howManyBirds = allBirds.Rows.Count;

        Console.WriteLine($"There are { howManyBirds } { _animal }s.");
        Console.ReadKey(true);
    }

    public static TestTable<T> MergeTestTables<T>(params TestTable<T>[] tables) where T : Animal {
        TestTable<T> merged = new TestTable<T>();

        _animal = typeof(T).Name;
        _animal = _animal.ToLower();

        foreach (TestTable<T> table in tables) {
                foreach (T row in table.Rows) {
                    merged.Rows.Add(row);
                }
        }

        return merged;
    }

    public class TestTable<T> where T : Animal
    {
        public List<T> Rows { get; set; } = new List<T>();
    }

    public abstract class Animal
    {
        public int EyeCount { get; set; }
    }

    public class Bird : Animal
    {
        public int FeatherCount { get; set; }
        public bool CanFly { get; set; }
    }

    public class Fish : Animal
    {
        public int NumberOfFins { get; set; }
        public bool Depth { get; set; }
    }

}