Last active
May 22, 2016 11:47
-
-
Save kou-yeung/b94019d36ceee52d3fce2415391320a4 to your computer and use it in GitHub Desktop.
Generic Construct for C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Generic | |
{ | |
public static T Construct<T>(params object[] p) | |
{ | |
var types = new Type[p.Length]; | |
for (var i = 0 ; i < p.Length; ++i) { types[i] = p[i].GetType(); } | |
return (T)typeof(T).GetConstructor(types).Invoke(p); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Foo | |
{ | |
public Foo(int val) | |
{ | |
Console.WriteLine("Foo(int) : " + val); | |
} | |
public Foo(string val) | |
{ | |
Console.WriteLine("Foo(string) : " + val); | |
} | |
} | |
public class prop | |
{ | |
static void Main() | |
{ | |
Generic.Construct<Foo>(1); | |
Generic.Construct<Foo>("hello world!!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment