Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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#_Asp.net Mvc_Interface_Entity Framework 6 - Fatal编程技术网

C# 获取接口列表

C# 获取接口列表,c#,asp.net-mvc,interface,entity-framework-6,C#,Asp.net Mvc,Interface,Entity Framework 6,我不熟悉界面,但我正试图强迫自己学习如何使用它们。我对如何选择实现接口的对象列表感到困惑。这是我目前掌握的要点: public class Planet { public ICollection<Structure> Structures {get;set;} public ICollection<Ship> Ships {get;set;} public int GatherRate {get;set} public void GetA

我不熟悉界面,但我正试图强迫自己学习如何使用它们。我对如何选择实现接口的对象列表感到困惑。这是我目前掌握的要点:

public class Planet 
{
    public ICollection<Structure> Structures {get;set;}
    public ICollection<Ship> Ships {get;set;}
    public int GatherRate {get;set}
    public void GetAllResourceGatherers
    {
        var resourceGatherers = Planet.Ships.OfType<IResourceGatherer>()
                                                .ToList();

        //the below line doesn't work. I want to be able to get a list of all 
        //ships and structures that implement IResourceGatherer.    
        resourceStorers.AddRange(Structures.OfType<IResourceGatherer>()
                                               .ToList());

        foreach (var gatherer in resourceGatherers)
        {
            GatherRate += gatherer.GatherRate;
        }
    }
}
public class Ship
{
    //Stuff that all ships should have such as health, speed, cost, etc
}
public class Structure
{
    //Stuff that all structures should have such as buildtime, cost, etc
}
public class ResourceStructure : Structure, IResourceGatherer
{
    //implement interface
}
public class ResourceShip : Ship, IResourceGatherer
{
    //implement interface
}
interface IResourceGatherer
{
    int GatherRate{get;set;}
}
公共级行星
{
公共ICollection结构{get;set;}
公共ICollection{get;set;}
public int GatherRate{get;set}
公共无效GetAllResourceGathers
{
var resourceGatherers=Planet.Ships.OfType()
.ToList();
//下面这行行不通。我想能够得到一个所有的列表
//实现IResourceGatherer的发布和结构。
AddRange(Structures.OfType())
.ToList());
foreach(resourceGatherers中的var gatherer)
{
GatherRate+=gatherer.GatherRate;
}
}
}
公船
{
//所有船只都应该拥有的东西,比如健康、速度、成本等等
}
公共阶级结构
{
//所有结构都应该具备的东西,如构建时间、成本等
}
公共类资源结构:结构,IResourceGatherer
{
//实现接口
}
公共类资源船:船,IResourceGatherer
{
//实现接口
}
接口IResourceGatherer
{
int GatherRate{get;set;}
}
有更好的方法吗


另外,我在这个项目中使用的是实体框架6和MVC4。

好吧,您正在尝试从
船舶
星球
的属性获取
IResourceGather
接口实现者。请注意,属性的类型为
ICollection
。现在告诉我,
Ship
是否实现了
IResourceStorer

我打赌不会。这就是为什么会得到空的select结果

根据最新消息:
那么,您正试图从
星球的
属性的
结构中获取
IResourceGather
接口实现者。请注意,属性的类型为
ICollection
。现在告诉我,
Structure
是否实现了
IResourceStorer

我打赌不会。这就是为什么会得到空的select结果


可以具有非空结果的唯一选项是将一些扩展装运添加到列表中:

Structures.Add(new ResourceStructure());

也许是这样的:

var resourceGatherers = new List<IResourceGatherer>();

var gathererShips = Planet.Ships.OfType<IResourceGatherer>().ToList();

var gathererStructures = Structures.OfType<IResourceGatherer>().ToList();

resourceGatherers.AddRange(gathererShips);
resourceGatherers.AddRange(gathererStructures);
var resourceGatherers=new List();
var gathererShips=Planet.Ships.OfType().ToList();
var gatherstructures=Structures.OfType().ToList();
ResourceGathers.AddRange(Gatherships);
resourceGatherers.AddRange(gathererStructures);

哇!我在我的例子中犯了一个错误。它应该是结构物,而不是船。我已经修复了上面的代码,请您再看一眼好吗?当您说“不工作”时,您的确切意思是什么?iis ResourceStorer是在哪里定义的?