Probably something simple I overlooked how do I fix this?
Note: compiler generates 0 errors yet it doesn't build
/**
* The action (optional object[] is the params)
*/
private Action<object[]> action;
/**
* Parameters
*/
private object[] parameters;
public virtual void runAction() //<- this is overridable
{
action(parameters);
}
public void setAction(Act开发者_JAVA百科ion action)
{
this.action<null> = action<null>;
}
public void setAction(Action action, params object[] parameters)
{
this.parameters = this.parameters;
this.action<parameters> = action<parameters>;
}
Maybe there is a way I can make this even without object[] parameters
even..
I call runAction
outside this class. there are many classes like this in List all I call runAction
method. I generate these classes on demend. This class contains a bunch more logic then just what you see.
Let me know thanks I appreciate the support.
this.action<parameters> = action<parameters>;
This is a mess. action
isn't a generic type or generic method, so this isn't supplying a generic parameter (and anyway, generic parameters are types, not variables. You're actually somehow using the less-than and greater-than relational operators? But parameters > ;
doesn't compare anything.
The compiler is absolutely right not to let this build. It seems like this would be what you want:
private Action action;
public virtual void runAction()
{
action();
}
public void setAction(Action action)
{
this.action = action;
}
public void setAction(Action<object[]> action, params object[] parameters)
{
this.action = delegate { action(parameters) };
}
I recommend that you read about "closures". You might find that this class you're writing isn't very useful at all.
I don't think Action
supports a variable number of parameters. I would think you could just do:
public void RunAction(Action<object[]> action, params object[] parameters)
{
action(parameters);
}
which would end up being used something like:
RunAction( x =>
{
Assert.AreEqual("Param1", x[0]);
Assert.AreEqual("Param2", x[1]);
},
"Param1",
"Param2");
Or am I missing something in what you are trying to do?
Since you are using VS 2010, and .Net 4.0? ... try
public void setAction(Action action, object[] parameters = null)
{
this.parameters = this.parameters;
this.action<parameters> = action<parameters>;
}
You are creating an 'optional' parameter.
If you're trying to use an optional parameter so that you can have a shorter call when you don't need the additional parameter, and you're just working with 1 - 2 parameters you can do the following:
Define two actions, one that accepts the first parameter, and one that accepts the two parameters (e.g.):
private readonly Action<EventType> _updateLog; private readonly Action<EventType, string> _updateLogWithDescription;
Once you instantiate the action, just assign the action with two parameters as the input parameter, and calculate the action with one parameter based on calling the other action and giving it the default parameters, e.g.:
public MyClass(Action<EventType, string> updateLogWithDescription) { _updateLogWithDescription = updateLogWithDescription; _updateLog = (EventType eventType) => updateLogWithDescription(eventType, string.Empty); }
You will then just need to worry about 1 source action that does all the internal computation, and then in the class that you are actually calling the actions you will simply use the two instances as you need them:
_updateLog(EventType.MyEvent);
_updateLogWithDescription(EventType.MyOtherEvent, $"My custom data {myvalue}");
I preferred to use this as my solution, as I really don't like to mess about with object[]
and casting.
精彩评论