Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6359605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:32:27+00:00 2026-05-24T23:32:27+00:00

In my application I previously used regular C# attributes to annotate a method. E.g.:

  • 0

In my application I previously used regular C# attributes to “annotate” a method. E.g.:


[Foo(SomeKey="A", SomeValue="3")]
[Foo(SomeKey="B", SomeValue="4")]
public void TheMethod()
{
   SpecialAttributeLogicHere();
}


What SpecialAttributeLogicHere() did, was to reflectively look at all the Foo-attributes that annotated this particular method. It would then (on its own), create its own dictionary for all the keys and values.

I’m now trying to move to PostSharp, because the SpecialAttributeLogic could be put into an aspect (and removed from the method body which is much cleaner!), within OnEntry. Foo will be replaced by an aspect that extends OnMethodBoundaryAspect.

I would still like to use it the following way:


[Foo(SomeKey="A", SomeValue="3")]
[Foo(SomeKey="B", SomeValue="4")]

But if Foo has an OnEntry, that means that the “SpecialAttributeLogic” will be executed twice. I basically need to “gather” all the keys and values from each Foo(), into a dictionary, which I then apply some logic to.

How to do this (or best practices) with PostSharp? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:32:28+00:00Added an answer on May 24, 2026 at 11:32 pm

    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.

    You can reflect the values at compile time as to keep performance optimal.

    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”.

    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<string, string>());
            }
    
            public void MyMethod(Dictionary<string, string> values)
            {
                //Do work
            }
    
        }
    
        [Serializable]
        public class MyAspect : MethodInterceptionAspect
        {
            Dictionary<string, string> values = new Dictionary<string, string>();
            MethodInfo target;
    
            public override void CompileTimeInitialize(System.Reflection.MethodBase method, AspectInfo aspectInfo)
            {
                target = method.DeclaringType.GetMethod(method.Name, new Type[] { typeof(Dictionary<string, string>) });
    
                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 < 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; }
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The only type of 'remote desktop' application I've used previously is VNC viewer. My
I have an application that can be used without authentication on computers in public
I've previously used log4net, but my current employer uses Enterprise Library application blocks. I
My application needs to, like most, store data. The application was previously used on
I have created a Gemfile in my rack application (that previously used .gems). When
I'm using NET 2.0 WinForms for my C# application. Previously I used .NET 4.0
I made some changes to an existing application that was previously just straight HTML
is there a way to capture application that was previously active, before my application
I have an application that uses CoreData. I previously had a class named Marker
I have an application that contains a VC++ project (along with C# projects). Previously,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.