Reputation: 227
I have a class with some virtual functions, let's pretend this is one of them:
public class AClassWhatever
{
protected virtual string DoAThingToAString(string inputString)
{
return inputString + "blah";
}
}
I want to instantiate this class while overriding "DoAThingToAString" inline, much like I can declare properties inline in a declaration, as follows:
...
AClassWhatever instance = new AClassWhatever
{
DoAThingToAString = new function(string inputString)
{
return inputString + inputString + " fill my eyes."
}
}
...
And now for "instance", DoAThingToAString is overridden with that definition. I need to be able to define the default behavior in the class definition and only override it as needed, differently different times, and I don't want to proliferate a bunch of subclasses to do this.
I know I need to use delegates or something similar, but I've been out of the syntax game for way too long and google was giving me too much irrelevant info.
Upvotes: 8
Views: 8584
Reputation: 9847
You can't do it with methods, but you could do the equivalent with delegates / anonymous methods:
public class AClassWhatever
{
public AClassWhatever()
{
this.DoAThingToAString = this.DoAThingToAStringImpl;
}
public Func<string, string> DoAThingToAString { get; set; }
protected virtual string DoAThingToAStringImpl(string input)
{
return input + input + " fill my eyes.";
}
}
Usage:
var instance = new AClassWhatever
{
DoAThingToAString = inputString => inputString + inputString +
" fill my eyes something other than the default behavior."
}
var result = instance.DoAThingToAString("input");
Note that inputString => inputString + inputString + "..."
is the same as x => x + x + "..."
Upvotes: 5
Reputation: 24232
Why not define a delegate property and use that definition if it exists when the method is called? In your instantiation of the object, you can provide an alternate definition using the property.
For example:
public MyDelegateTypeThatMatchesMyFunctionCall DoAThingToAStringProperty { get; set; }
public class AClassWhatever
{
protected virtual string DoAThingToAString(string inputString)
{
var handler = this.DoAThingToAStringProperty;
if (handler)
{
return handler(inputString);
}
// do default behavior
return inputString + "blah";
}
}
If you want to ensure that your delegate property overrides any overridden definition of the virtual method you could do it like this:
public MyDelegateTypeThatMatchesMyFunctionCall DoAThingToAStringProperty { get; set; }
public class AClassWhatever
{
protected string DoAThingToAString(string inputString)
{
var handler = this.DoAThingToAStringProperty;
if (handler)
{
return handler(inputString);
}
// do default behavior
return DoAThingToAStringInternal(inputString);
}
protected virtual string DoAThingToAStringInternal(string inputString)
{
// do default behavior
return inputString + "blah";
}
}
Upvotes: 0
Reputation: 17579
You can use delegates or Func
s to do this.
First, add this to your AClassWhatever
class:
public Func<string, string> DoAThingToAString = (x) => x + "blah";
Now you can use the same syntax to override the action.
AClassWhatever instance = new AClassWhatever()
{
DoAThingToAString = (x) => x + x + " fill my eyes."
};
Upvotes: 8