Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve name of a generic method, including generic types names

In C#, I have a method with the following signature :

List<T> Load<T>(Repository<T> repository) 

Inside Load() method, i'd like to dump full method name (for debugging purposes), including the generic type. eg : calling Load<SomeRepository>(); would write "Load<SomeRepository>"

What i have try so far : using MethodBase.GetCurrentMethod() and GetGenericArguments() to retrieve information.

List<T> Load<T>(Repository<T> repository) 
{
   Debug.WriteLine(GetMethodName(MethodBase.GetCurrentMethod()));
}

string GetMethodName(MethodBase method)
{
     Type[] arguments = method.GetGenericArguments();
     if (arguments.Length > 0)
        return string.Format("{0}<{1}>", 
          method.Name, string.Join(", ", arguments.Select(x => x.Name)));
     else
        return method.Name;
}

Retrieving method name works, but for generic parameter it always return me "T". Method returns Load<T> instead of Load<SomeRepository> (which is useless)

I have tried to call GetGenericArguments() outside GetMethodName() and provide it as argument but it doesn't help.

I could provide typeof(T) as a parameter of GetMethodName() (it will works) but then it will be specific to number of generic types eg : with Load<T, U> it would not work anymore, unless I provide the other argument.

like image 769
tigrou Avatar asked Oct 01 '13 09:10

tigrou


People also ask

What are generic methods generic methods are the methods defined in a generic class?

Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.

How do you declare a generic method How do you invoke a generic method?

Generic MethodsAll generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example). Each type parameter section contains one or more type parameters separated by commas.

Which of these is correct way of defining a generic method?

4. Which of these is an correct way of defining generic method? Explanation: The syntax for a generic method includes a type parameter, inside angle brackets, and appears before the method's return type. For static generic methods, the type parameter section must appear before the method's return type.


1 Answers

The answer of Jeppe Stig Nielsen is correct in terms of your requirements. In fact, your solution returns T and his returns the runtime type name. If you ask for something different, then try to rewrite your question. The below is another solution for one generic item:

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        Load(new Repository<int>());
        Load(new Repository<string>());
        Console.ReadLine();
    }

    class Repository<T> { }

    static List<T> Load<T>(Repository<T> repository)
    {
        Console.WriteLine("Debug: List<{1}> Load<{1}>({0}<{1}> repository)", typeof(Repository<T>).Name, typeof(Repository<T>).GenericTypeArguments.First());
        return default(List<T>);
    }
}

Here is the output that you asked for:

enter image description here

like image 76
Ryszard Dżegan Avatar answered Sep 22 '22 23:09

Ryszard Dżegan