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

  • SEARCH
  • Home
  • 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 7564961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:01:34+00:00 2026-05-30T14:01:34+00:00

I want to make an attribute that will allow me to specify some method

  • 0

I want to make an attribute that will allow me to specify some method applied to property, like this:

public class MyClass
{
    [MyAttribute(Converter="ConverterMethod")]
    public string Prop { get; set; }

    public static string ConverterMethod(string src)
    {
        return src + " converted";
    }
}

What is the ‘right’ way to do that?

Here are the ways that I see:

  1. Make string property and extract corresponding method with reflection during runtime
  2. Make Dictionary<string, Func<string, string>> and populate it with corresponding methods during runtime. Then extract method using attribute’s string property as a key. This method is more resistant to refactoring, if I rename method everything will work (dictionary key will remain the same though)
  3. Make ‘IConverter’ interface and pass typeof(ConverterImpl) to attribute. Then create an instance of converter during runtime and use its interface to convert values. This way seem the best to me but I never used Type properties in attributes and don’t even know if they are serialized well.

Which approach is best? Is there other approaches? How people usually do such things?

  • 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-30T14:01:35+00:00Added an answer on May 30, 2026 at 2:01 pm

    The first option is pretty normal, and has the advantage of simplicity. You are right to say the it is a little susceptible to refactoring, but… this is rarely a “real” problem.

    Another (fourth) approach, however, would be to make the attribute abstract with an abstract method, and subclass it with the conversion code in the attribute – then at runtime you can get the attribute (as the base-attribute) and just call the virtual method, this approach s common in things like MVC.

    Personally, I usually just use the first option, along with unit tests for safety (in case of refactoring etc).

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

Sidebar

Related Questions

I have a class that need to make some magic with every operator, like
I want to make a table in SqlServer that will add, on insert, a
Hey, what i want to do is to make a int that will be
I have a method that will provide an array of model object. Some of
I want to make a custom AuthorizeAttribute class as described here: Override Authorize Attribute
I think that I fully understand this, but I just want to make sure
In Python, I want to make selected instance attributes of a class be readonly
Let's say I want make some of my sources publicly available via my blog
I have style sheet with a class name changebackgroundcolor i want make change in
I want to make an etag that matches what Apache produces. How does apache

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.