Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use the castle Dynamic proxy instead on simple attributes. Because to do it with attributes will force you to use the reflection in runtime to change something in attribute params. This way, like a <code>user1908061</code> adviced will cause 2 issues:</p> <p>1) Performance issues. Calling in the runtime a stored procedures is very "heavy-weight" for the performance of your application 2) Multy-thread issue. Let's imagine that 2 threads call this codes with the interval of 10 ticks</p> <pre><code>var method = typeof(TestClass).GetMethod("Foo"); var attribute = method.GetCustomAttribute(typeof(TestAttribute)) as TestAttribute; attribute.Bar = "Hello"; </code></pre> <p>Thread1 will change the attribute of <code>Bar</code> param to "Hello" in this time the Thread2 will access the same code and will change the <code>Bar</code> param to some other value, this will cause the problem. Let's look on it in the timeline:</p> <pre><code> 0 ticks - Thread1 started to work, accessed the property of TestAttribute and made the Bar param = "Hello" 20 ticks - Thread2 started to work, accessed the property of TestAttribute and made the Bar param = "SomeOtherValue" 40 ticks - You are expecting that you will call function with `Bar`=="Hello", but it's getting called with the `Bar`=="SomeOtherValue" 60 ticks - Thread2 getting called with `Bar`=="SomeOtherValue" </code></pre> <p>So your Thread1 will get the wrong result.</p> <p>So I would recommend to use CastleDynamicProxy to do what you want. With it you can run some code before the method and replace the result of method and also you can run some code after the method. This way will work in the runtime.</p> <p>Also there is a technology - PostSharp will do the same but a little bit in another way. This one will work in the compile time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload