Invoking generic method/delegate from a non-generic base class

LightStriker

I have the feeling I'm not looking at this issue from the right angle here, and I'm just not thinking of other solution.

Assuming this generic class;

public abstract class Port<T>
{
    public delegate T PullDelegate();

    private PullDelegate pull;

    public Port(PullDelegate pull)
    {
        this.pull = pull;
    }

    public T Pull()
    {
        return pull();
    }
}

It is used to define "ports" in a graph-node editor. A port can transfer a object/value from one node to another, but they also need to be "type-safe", which means I cannot plug any port to another of the wrong type (at least not without some conversion).

The node "owns" a port and give it a delegate towards one of its method so when another node "pull" on a value, the port simply invokes it and returns the proper value.

My issue starts when I'm trying to call Pull() from a non-generic collection. Obviously, I could make a non-generic base method, but then Pull could not return T, it would need to return object.

Also, each nodes have collection accessors for their ports so the other items can get them. That collect has to be non-generic because a node can have many ports of many type.

    public abstract Port[] Inputs { get; }
    public abstract Port[] Outputs { get; }
    public abstract Port[] Entries { get; }
    public abstract Port[] Exits { get; }

The moment the non-generic type get into play, everything generic become inaccessible. If only Port<>[] would be a thing.

I'm feeling like I'm missing something...

jgauffin

Make Port<T> implement the non-generic interface IPort using explicit implementations. In that way it's hidden from the API but still allow you to invoke methods in the generic classes.

public interface IPort
{
    object SomeAction(object data);
}

public class Port<T> : IPort
{
    //[.. all other methods ..]


    object IPort.SomeAction(object data)
    {
        var typedData = (T)data;
        //invoke our typed version.
        return SomeAction(data);
    }

    public T SomeAction(T data)
    {
         // ...
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Casting non generic class to generic base class

From Dev

MongoDB C# driver type discriminators with generic class inheriting from non-generic base class

From Dev

define a generic property in non-generic base class

From Dev

Use generic base class in a collection of classes derived from generic

From Dev

Infer generic parameter from base class

From Dev

Overriding generic methods from base class

From Dev

Typescript: Return "this" from generic base class

From Dev

C# Generic Method from Base Class

From Dev

Mock async generic method from base class

From Dev

Generic Base Class Overriding Non-Generic Base Class Function Pattern? (.NET)

From Dev

Will work these code the same (Generic Class with base class constraint vs Non-generic Class "counterpart")

From Dev

Generic Entity Base Class

From Dev

casting to base generic class

From Dev

Generic base class constructor

From Dev

Generic class method on non-generic class

From Dev

Assigning class that inherits from a constrained generic base class to the inherited base class with the constrained generic

From Dev

Invoking generic method from Main using reflection

From Dev

C# cast non-generic class to generic base class regardless of type T

From Dev

Casting a method to generic and non generic class

From Dev

generic method in a non-generic class

From Dev

Register generic interface with non generic class

From Java

Generic Method in Non-Generic Class

From Java

Generic instance variable in non-generic class

From Dev

Generic method inside non-generic class

From Dev

Pattern for exposing non generic and generic Class

From Dev

Non-generic constructor in generic class in java

From Dev

C++: Non generic method in generic class?

From Dev

Method for any class derived from a generic base class

From Dev

Use generic class for generic parameter base with out generic parameter

Related Related

  1. 1

    Casting non generic class to generic base class

  2. 2

    MongoDB C# driver type discriminators with generic class inheriting from non-generic base class

  3. 3

    define a generic property in non-generic base class

  4. 4

    Use generic base class in a collection of classes derived from generic

  5. 5

    Infer generic parameter from base class

  6. 6

    Overriding generic methods from base class

  7. 7

    Typescript: Return "this" from generic base class

  8. 8

    C# Generic Method from Base Class

  9. 9

    Mock async generic method from base class

  10. 10

    Generic Base Class Overriding Non-Generic Base Class Function Pattern? (.NET)

  11. 11

    Will work these code the same (Generic Class with base class constraint vs Non-generic Class "counterpart")

  12. 12

    Generic Entity Base Class

  13. 13

    casting to base generic class

  14. 14

    Generic base class constructor

  15. 15

    Generic class method on non-generic class

  16. 16

    Assigning class that inherits from a constrained generic base class to the inherited base class with the constrained generic

  17. 17

    Invoking generic method from Main using reflection

  18. 18

    C# cast non-generic class to generic base class regardless of type T

  19. 19

    Casting a method to generic and non generic class

  20. 20

    generic method in a non-generic class

  21. 21

    Register generic interface with non generic class

  22. 22

    Generic Method in Non-Generic Class

  23. 23

    Generic instance variable in non-generic class

  24. 24

    Generic method inside non-generic class

  25. 25

    Pattern for exposing non generic and generic Class

  26. 26

    Non-generic constructor in generic class in java

  27. 27

    C++: Non generic method in generic class?

  28. 28

    Method for any class derived from a generic base class

  29. 29

    Use generic class for generic parameter base with out generic parameter

HotTag

Archive