How to distinguish MethodBase in generics
Asked Answered
H

2

2

I have a cache based on

Dictionary<MethodBase, string>

The key is rendered from MethodBase.GetCurrentMethod. Everything worked fine until methods were explicitly declared. But one day it is appeared that:

Method1<T>(string value)

Makes same entry in Dictionary when T gets absolutely different types.

So my question is about better way to cache value for generic methods. (Of course I can provide wrapper that provides GetCache and equality encountered generic types, but this way doesn't look elegant).

Update Here what I exactly want:

static Dictionary<MethodBase, string> cache = new Dictionary<MethodBase, string>();
static void Method1<T>(T g) 
{
    MethodBase m1 = MethodBase.GetCurrentMethod();
    cache[m1] = "m1:" + typeof(T);
}
public static void Main(string[] args)
{
    Method1("qwe");
    Method1<Stream>(null);
    Console.WriteLine("===Here MUST be exactly 2 entry, but only 1 appears==");
    foreach(KeyValuePair<MethodBase, string> kv in cache)
        Console.WriteLine("{0}--{1}", kv.Key, kv.Value);
}
Hypothetical answered 21/12, 2009 at 14:34 Comment(2)
Do you want a different cache entry for each set of type parameters or a different cache entry for each physical piece of code (my answer)?Cupric
This appears to be impossible.Cupric
B
1

Use MakeGenericMethod, if you can:

using System;
using System.Collections.Generic;
using System.Reflection;

class Program
{
    static Dictionary<MethodBase, string> cache = new Dictionary<MethodBase, string>();

    static void Main()
    {
        Method1(default(int));
        Method1(default(string));
        Console.ReadLine();
    }

    static void Method1<T>(T g)
    {
        var m1 = (MethodInfo)MethodBase.GetCurrentMethod();
        var genericM1 = m1.MakeGenericMethod(typeof(T)); // <-- This distinguishes the generic types
        cache[genericM1] = "m1:" + typeof(T);
    }
}
Bluebonnet answered 1/10, 2013 at 14:5 Comment(1)
it is very old question, but I've just spent some time to evaluate it. Looks really good!Hypothetical
K
1

This is not possible; a generic method has a single MethodBase; it doesn't have one MethodBase per set of generic arguments.

Kerianne answered 21/12, 2009 at 23:18 Comment(0)
B
1

Use MakeGenericMethod, if you can:

using System;
using System.Collections.Generic;
using System.Reflection;

class Program
{
    static Dictionary<MethodBase, string> cache = new Dictionary<MethodBase, string>();

    static void Main()
    {
        Method1(default(int));
        Method1(default(string));
        Console.ReadLine();
    }

    static void Method1<T>(T g)
    {
        var m1 = (MethodInfo)MethodBase.GetCurrentMethod();
        var genericM1 = m1.MakeGenericMethod(typeof(T)); // <-- This distinguishes the generic types
        cache[genericM1] = "m1:" + typeof(T);
    }
}
Bluebonnet answered 1/10, 2013 at 14:5 Comment(1)
it is very old question, but I've just spent some time to evaluate it. Looks really good!Hypothetical

© 2022 - 2024 — McMap. All rights reserved.