Skip to content

Instantly share code, notes, and snippets.

@kumpera
Created July 12, 2016 17:07
Show Gist options
  • Select an option

  • Save kumpera/35a1e014252d35ab2d617e6ff9732933 to your computer and use it in GitHub Desktop.

Select an option

Save kumpera/35a1e014252d35ab2d617e6ff9732933 to your computer and use it in GitHub Desktop.
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
class Test {
public virtual void Foo(int arg)
{
Console.WriteLine ("foo {0}", arg);
}
public virtual void Bar (int a, int b)
{
Console.WriteLine ("bar {0} {1}", a, b);
}
}
class Driver {
public delegate void InvokeDelegate (MethodInfo info, object[] args);
/* Create a subclass of @type names @name and have all virtuals of @type dispatch to @invoke */
[MethodImpl(MethodImplOptions.InternalCall)]
extern static Type CloneType (Type type, string name, InvokeDelegate invoke);
/* Allocs an object without calling its ctor. Was too lazy to remember the serialization equivalent */
[MethodImpl(MethodImplOptions.InternalCall)]
extern static object RawNew (Type type);
static void Invoke (MethodInfo info, object[] args) {
Console.WriteLine ("method: {0} args: {1}", info, args.Length);
foreach (var a in args)
Console.WriteLine ("\t{0}", a);
}
static void Main () {
var clonedType = CloneType (typeof (Test), "bla", Invoke);
Test t = (Test)RawNew (clonedType);
t.Foo (20);
t.Bar (40, 50);
}
}
/* Output is:
method: Void Foo(Int32) args: 2
\[AUTO\].bla
20
method: Void Bar(Int32, Int32) args: 3
\[AUTO\].bla
40
50
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment