Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are the answers to your questions along with an example:</p> <ol> <li><p>Yes, although there are two things really to be aware of here with generic methods, type inference and overload method resolution. Type inference occurs at compile time before the compiler tries to resolve overloaded method signatures. The compiler applies type inference logic to all generic methods that share the same name. In the overload resolution step, the compiler includes only those generic methods on which type inference succeeded. <a href="http://msdn.microsoft.com/en-us/library/twcad0zb(VS.80).aspx" rel="nofollow noreferrer">More here...</a></p></li> <li><p>Please see the full example Console Application program code below that shows how several variants of the Foo method can be specified in creation of a MethodInfo object and then invoked using an Extension method:</p></li> </ol> <p>Program.cs</p> <pre><code>class Program { static void Main(string[] args) { MethodInfo foo1 = typeof(Example).GetGenericMethod("Foo", new[] { typeof(string) }, new[] { typeof(int) }, typeof(void)); MethodInfo foo2 = typeof(Example).GetGenericMethod("Foo", new[] { typeof(string), typeof(int) }, new[] { typeof(int) }, typeof(void)); MethodInfo foo3 = typeof(Example).GetGenericMethod("Foo", new[] { typeof(string) }, new[] { typeof(string) }, typeof(void)); MethodInfo foo4 = typeof(Example).GetGenericMethod("Foo", new[] { typeof(string), typeof(int) }, new[] { typeof(int), typeof(string) }, typeof(string)); Console.WriteLine(foo1.Invoke(null, new object[] { 1 })); Console.WriteLine(foo2.Invoke(null, new object[] { 1 })); Console.WriteLine(foo3.Invoke(null, new object[] { "s" })); Console.WriteLine(foo4.Invoke(null, new object[] { 1, "s" })); } } </code></pre> <p>Example.cs:</p> <pre><code>public class Example { public static void Foo&lt;T&gt;(int ID) { } public static void Foo&lt;T, U&gt;(int ID) { } public static void Foo&lt;T&gt;(string ID) { } public static string Foo&lt;T, U&gt;(int intID, string ID) { return ID; } } </code></pre> <p>Extensions.cs:</p> <pre><code>public static class Extensions { public static MethodInfo GetGenericMethod(this Type t, string name, Type[] genericArgTypes, Type[] argTypes, Type returnType) { MethodInfo foo1 = (from m in t.GetMethods(BindingFlags.Public | BindingFlags.Static) where m.Name == name &amp;&amp; m.GetGenericArguments().Length == genericArgTypes.Length &amp;&amp; m.GetParameters().Select(pi =&gt; pi.ParameterType).SequenceEqual(argTypes) &amp;&amp; m.ReturnType == returnType select m).Single().MakeGenericMethod(genericArgTypes); return foo1; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload