none
How to call the correct generic method when conflictes occurs, and how to find generic method definition. RRS feed

  • Question

  • 	public class TestClass<T>
    	{
    		public void TestMethod(T t1)
    		{
    		}
    		public void TestMethod(int t1)
    		{
    		}
    
    		void Question()
    		{
    			
    			TestClass<string> tc1 = new TestClass<string>();
    			MethodInfo method1 = new Action<string>(tc1.TestMethod).Method;
    
    			TestClass<int> tc2 = new TestClass<int>();
    			MethodInfo method2 = new Action<int>(tc2.TestMethod).Method; 
    			MethodInfo method3 = new Func<MethodInfo, Type, MethodInfo>(delegate(MethodInfo genericMethod, Type genericArgument)
    			{
    				//If I got method2, is there any way to get method1 if I provides method2 and a type parameter of 'string'
    				return null;
    			})(method2, typeof(string));
    
    			tc2.TestMethod(1);//The method will call TestMethod(int t1), how to call TestMethod(T t1) instead.
    		}
    	}

    I have two question.

    Question 1:

    If I got a method info from a generic type, and a generic type argument, can I get the same method info of new generic type from original type's generic definition?

    Question 2:

    As you see, there is a conflicte in TestMethod if I call TestClass<int>'s method, how to call the correct method?

    Sunday, July 12, 2015 8:04 AM

Answers

  • Hi Sean,

    Questions 1

    The answer is you can’t. Because when generic argument is specified to a generic class. Compiler will generate a separate class for each argument. That means TestClass<int> and TestClass<string> are totally different class, of course you could not get the methondinfo based on the methodinfo from another class.

     

    Questions 2

    The only possible way I figured out is just to use the hash code of each methodinfo to identify the conflict method. And call the method via reflection. But I don't understand this design because it makes no sense.  Per my understanding and it also go against the purpose of the generic type design.

       

    I suggest you go to that article to understand the implementation of generic type. It might give you the answer to the questions.

    Reference link:

    http://research.microsoft.com/pubs/64031/designandimplementationofgenerics.pdf

    https://msdn.microsoft.com/en-us/library/ms172334(v=vs.110).aspx

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.





    Tuesday, July 14, 2015 2:27 AM

All replies

  • Hi Sean,

    Questions 1

    The answer is you can’t. Because when generic argument is specified to a generic class. Compiler will generate a separate class for each argument. That means TestClass<int> and TestClass<string> are totally different class, of course you could not get the methondinfo based on the methodinfo from another class.

     

    Questions 2

    The only possible way I figured out is just to use the hash code of each methodinfo to identify the conflict method. And call the method via reflection. But I don't understand this design because it makes no sense.  Per my understanding and it also go against the purpose of the generic type design.

       

    I suggest you go to that article to understand the implementation of generic type. It might give you the answer to the questions.

    Reference link:

    http://research.microsoft.com/pubs/64031/designandimplementationofgenerics.pdf

    https://msdn.microsoft.com/en-us/library/ms172334(v=vs.110).aspx

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.





    Tuesday, July 14, 2015 2:27 AM
  • Question 2 is a occasional idea when I wrote question 1.

    Thanks a lot!

    Monday, July 27, 2015 3:13 PM