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 6955379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:43:43+00:00 2026-05-27T14:43:43+00:00

I have a C# annotation which is : [AttributeUsage(AttributeTargets.Method)] public class OperationInfo : System.Attribute

  • 0

I have a C# annotation which is :

[AttributeUsage(AttributeTargets.Method)]
public class OperationInfo : System.Attribute {
    public enum VisibilityType {
        GLOBAL,
        LOCAL,
        PRIVATE
    }
    public VisibilityType Visibility { get; set; }
    public string Title { get; set; }
    public Func<List<string>, List<string>> Func;

    public OperationInfo(VisibilityType visibility, string title, Func<List<string>, List<string>> function) {
        Visibility = visibility;
        Title = title;
        Func = function;
    }
}

As you can see, there is a property which is a Func and I want to call it dynamically. Basically, I’d like to parse all the methods which have this annotation and then call the Func binded to the annotation.

I’d like to use it like this (this is a simple example of an echo function, which gets a string and return the same string):

[OperationInfo(OperationInfo.VisibilityType.GLOBAL, "echo", IDoEcho)]
    public static string DoEcho(string a)
    {
        return a;
    }

    [OperationInfo(OperationInfo.VisibilityType.PRIVATE, null, null)]
    public static List<string> IDoEcho(List<string> param) {
        return new List<string>() { DoEcho(param[0]) };
    }

I’ve got no error in my Annotation class but when it comes to regenerate the entire solution, each time I declare a method with the annotation I’ve got an error which tells me that I must use literals in an annotation.

I understand there is limitations, but is there any way I can avoid this problem? I know I can use a string instead of a Func, and look dynamically for the function whose name is the same as the string, but I would like not to do so.

Thanks for helping 🙂

  • 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-27T14:43:44+00:00Added an answer on May 27, 2026 at 2:43 pm

    I don’t think it is possible, c# only allows you to use Attributes with constant values only.

    One way of doing this would be to create a class instead of a func, and then you pass the type of that class into the attribute.
    You’d then need to instantiate it with reflection and execute a method in it.

    So an example would be:

    public interface IDoStuff
    {
        IList<string> Execute();
    }
    

    then your attribute would take a type instead of a Func

    public OperationInfo(VisibilityType v, string title, Type type)
    {
       ///...
    }
    

    you then would create a class that implements your interface and pass it into the attribute. Although this would mean that (at least initially, and without knowing your problem) you will need to create a new class every time you want to return different results.

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

Sidebar

Related Questions

I have the following java class with the JAXB @XMLRootElement annotation @XmlRootElement(name=ClientData) public class
We have an attribute called DateReleased for which the following Data Annotation attributes are
I have a ParentBean class which has the @ManagedBean annotation. If I subclass this
I have the following class (which is a JPA entity listener): @Service public class
In C# I have the following attribute above a method: [Test, SetCulture(en-GB)] which, I
I have a mapview with several annotations. Every annotation has a leftCalloutAccessoryView which is
I have a method annotated with @PrePersist annotation but I've seen that when this
Let's say I have an annotation JSR 303 class like this: class A {
I have a mapview and i put an annotation which shows a custom title
Suppose I have the following XML (which is the embedding of TEI annotation scheme

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.