Advertisement
Guest User

Finding generic method with parameter type

a guest
May 27th, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1. using System;
  2. using System.Reflection;
  3.  
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5.  
  6. namespace tacGenericTest
  7. {
  8.     [TestClass]
  9.     public class GetMethodTest
  10.     {
  11.         [TestMethod]
  12.         public void FindMethodTest()
  13.         {
  14.             var methodInfo = typeof (GenericMethodContainer).GetMethod("FindOne", BindingFlags.Static | BindingFlags.Public, null, new []{typeof(object)}, null);
  15.             Assert.IsNotNull(methodInfo);
  16.             Assert.AreEqual("FindOne", methodInfo.Name);
  17.             Assert.AreEqual("DateTime", methodInfo.MakeGenericMethod(typeof(DateTime)).Invoke(null, new object[] {null}));
  18.         }
  19.     }
  20.  
  21.     public class GenericMethodContainer
  22.     {
  23.         public static string FindOne<T>(string arg)
  24.         {
  25.             return typeof(T).Name;
  26.         }
  27.  
  28.         public static string FindOne<T>(object arg)
  29.         {
  30.             return typeof(T).Name;
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement