Calling non-generic method from generic method in C#

fnzr

This is similar to this question Call overloaded generic method from generic method but it's not the same: in that case, the method returned void, while I need to return the generic type T, so the proposed solution does not work.

I'm implementing a tuple-like class which has generic parameters and accessors for these parameters:

public class MyClass<T1, T2>
{
    private readonly T1 _item1;
    private readonly T2 _item2;

    public MyClass(T1 item1, T2 item2)
    {
        _item1 = item1;
        _item2 = item2;
    }

    public T1 GetItem(T1 _) => _item1;

    public T2 GetItem(T2 _) => _item2;

    private T GetItem<T>(T x)
    {
        throw new ArgumentException();
        // return x;
    }

    public T Get<T>()
    {
        return GetItem(default(T));
    }
}

I would like to use this class like this:

var obj = new MyClass<int, string>(1, "hello");
var a = obj.Get<int>();
var b = obj.Get<string>();

But if I try this, the Generic T GetItem<T>(T x) is always called (the exception is thrown), and not the non-generic implementations.

If I try to access the GetItem method directly, it works as intended:

var obj = new MyClass<int, string>(1, "hello");
var q = obj.GetItem(default(int));
var w = obj.GetItem(default(string));

q and w contain 1 and hello as expected.

Is there a way for me to use this class as I want (using obj.Get<T>)? I want to avoid Item1 Item2 Item3 getters.

Heinzi

But if I try this, the Generic T GetItem<T>(T x) is always called (the exception is thrown), and not the non-generic implementations.

That's because overload resolution happens at compile-time, and at compile-time, the concrete type of T is unknown.

Is there a way for me to use this class as I want (using obj.Get<T>)?

Using plain old dynamic type checking, sure:

public class MyClass<T1, T2>
{
    private readonly T1 _item1;
    private readonly T2 _item2;

    public MyClass(T1 item1, T2 item2)
    {
        _item1 = item1;
        _item2 = item2;
    }

    public T Get<T>()
    {
        if (typeof(T) == typeof(T1))
        {
            return (T)(object)_item1;
        } 
        
        if (typeof(T) == typeof(T2))
        {
            return (T)(object)_item2;
        }
        
        throw new InvalidOperationException();
    }
}

The advantage of this solution is that the behavior of the edge cases (T1 == T2, or T matches neither T1 nor T2) is properly defined and easy to see.


Alternatively, you can use the dynamic "hack" used in the question you linked to, you just need to cast the result to T: return (T)GetItem((dynamic)default(T));, here's a working fiddle.

But this is just plain ugly, please don't do that. Compare the fiddle with the version above and try to judge honestly which one is easier to read, easier to understand, and, thus, easier to maintain.

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

Calling generic method from viewmodel

From Dev

C++: Non generic method in generic class?

From Dev

Calling a MaybeNull generic method from another generic method

From Dev

Calling generic method with generic collection

From Dev

Calling c++ generic method from C#

From Java

Java call non-generic method from generic method

From Dev

Calling a generic method

From Java

Calling an overloaded java generic method from scala

From Dev

Calling a Generic Method from BaseEntity on unknown object

From Dev

Calling constructor from a subclass method with a generic parameter

From Dev

Calling a generic Java varargs method from Kotlin

From Dev

Calling a static method from a generic constraint Dart

From Dev

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

From Dev

How to dispatch to generic handler from a non-generic method?

From Dev

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

From Dev

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

From Dev

C# - Generic method calling method which returns concrete type?

From Dev

CLI/C++ Interface class, Generic method calling template method

From Dev

Calling method on generic type Dart

From Dev

Calling a generic method with interface instances

From Dev

Calling a Method that has Generic Parameters

From Dev

Generics - Calling a method with a generic parameter

From Dev

Calling a generic scala method in groovy

From Dev

MissingMethodException calling a method with generic parameter

From Dev

Calling generic method with Mono Cecil

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

Related Related

  1. 1

    call generic method from non generic method

  2. 2

    Calling generic method from viewmodel

  3. 3

    C++: Non generic method in generic class?

  4. 4

    Calling a MaybeNull generic method from another generic method

  5. 5

    Calling generic method with generic collection

  6. 6

    Calling c++ generic method from C#

  7. 7

    Java call non-generic method from generic method

  8. 8

    Calling a generic method

  9. 9

    Calling an overloaded java generic method from scala

  10. 10

    Calling a Generic Method from BaseEntity on unknown object

  11. 11

    Calling constructor from a subclass method with a generic parameter

  12. 12

    Calling a generic Java varargs method from Kotlin

  13. 13

    Calling a static method from a generic constraint Dart

  14. 14

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

  15. 15

    How to dispatch to generic handler from a non-generic method?

  16. 16

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

  17. 17

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

  18. 18

    C# - Generic method calling method which returns concrete type?

  19. 19

    CLI/C++ Interface class, Generic method calling template method

  20. 20

    Calling method on generic type Dart

  21. 21

    Calling a generic method with interface instances

  22. 22

    Calling a Method that has Generic Parameters

  23. 23

    Generics - Calling a method with a generic parameter

  24. 24

    Calling a generic scala method in groovy

  25. 25

    MissingMethodException calling a method with generic parameter

  26. 26

    Calling generic method with Mono Cecil

  27. 27

    Casting a method to generic and non generic class

  28. 28

    Generic class method on non-generic class

  29. 29

    generic method in a non-generic class

HotTag

Archive