I'm just curious, in AS3 I can use the methods apply and call, as in:
aFn.apply( thisObj, arrayOfArgs );
or
aFn.call( thisObj, a, b, c );
is there an equivalent syntax in C#?
Preferably without using Reflection.
Thank you for reading.
Not quite. Given a particular instance method like say Object#Equals
You can use Delegate.CreateDelegate to create an "open" delegate. Which will unbind the "this" parameter. Unfortunately, it will not call that particular implementation of Equals, it will call this's class implementation. As Object#Equals is a virtual method.
Once you have your open delegate you can use dynamic invoke. To apply, but the first element in the array is the this.
You can do this::
var @this = new { a = 3, b = 4 };
var other = new { a = 3, b = 4 };
Func<object, object, bool> equalFn = Delegate.CreateDelegate(
typeof(Func<object, object, bool>),
typeof(object).GetMethod("Equals",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance)) as Func<object, object, bool>;
equalFn.Invoke(@this, other); // call example
equalFn.DynamicInvoke(new[] { @this, other }); // apply example
Open delegates are fairly useful. I can't really recommend a good use for DynamicInvoke if you know your types though.
params object[] myObjectssyntax for this parameter. msdn.microsoft.com/en-us/library/w5zay9db.aspx