I have a simple class. And I have to add some conditions to some methods and properties. For example:
public class Example
{
public Boolean Condition {get; set;}
public Double ConditionValue {get; set;}
[Verify("!Condition && ConditionValue>5")]
public void DoSomthing()
{ ... }
}
I want to check the condition (for example, “!Condition && ConditionValue>5”) in an aspect attribute. I can not give an action/func into an attribute so I give a simple string. And I need to translate this string into a condition:
[Serializable]
public class MyAspectAttribute : OnMethodBoundaryAspect
{
public override void OnExit(MethodExecutionArgs args)
{
if (!this.Condition && this.ConditionValue>5) // If-statment from string, this is the problem...
{ ... }
}
}
How I can extract real if-statment from string? I see some solutions, but I’m not sure they fine:
- Use a runtime compiler, e.g. CSharpCodeProvider.
- Use a library such as http://flee.codeplex.com/
- Somthing else…?
How I can do it gracefully? Thanks!
Upd: I have edited the question on the advices…
Upd2: I don’t try to verification my code, I can do this:
public class Example2
{
public Boolean Condition {get; set;}
[LoggingIf("Condition")]
public void DoSomthing1()
{ ... }
[PrintReportIf("!Condition")]
public void DoSomthing2()
{ ... }
}
Considered using lambda expressions parser? You should be able to do something along the lines:
And compile that later using:
Edit:
For custom user types, you need to provide namespace (and assembly if type resides in different assembly than the one you call parser from):
In download section there’s User guide file – it provides some extra information on library usage.