Skip to content

Instantly share code, notes, and snippets.

@Spartan322
Created April 6, 2020 01:23
Show Gist options
  • Save Spartan322/dced1845903ae3aea16b326242e280a3 to your computer and use it in GitHub Desktop.
Save Spartan322/dced1845903ae3aea16b326242e280a3 to your computer and use it in GitHub Desktop.
public static T Create<T>(Type[] types, params object[] args)
{
if (types.Length != args.Length)
throw new TypeLoadException();
var ctor = typeof(T).GetConstructor(types);
if (ctor == null) return default;
return GetActivator<T>(ctor)(args);
}
public static T Create<T>(params object[] args)
{
var typeArr = new Type[args.Length];
for (int i = 0; i < args.Length; i++)
typeArr[i] = args[i].GetType();
return Create<T>(typeArr, args);
}
public static T Create<T, ArgT>(ArgT arg)
=> Create<T>(new Type[] { typeof(ArgT) }, arg);
public static T Create<T, ArgT, ArgT2>(ArgT arg, ArgT2 arg2)
=> Create<T>(new Type[] { typeof(ArgT), typeof(ArgT2) }, arg, arg2);
public static T Create<T, ArgT, ArgT2, ArgT3>(ArgT arg, ArgT2 arg2, ArgT3 arg3)
=> Create<T>(new Type[] { typeof(ArgT), typeof(ArgT2), typeof(ArgT3) }, arg, arg2, arg3);
public static T Create<T, ArgT, ArgT2, ArgT3, ArgT4>(ArgT arg, ArgT2 arg2, ArgT3 arg3, ArgT4 arg4)
=> Create<T>(new Type[] { typeof(ArgT), typeof(ArgT2), typeof(ArgT3), typeof(ArgT4) }, arg, arg2, arg3, arg4);
public static T Create<T, ArgT, ArgT2, ArgT3, ArgT4, ArgT5>(ArgT arg, ArgT2 arg2, ArgT3 arg3, ArgT4 arg4, ArgT5 arg5)
=> Create<T>(new Type[] { typeof(ArgT), typeof(ArgT2), typeof(ArgT3), typeof(ArgT4), typeof(ArgT5) }, arg, arg2, arg3, arg4, arg5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment