I am creating Traces for a method and want it to use with a custom attribute. I will decorate each method with TraceMethod.
eg:
[TraceMethod()]
public void SomeMethod()
{
}
public class TraceMethod : Attribute
{
public void StartTrace(){}
public void EndTrace(){}
}
So here,
How to call StartTrace() before the SomeMethod start executing and EndTrace() after the execution of SomeMethod ends? Is it possible?
What you are trying to do is Aspect-Oriented Programming, which is something that is currently not supported out-of-the-box in the .NET world. You will have to use a third-party component; there are some out there, both paid and open sourced.
You could modify method body:
public void SomeMethod()
{
var trace = new Trace();
try
{
... rest of method
}
finally
{
trace.EndTrace();
}
}
public class TraceMethod : Attribute
{
public TraceMethod() => StartTrace();
public void StartTrace() { ... }
public void EndTrace() { ... }
}
Perhaps create a custom class that marks the scope of a function? Create an instance of the class at the start of a function and when function terminates the class gets out of scope and destructor is called.
Constructor and destructor mark beginning and end of a function.
Edit:
As commented it is not garranteed that the destructor is called immediately after the object gets out of scope. Better is to use a using() block:
public void SomeMethod()
{
using (TraceMethod trace = new TraceMethod())
{
}
}
public class TraceMethod : IDisposable
{
public TraceMethod() { StartTrace(); } // Constructor
public void Dispose() { EndTrace(); } // Gets called when leaving the using() block
private void StartTrace() { ... }
private void EndTrace() { ... }
}
Related
Picture a case like this:
I have a controller action (or service method) where I need to call three methods in a consecutive order, each method has a single responsibility.
public return_type MyMethod(_params_) {
// .. some code
Method_1 (...);
Method_2 (...);
Method_3 (...);
// ... some more code
}
A developer can make the mistake of calling Method_2 before Method_1, or at least we can say that nothing forces him to follow this order, or to get an exception when the order isn't followed.
Now we can call Method_2 inside Method_1, and Method_3 inside Method_2, but that doesn't seem right when each method handles a completely different responsibility.
Is there a design pattern for this situation? Or any "clean" way to handle this?
This is exactly what facade pattern do.
Try to extract the three methods to another class, and make them private. Expose a single method MyMethod that calls the other methods in the desired order. Clients should use Facade.MyMethod
More details: https://en.m.wikipedia.org/wiki/Facade_pattern
I suppose you should leave control of execution for yourself and give possibility just to set what should be executed.
public interface IMethodsExecutor
{
void Execute();
void ShouldRunMethod1();
void ShouldRunMethod2();
void ShouldRunMethod3();
}
public class MethodsExecutor: IMethodsExecutor
{
private bool _runMethod1;
private bool _runMethod2;
private bool _runMethod3;
public MethodsExecutor()
{
_runMethod1 = false;
_runMethod2 = false;
_runMethod3 = false;
}
public void ShouldRunMethod1()
{
_runMethod1 = true;
}
public void ShouldRunMethod2()
{
_runMethod2 = true;
}
public void ShouldRunMethod3()
{
_runMethod3 = true;
}
private void Method1()
{
}
private void Method2()
{
}
private void Method3()
{
}
public void Execute()
{
if (_runMethod1)
{
Method1();
}
if (_runMethod2)
{
Method2();
}
if (_runMethod3)
{
Method3();
}
}
}
So that the usage will be:
IMethodsExecutor methodsExecutor = new MethodsExecutor();
methodsExecutor.ShouldRunMethod1();
methodsExecutor.ShouldRunMethod3();
methodsExecutor.Execute();
How could I wrap some code in brackets to do the following?
MyCustomStatement(args){
// code goes here
}
So that before the code in the brackets executes, it'll call a method and when the code in the brackets finishes executing it will call another method. Is there such a thing? I know it seems redundant to do this when I can simply call the methods before and after the code and all, but I simply was curious. I don't know how to word this exactly because I'm new to programming.
Thanks!
You can do this by storing the code in an abstract class that executes the "before" and "after" code for you when you call Run():
public abstract class Job
{
protected virtual void Before()
{
// Executed before Run()
}
// Implement to execute code
protected abstract void OnRun();
public void Run()
{
Before();
OnRun();
After();
}
protected virtual void After()
{
// Executed after Run()
}
}
public class CustomJob : Job
{
protected override void OnRun()
{
// Your code
}
}
And in the calling code:
new CustomJob().Run();
Of course then for every piece of custom code you'll have to create a new class, which may be less than desirable.
An easier way would be to use an Action:
public class BeforeAndAfterRunner
{
protected virtual void Before()
{
// Executed before Run()
}
public void Run(Action actionToRun)
{
Before();
actionToRun();
After();
}
protected virtual void After()
{
// Executed after Run()
}
}
Which you can call like this:
public void OneOfYourMethods()
{
// your code
}
public void RunYourMethod()
{
new BeforeAndAfterRunner().Run(OneOfYourMethods);
}
To literally achieve what you want, you can use a delegate:
Action<Action> callWithWrap = f => {
Console.WriteLine("Before stuff");
f();
Console.WriteLine("After stuff");
};
callWithWrap(() => {
Console.WriteLine("This is stuff");
});
This requires adding "weird syntax" to your blocks and an understanding of how delegates and anonymous functions in C# work. More commonly, if you're doing this within a class, use the technique demonstrated in #CodeCaster's answer.
I have an problem with my code. I have 2 classes:
clsSMS
clsWorker
When my thread is running, I want to modify an attribute of them from my clsSMS class.
public class clsSMS
{
clsWorker objclsWorker;
public clsSMS(clsWorker objclsWorker = null)
{
this.objclsWorker.operatorBlocageError38();
// The above call doesn't work...
// I think the objclsWorker is always null...
// What do you think?
}
}
public class clsWorker
{
public clsSMS clsobjSMS;
public clsWorker(...)
{
this.clsobjSMS = new clsSMS(objclsWorker: this);
}
public void operatorBlocageError38(/*String port_concerne, bool erreur38*/)
{
MessageBox.Show("The method call work fine!");
}
}
It doesn't appear from the posted code that you ever instantiated clsWorker.
clsWorker worker = new clsWorker();
worker.operatorBlocageError38();
If you call operatorBlocageError38 before instantiating, the method has to be marked static.
public static void operatorBlocageError38()
Create class MustDispose.cs
public class MustDispose
{
public MustDispose()
{
}
}
in.aspx Page
protected void Page_Load(object sender, EventArgs e)
{
using (MustDispose obj = new MustDispose)
{
// use the object
}
}
errorr
Destructors and object.Finalize cannot be called directly. Consider
calling IDisposable.Dispose if available.
You class has to impplement IDisposable, in oder to be used inside using statement.
Example:
public class MustDispose : IDisposable
{
public MustDispose()
{
}
//implement Dispose
public void Dispose()
{
....
}
}
reading from using:
Provides a convenient syntax that ensures the correct use of
IDisposable objects.
You need to implement IDisposable like so:
class TestDispose : IDisposable
{
public TestDispose()
{
...
}
public void Dispose()
{
// dispose of your resources here
}
}
Then you will be able to use it in a using block which automatically calls obj.Dispose at the end of the block.
using (var obj = new TestDispose())
{
...
}
Is like:
var obj = new TestDispose();
try
{
...
}
finally
{
obj.Dispose();
}
This would be the first time I'd use delegates in c# so please bear with me. I've read a lot about them but never thought of how/why to use this construct until now.
I have some code that looks like this:
public class DoWork()
{
public MethodWorkA(List<long> TheList) {}
public void MethodWork1(parameters) {}
public void MethodWork2(parameters) {}
}
I call MethodWorkA from a method outside the class and MethodWorkA calls MethodWork 1 and 2. When I call methodA, I'd like to pass some sort of parameter so that sometimes it just does MethodWork1 and sometimes it does both MethodWork1 and MethodWork2.
So when I call the call it looks like this:
DoWork MyClass = new DoWork();
MyClass.MethodA...
Where does the delegate syntax fit in this?
Thanks.
public void MethodWorkA(Action<ParamType1, ParamType2> method) {
method(...);
}
You can call it using method group conversion:
MethodWorkA(someInstance.Method1);
You can also create a multicast delegate that calls two methods:
MethodWorkA(someInstance.Method1 + someInstance.Method2);
For what you described, you don't need delegates.
Just do something like this:
public class DoWork
{
public void MethodWorkA(List<long> theList, bool both)
{
if (both)
{
MethodWork1(1);
MethodWork2(1);
}
else MethodWork1(1);
}
public void MethodWork1(int parameters) { }
public void MethodWork2(int parameters) { }
}
If you're just experimenting with delegates, here goes:
public partial class Form1 : Form
{
Func<string, string> doThis;
public Form1()
{
InitializeComponent();
Shown += Form1_Shown;
}
void Form1_Shown(object sender, EventArgs e)
{
doThis = do1;
Text = doThis("a");
doThis = do2;
Text = doThis("a");
}
string do1(string s)
{
MessageBox.Show(s);
return "1";
}
string do2(string s)
{
MessageBox.Show(s);
return "2";
}
}
Considering that all methods are inside the same class, and you call MethodWorkA function using an instance of the class, I honestly, don't see any reason in using Action<T> or delegate, as is I understood your question.
When I call methodA, I'd like to pass some sort of parameter so that
sometimes it just does MethodWork1 and sometimes it does both
MethodWork1 and MethodWork2.
Why do not just pass a simple parameter to MethodWorkA, like
public class DoWork()
{
public enum ExecutionSequence {CallMethod1, CallMethod2, CallBoth};
public MethodWorkA(List<long> TheList, ExecutionSequence exec)
{
if(exec == ExecutionSequence.CallMethod1)
MethodWork1(..);
else if(exec == ExecutionSequence.CallMethod2)
MethodWork2(..);
else if(exec == ExecutionSequence.Both)
{
MethodWork1(..);
MethodWork2(..);
}
}
public void MethodWork1(parameters) {}
public void MethodWork2(parameters) {}
}
Much simplier and understandable for your class consumer.
If this is not what you want, please explain.
EDIT
Just to give you an idea what you can do:
Example:
public class Executor {
public void MainMethod(long parameter, IEnumerable<Action> functionsToCall) {
foreach(Action action in functionsToCall) {
action();
}
}
}
and in the code
void Main()
{
Executor exec = new Executor();
exec.MainMethod(10, new List<Action>{()=>{Console.WriteLine("Method1");},
()=>{Console.WriteLine("Method2");}
});
}
The output will be
Method1
Method2
In this way you, for example, can push into the collection only functions you want to execute. Sure, in this case, the decision logic (which functions have to be executed) is determined outside of the call.