Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

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

$
0
0
	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?


Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>