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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:49:54+00:00 2026-06-09T00:49:54+00:00

[MyAttribute(Name = Test123] public MyClass { public string Name { get { return Test123;

  • 0
[MyAttribute(Name = "Test123"]
public MyClass
{
    public string Name 
    {
       get { return "Test123"; }
    }
}

Is this code sample, I need to define the name two times. One time in the attribute and one time in the class itself. Is there a way to avoid this kind of duplication?

I need both because sometime, I’m iterating through the assembly to find all class that use MyAttribute and to list them (with the name). Other time, I’ve access to the instantiated object so I don’t want to use the attribute.

  • 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-06-09T00:49:57+00:00Added an answer on June 9, 2026 at 12:49 am

    If the name can be put into a string constant, then you can use that constant for both the attribute parameter and also the return value of the property.

    If you ever need to change the name, just change the const value.

    [MyAttribute(MyClass.MyClassName)]
    public MyClass
    {
        private const string MyClassName = "Test123";
    
        public string Name 
        {
           get { return MyClass.MyClassName; }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[MyAttribute()] public string Name { get; set; } In MyAttribute I need to know
I have read a parent Class called MyClass code like this, public class MyClass
Say I have a situation like so: [MyAttribute] public class MyClass { } [AttributeUssage(AttributeTargets.Class)]
How can I get the PropertyDescriptor for the current property? For example: [MyAttribute(SomeText)] public
I've got some aspect like this: public class MyAttribute : OnMethodInvocationAspect { public int
I have a controller [MyAttribute] public class MyController: Controller { public MyController(InterfaceA a, InterfaceB
Example: [AttributeUsage(AttributeTargets.Class)] public class MyAttribute : Attribute { public MyAttribute() { Console.WriteLine(I was defined
Let say I got the following : public class A { [MyAttribute] public void
Previously on #ruby somebody told me that I do not need to use @name
I have created an Attribute, call MyAttribute, which is performing some security and for

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.