Overriding virtual methods when instantiating a class

I have a class with some virtual functions, let's say this is one of them:

public class AClassWhatever { protected virtual string DoAThingToAString(string inputString) { return inputString + "blah"; } } 

I want to instantiate this class by overriding the built-in string "DoathingToAString", how can I declare the inline properties in the declaration as follows:

 ... AClassWhatever instance = new AClassWhatever { DoAThingToAString = new function(string inputString) { return inputString + inputString + " fill my eyes." } } ... 

And now for the "instance", DoThingToAString is overridden by this definition. I need to be able to define the default behavior in the class definition and only redefine it as needed, at different times, and I do not want to propagate a bunch of subclasses to do this.

I know that I need to use delegates or something like that, but I have been away from the syntax game for too long and Google has given me too much irrelevant information.

+7
source share
3 answers

You can use delegates or Func .

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." }; 
+6
source

You cannot do this using methods, but you could do the equivalent using delegation / anonymity 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."; } } 

Using:

 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 + "..." same as x => x + x + "..."

+5
source

Why not define a delegate property and use this definition if it exists when calling a method? In your object instance, you can provide an alternative definition using a 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 your delegate property to override any overridden definition of a virtual method, you can do this as follows:

 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"; } } 
0
source

All Articles