Last active
March 8, 2016 21:40
-
-
Save pema99/8021effec4287516c3a0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public object CreateInstance(string Name, params object[] Args) | |
{ | |
Type ToCreate = Type.GetType(Name); | |
var InnerTypeList = new List<Type>(); | |
InnerTypeList.Add(typeof(CallSite)); | |
InnerTypeList.Add(typeof(Type)); | |
foreach (object Arg in Args) | |
{ | |
InnerTypeList.Add(Arg.GetType()); | |
} | |
InnerTypeList.Add(ToCreate); | |
Type FuncType; | |
switch (Args.Length) | |
{ | |
case 0: | |
FuncType = typeof(Func<,,>); | |
break; | |
case 1: | |
FuncType = typeof(Func<,,,>); | |
break; | |
case 2: | |
FuncType = typeof(Func<,,,,>); | |
break; | |
case 3: | |
FuncType = typeof(Func<,,,,,>); | |
break; | |
case 4: | |
FuncType = typeof(Func<,,,,,,>); | |
break; | |
case 5: | |
FuncType = typeof(Func<,,,,,,,>); | |
break; | |
case 6: | |
FuncType = typeof(Func<,,,,,,,,>); | |
break; | |
case 7: | |
FuncType = typeof(Func<,,,,,,,,,>); | |
break; | |
case 8: | |
FuncType = typeof(Func<,,,,,,,,,>); | |
break; | |
} | |
var InnerType = FuncType.MakeGenericType(InnerTypeList.ToArray()); | |
var OuterType = typeof(CallSite<>).MakeGenericType(InnerType); | |
var CreateMethod = OuterType.GetMethod("Create"); | |
var ArgInfo = new List<CSharpArgumentInfo>(); | |
ArgInfo.Add(CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType, null)); | |
for (int i = 0; i < Args.Length; i++) | |
ArgInfo.Add(CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)); | |
dynamic Call = CreateMethod.Invoke( | |
this, | |
new object[] { | |
Binder.InvokeConstructor( | |
CSharpBinderFlags.None, | |
typeof(Test), | |
ArgInfo | |
) | |
} | |
); | |
var Parameters = new object[2 + Args.Length]; | |
Parameters[0] = Call; | |
Parameters[1] = ToCreate; | |
for (int i = 0; i < Args.Length; i++) | |
Parameters[i + 2] = Args[i]; | |
return Call.Target.DynamicInvoke(Parameters); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment