Passing a custom attribute with a variable value as a parameter

16,196

Solution 1

Can something like this be done ?!

No. Constructor parameters in attributes must be resolved at compile time. They are intended as metadata on the type or method itself, not something that would be used per call or per instance.

Given your description, an attribute is likely not an appropriate way to handle this. Since you want to run extra code that happens per call, you will need a different technique. For example, you could pass a delegate, ie:

public int CheckedAddEntity(int entityId, Func<int, int> funcToAdd)
{
    // Perform your checking on entityId here
    return funcToAdd();
}

This would let you then call via something like:

int result = CheckedAddEntity(entityId, AddNewEntity);

Solution 2

In this case, I recommend looking at Aspect-Oriented programming. It is a different way of doing code, but one that allows you to re-use the boilerplate logic (e.g. authentication) throughout. You might have to design your attribute a little bit differently, but all of the logic can be put into an "aspect" which then gets compiled automatically into the code when you build the project.

I personally use PostSharp, although I know there are others out there. They have a free license available for development; as long as you don't require advanced functionality, it's very cost-effective.

http://www.postsharp.net/blog/post/5-Ways-That-Postsharp-Can-SOLIDify-Your-Code-Authorization

Share:
16,196

Related videos on Youtube

Samir Adel
Author by

Samir Adel

Samir Adel, is a graduate of the faculty of Computer Science and Information System, Ain Shams University. Samir is a .Net Senior Software Developer specialized in the Web Development at ArkDev where has developed, maintained and supported a web strategic management system since 2006. He has proved experience in communicating with clients, analyzing the clients requirements and the translation of the requirements to a robust architecture using the latest .Net technologies and design patterns. Samir holds a certificate of completion of the “Next Generation Strategy” course presented by Michael Porter, in addition to his knowledge in the following fields: Asp.net HtppHandler and HtppModule Globalization ADO.Net Entity Framewrok LinQ C#1.1, C#2 and C# 3.5 Custom web Controls SQL Javascript Ajax HTML CSS

Updated on September 15, 2022

Comments

  • Samir Adel
    Samir Adel about 1 year

    I created a custom attribute class that will check the system security and throws an authentication exception if there is a security error.

    public class EntityChecker: System.Attribute
    {
        public EntityChecker(int entityId)
        {
            // doing some logic to check if the entityId is allowed to be inserted
        }
    }
    

    I want to use this custom attribute as a declaration to an entity addition function and I want to pass a variable from the function to the attribute constructor. can something like this be done?

    [EntityChecker(entityId)]
    public int AddNewEntity(entityId)
    {
     // logic of entity addition
    }
    
  • Samir Adel
    Samir Adel about 11 years
    first thank you so much for your response so how can I do the same logic, I want a security check to be done directly before the function is called ?!
  • Reed Copsey
    Reed Copsey about 11 years
    @SamirAdel I provided another alternative approach.
  • Justin L.
    Justin L. about 6 years
    fyi your link is dead!
  • theMayer
    theMayer about 6 years
    Thanks @JustinL. - FYI anyone can edit posts on SO. I've updated this one, but feel free to contribute more than a comment in the future :)