Skip to content

Instantly share code, notes, and snippets.

@kou-yeung
Last active May 22, 2016 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kou-yeung/b94019d36ceee52d3fce2415391320a4 to your computer and use it in GitHub Desktop.
Save kou-yeung/b94019d36ceee52d3fce2415391320a4 to your computer and use it in GitHub Desktop.
Generic Construct for C#
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);
}
}
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