C# Call method on non-generic list of generic classes

Bluecakes

My main class Computer interacts with a number of external systems and currently has the logic for getting/updating their info crammed into it.

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

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:

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

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)

Magnus

So function UpdateData does not exist in System and that is why you cant call it. 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.

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();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

call generic method from non generic method

From Dev

C++: Non generic method in generic class?

From Java

Java call non-generic method from generic method

From Dev

C# - List of generic classes

From Dev

Call generic method in c#

From Dev

C# call generic interface method from non generic parent interface without dynamic

From Dev

Non-generic method call in Generic class showing "type not valid"

From Dev

Calling non-generic method from generic method in C#

From Dev

How to invoke generic method with a list of non-generic inputs

From Dev

Call method based on the type of the generic list element

From Java

Generic call a method in different classes, but with the same method signature

From Dev

Call a method accepting TX as a generic type from another method with Generic Type T having List(L), with the type of (L) in C#

From Dev

C# generic method that receives List<T> doesn't call overloaded method for actual type of T (prefers generic one)

From Dev

C# Extend Generic Class in Method call

From Dev

C# emit call to generic method

From Dev

c# Call method for generic types

From Java

create a list of generic classes

From Dev

Unique classes in generic list

From Dev

Call object method of generic

From Dev

Java generic method call

From Dev

How to call a non-generic method of a class T from a method of a generic class?

From Dev

C# Working with a generic list in a method

From Dev

Casting a method to generic and non generic class

From Dev

Generic class method on non-generic class

From Dev

generic method in a non-generic class

From Java

Generic Method in Non-Generic Class

From Dev

How to wrap a generic method into a non generic one?

From Dev

Generic Method in Non-Generic Interface

From Dev

Generic method inside non-generic class

Related Related

  1. 1

    call generic method from non generic method

  2. 2

    C++: Non generic method in generic class?

  3. 3

    Java call non-generic method from generic method

  4. 4

    C# - List of generic classes

  5. 5

    Call generic method in c#

  6. 6

    C# call generic interface method from non generic parent interface without dynamic

  7. 7

    Non-generic method call in Generic class showing "type not valid"

  8. 8

    Calling non-generic method from generic method in C#

  9. 9

    How to invoke generic method with a list of non-generic inputs

  10. 10

    Call method based on the type of the generic list element

  11. 11

    Generic call a method in different classes, but with the same method signature

  12. 12

    Call a method accepting TX as a generic type from another method with Generic Type T having List(L), with the type of (L) in C#

  13. 13

    C# generic method that receives List<T> doesn't call overloaded method for actual type of T (prefers generic one)

  14. 14

    C# Extend Generic Class in Method call

  15. 15

    C# emit call to generic method

  16. 16

    c# Call method for generic types

  17. 17

    create a list of generic classes

  18. 18

    Unique classes in generic list

  19. 19

    Call object method of generic

  20. 20

    Java generic method call

  21. 21

    How to call a non-generic method of a class T from a method of a generic class?

  22. 22

    C# Working with a generic list in a method

  23. 23

    Casting a method to generic and non generic class

  24. 24

    Generic class method on non-generic class

  25. 25

    generic method in a non-generic class

  26. 26

    Generic Method in Non-Generic Class

  27. 27

    How to wrap a generic method into a non generic one?

  28. 28

    Generic Method in Non-Generic Interface

  29. 29

    Generic method inside non-generic class

HotTag

Archive