I am resorting to a horrible hack in order to fill a locked-down data type in the XNA framework: there's an internal method in a structure that I wish to call without feeding the garbage collector.
If I keep said structure boxed in an object variable and use MethodInfo.Invoke()
, that call would itself feed the garbage collector by boxing the parameters:
private object boxedTouchCollection;
void test() {
MethodInfo addTouchLocationMethod = typeof(TouchCollection).GetMethod(
"AddTouchLocation", BindingFlags.Instance | BindingFlags.NonPublic
);
addTouchLocationMethod.Invoke(
this.boxedState, new object[] { /* parameters being boxed */ }
);
}
I'm not sure whether Delegate.CreateDelegate()
can be used here - can I just turn the first parameter into an object and it will work on the boxed structure? Or can I store my structure unboxed and declare the first parameter as ref TouchCollection
?
delegate void AddTouchLocationDelegate(
ref TouchCollection collection,
int id,
// ...more parameters...
);
private TouchCollection touchCollection;
void test() {
Delegate.CreateDelegate(
typeof(AddTouchLocationDelegate),
typeof(ref TouchCollection), // doesn't compile
addTouchLocationMethod
);
}
Is there a way I can make Delegate.CreateDelegate()
work?
Or will I have to resort to dynamic IL generation?
Here's one way.
It relies on this overload of Delegate.CreateDelegate
, which creates open instance-method delegates. The only tricky bit is that so you'll have to create the appropriate delegate-type to be able to pass the struct by reference.
I don't think there should be any boxing with this technique - either with the arguments to the method, or with the struct itself.
Example: (Apologies for simplifying the example-types)
public struct Foo
{
// Internal method to be called. Takes a value-type parameter.
internal void Test(int someParam)
{
Console.WriteLine(someParam);
}
// Custom delegate-type. Takes the Foo instance of interest
// by reference, as well as the argument to be passed on to Test.
public delegate void MyDelegate(ref Foo foo, int someParam);
// Creates type-safe delegate
private static MyDelegate GetTestDelegate()
{
var flags = BindingFlags.Instance | BindingFlags.NonPublic;
var methodInfo = typeof(Foo).GetMethod("Test", flags);
return (MyDelegate) Delegate.CreateDelegate
(typeof(MyDelegate), methodInfo);
}
static void Main()
{
Foo foo = new Foo();
MyDelegate action = GetTestDelegate();
// should dodge boxing
action(ref foo, 42);
}
}
Here's another solution using Linq Expression Trees that I found in the meantime:
private delegate void AddTouchLocationDelegate(
ref TouchCollection touchCollection,
int id,
TouchLocationState state,
float x,
float y,
TouchLocationState prevState,
float prevX,
float prevY
);
private static AddTouchLocationDelegate createAddTouchLocationDelegate() {
MethodInfo addTouchLocationMethod = typeof(TouchCollection).GetMethod(
"AddTouchLocation", BindingFlags.Instance | BindingFlags.NonPublic
);
Type byrefTouchCollection = typeof(TouchCollection).MakeByRefType();
ParameterExpression instance = Expression.Parameter(byrefTouchCollection, "instance");
ParameterExpression idValue = Expression.Parameter(typeof(int), "id");
ParameterExpression stateValue = Expression.Parameter(
typeof(TouchLocationState), "state"
);
ParameterExpression xValue = Expression.Parameter(typeof(float), "x");
ParameterExpression yValue = Expression.Parameter(typeof(float), "y");
ParameterExpression prevStateValue = Expression.Parameter(
typeof(TouchLocationState), "prevState"
);
ParameterExpression prevXValue = Expression.Parameter(typeof(float), "prevX");
ParameterExpression prevYValue = Expression.Parameter(typeof(float), "prevY");
Expression<AddTouchLocationDelegate> expression =
Expression.Lambda<AddTouchLocationDelegate>(
Expression.Call(
instance, addTouchLocationMethod,
idValue, stateValue, xValue, yValue, prevStateValue, prevXValue, prevYValue
),
instance,
idValue, stateValue, xValue, yValue, prevStateValue, prevXValue, prevYValue
);
return expression.Compile();
}
Usage is straightforward:
var d = createAddTouchLocationDelegate();
d(
ref this.touches,
1, TouchLocationState.Pressed, 10, 10, TouchLocationState.Released, 0, 0
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With