I have a class with a enum DoStuff with values before, after, or none. These names are not the true names, but it gets the point across. The class has a method foo.
What follows is a set of readonly properties, of different types, each of which looks as follows:
public [type] MyProperty {
get {
if(enumValue == DoStuff.Before)
foo();
[type] result = //Do calculations here
if(enumValue == DoStuff.After)
foo();
return result;
}
}
Is there a way of abstracting these pre/post-calculation calls out? I can currently think of two solutions:
-
Create a private method that takes a delegate, and calls
fooin the appropriate place. Complicated by the lack of generics on the platform I’m writing this for. -
Make an uninstantiable base class with neither wrapper call, and derive a Before and After subclass that accesses the base class’ properties, with the call in the appropriate place
Is there a well known pattern for this kind of structure?
Create a method with this syntax:
Then in your property