简体   繁体   English

非泛型类列表上的C#调用方法

[英]C# Call method on non-generic list of generic classes

My main class Computer interacts with a number of external systems and currently has the logic for getting/updating their info crammed into it. 我的主班Computer与许多外部系统交互,并且目前具有将其信息塞入/更新到其中的逻辑。

I want to move the logic for each system to it's own separate class and i've been able to part of the way there but i'm having trouble calling a method on them. 我想将每个系统的逻辑移到它自己的单独的类中,并且我已经能够部分解决该问题,但是我在对它们调用方法时遇到了麻烦。

I've only just recently started playing around with generics so this is probably badly written code, apologies for your eyes in advance. 我只是最近才开始使用泛型,所以这可能是编写不好的代码,请您提前道歉。

These are my System classes 这些是我的System

public class System
{
    public string Name { get;set; }
}

public class System<T> : System
{
    public virtual T GetData() => default(T);
    public virtual void UpdateData() {}
}

public interface ISystem<T>
{
    T GetData();
    void UpdateData();
}       

These are the systems i've created using the System classes: 这些是我使用System类创建的System

public class ComplexSystem : ISystem<RegistryKey>
{
    public string GetData() => MethodThatGetsRegistryData();
    public bool UpdateData() => MethodThatUpdatesRegistry();
}

public class IntSystem : ISystem<int>
{
    private int value = 9;
    public int GetData() => this.value;
    public void UpdateData()
    {
        this.value = 3;
    }
}

public class StringSystem : ISystem<string>
{
    private string value = "hello";
    public string GetData() => this.value;
    public void UpdateData()
    {
        this.value = "updated";
    }
}

and this is how i'm trying to use the whole thing: 这就是我试图使用整个事情的方式:

public class Computer
{   
    public List<System> AssociatedSystems = new List<System>();

    public Computer()
    {
        this.AssociatedSystems.Add(new System<ComplexSystem> {Name="ComplexSystem"});
        this.AssociatedSystems.Add(new System<IntSystem> {Name="IntSystem"});
        this.AssociatedSystems.Add(new System<StringSystem> {Name="StringSystem"});
    }

    public void UpdateDataWhenComputerIsChanged()
    {           
        // is it possible to loop through them and call UpdateData on each one without know what generic they are?
        foreach (var associatedSystem in this.AssociatedSystems)
        {
            // something like...
            associatedSystem.UpdateData();
        }
    }
}

Which results in 导致

System does not contain a definition for UpdateData() and no extension method could be found 系统不包含UpdateData()的定义,并且找不到扩展方法

I'm not married to the code above so if there's a better way of doing it i'm all for learning. 我不喜欢上面的代码,因此,如果有更好的方法,我将全力以赴。 I'm essentially looking for a way to have a list of classes that contain data logic (like GetData() and UpdateData() ) so i don't have to put that logic into my main class ( Computer ) 我本质上是在寻找一种包含数据逻辑的类列表的方法(例如GetData()UpdateData() ),因此我不必将该逻辑放入我的主类( Computer

So function UpdateData does not exist in System and that is why you cant call it. 因此,函数UpdateDataSystem中不存在,这就是为什么您不能调用它的原因。 I would suggest introducing an ISystem interface and put Name (if you need it) and UpdateData in that and let the generic ISystem<T> interface inherit from that. 我建议引入一个ISystem接口,并在其中放入Name (如果需要)和UpdateData ,并让通用ISystem<T>接口从该接口继承。

public interface ISystem
{
    string Name {get;set;}
    void UpdateData();
}

public interface ISystem<T> : ISystem
{
    T GetData();
}

public class Computer
{
    public List<ISystem> AssociatedSystems = new List<ISystem>();
    .....
    foreach (var associatedSystem in this.AssociatedSystems)
    {
        associatedSystem.UpdateData();
    }
}

Yes there are multiple ways to do this. 是的,有多种方法可以做到这一点。 The ones that I can think of are: 我能想到的是:

  1. Type checking each System in the loop 类型检查循环中的每个System
  2. Adding a Type property to system 向系统添加Type属性
  3. Adding a UpdateData(object) or UpdateData(SomeHighLevelType) 添加一个UpdateData(object)UpdateData(SomeHighLevelType)

If you need more help please specify the nature of your systems. 如果您需要更多帮助,请指定系统的性质。 Are they limited to some specific types or you want is extendable for ever and how much speed critical is your project and I can elaborate more 它们是否仅限于某些特定类型,或者您想要永久扩展,并且您的项目对速度至关重要?我可以详细说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM