Is it possible to do that in C# 3 or 4? Maybe with some reflection?
class Magic { [RunBeforeAll] public void BaseMethod() { } //runs BaseMethod before being executed public void Method1() { } //runs BaseMethod before being executed public void Method2() { } }
EDIT
There is an alternate solution for this, make Magic
a singleton and put your code on the getter of the static instance. That's what I did:
public class Magic { private static Magic magic = new Magic(); public static Magic Instance { get { magic.BaseMethod(); return magic; } } public void BaseMethod() { } //runs BaseMethod before being executed public void Method1() { } //runs BaseMethod before being executed public void Method2() { } }
Methods annotated with the @Before annotation are run before each test. This is useful when we want to execute some common code before running a test. Notice that we also added another method annotated with @After in order to clear the list after the execution of each test.
Most (if not all) of the standard JDK libraries will follow it. This is IMHO a good reason to go with the methods-last approach. Regarding the placement of main among the methods, putting it first or last would work. If you find it "special" in some way, then put it dead last in the file.
Declaration order of methods never matters in C# or Java. Likewise it doesn't matter whether you declare a method before or after a variable that it uses.
Java does not support “directly” nested methods. Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile.
You can't do this automatically in C# - you should probably be looking at AOP, e.g. with PostSharp.
There is an alternate solution for this, make Magic a singleton and put your code on the getter of the static instance. That's what i did.
public class Magic{ private static Magic magic; public static Magic Instance{ get { BaseMethod(); return magic; } } public void BaseMethod(){ } //runs BaseMethod before being executed public void Method1(){ } //runs BaseMethod before being executed public void Method2(){ } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With