Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like you want to build a namevaluepair inside of your method. You cannot do this with an aspect. What I suggest is you use a MethodInterceptionAspect and reflect the attributes on the method then build your collection and pass it in the the method via a parameter (maybe using an overloaded method) or setting it as a class member.</p> <p>You can reflect the values at compile time as to keep performance optimal.</p> <p>Here is a quicky solution to your problem. It's a bit ugly (you will need to make modifications to fit). There are other ways but they aren't as "generic".</p> <pre><code>namespace ConsoleApplication12 { class Program { static void Main(string[] args) { MyExampleClass ec = new MyExampleClass(); ec.MyMethod(); } } public class MyExampleClass { [Special(Key = "test1", Value = "1234")] [Special(Key = "test2", Value = "4567")] [MyAspect] public void MyMethod() { MyMethod(new Dictionary&lt;string, string&gt;()); } public void MyMethod(Dictionary&lt;string, string&gt; values) { //Do work } } [Serializable] public class MyAspect : MethodInterceptionAspect { Dictionary&lt;string, string&gt; values = new Dictionary&lt;string, string&gt;(); MethodInfo target; public override void CompileTimeInitialize(System.Reflection.MethodBase method, AspectInfo aspectInfo) { target = method.DeclaringType.GetMethod(method.Name, new Type[] { typeof(Dictionary&lt;string, string&gt;) }); foreach (Attribute a in method.GetCustomAttributes(false)) { if (a is SpecialAttribute) { values.Add(((SpecialAttribute)a).Key, ((SpecialAttribute)a).Value); } } } public override void OnInvoke(MethodInterceptionArgs args) { if (values == null || values.Count &lt; 1) { args.Proceed(); } else { target.Invoke(args.Instance, new object[] { values }); } } } [AttributeUsage(AttributeTargets.Method, AllowMultiple = true) ] public class SpecialAttribute : Attribute { public string Key { get; set; } public string Value { get; set; } } } </code></pre> <p>target and values are both initialized at compiletime (not runtime) for consumption at runtime. They get serialized with the aspect at compiletime. This way you save on the reflection hit at runtime.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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