Reflection Performance – Create Delegate (Properties C#)

This should work for you: static Action<object, object> BuildSetAccessor(MethodInfo method) { var obj = Expression.Parameter(typeof(object), “o”); var value = Expression.Parameter(typeof(object)); Expression<Action<object, object>> expr = Expression.Lambda<Action<object, object>>( Expression.Call( Expression.Convert(obj, method.DeclaringType), method, Expression.Convert(value, method.GetParameters()[0].ParameterType)), obj, value); return expr.Compile(); } Usage: var accessor = BuildSetAccessor(typeof(TestClass).GetProperty(“MyProperty”).GetSetMethod()); var instance = new TestClass(); accessor(instance, “foo”); Console.WriteLine(instance.MyProperty); With TestClass: public class TestClass … Read more

Eclipse RCP: Actions VS Commands

Did you read the eclipse wiki FAQ What is the difference between a command and an action? You probably already understand that Actions and Commands basically do the same thing: They cause a certain piece of code to be executed. They are triggered, mainly, from artificats within the user interface The main concern with Actions … Read more

How can I create an Action delegate from MethodInfo?

Use Delegate.CreateDelegate: // Static method Action action = (Action) Delegate.CreateDelegate(typeof(Action), method); // Instance method (on “target”) Action action = (Action) Delegate.CreateDelegate(typeof(Action), target, method); For an Action<T> etc, just specify the appropriate delegate type everywhere. In .NET Core, Delegate.CreateDelegate doesn’t exist, but MethodInfo.CreateDelegate does: // Static method Action action = (Action) method.CreateDelegate(typeof(Action)); // Instance method (on … Read more

How do I use two submit buttons, and differentiate between which one was used to submit the form? [duplicate]

Give each input a name attribute. Only the clicked input‘s name attribute will be sent to the server. <input type=”submit” name=”publish” value=”Publish”> <input type=”submit” name=”save” value=”Save”> And then <?php if (isset($_POST[‘publish’])) { # Publish-button was clicked } elseif (isset($_POST[‘save’])) { # Save-button was clicked } ?> Edit: Changed value attributes to alt. Not sure this … Read more

How can I pass a parameter in Action?

If you know what parameter you want to pass, take a Action<T> for the type. Example: void LoopMethod (Action<int> code, int count) { for (int i = 0; i < count; i++) { code(i); } } If you want the parameter to be passed to your method, make the method generic: void LoopMethod<T> (Action<T> code, … Read more