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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:31:42+00:00 2026-05-25T21:31:42+00:00

Consider the following attribute. internal class NiceAttribute : Attribute { private string _stuff; public

  • 0

Consider the following attribute.

internal class NiceAttribute : Attribute
{
  private string _stuff;

  public string Stuff
  {
    set { _stuff = value; }
  }
}

When I try to use the attribute [Nice(Stuff = "test")] the compiler gives the following error.

‘Stuff’ is not a valid named attribute argument. Named attribute
arguments must be fields which are not readonly, static, or const, or
read-write properties which are public and not static.

What is the rational behind the requirement for the property to be readable?


Update
I will try to sketch my use case for having write-only properties on attributes.

interface ISettingsBuilder
{
  Settings GetSettings();
}

class SettingsAttribute : Attribute, ISettingsBuilder
{
  private readonly IDictionary<string, object> _settings =
    new Dictionary<string, object>();

  public Settings GetSettings()
  {
    // Use _settings to create an immutable instance of Settings
  }

  public string Stuff
  {
    set { _settings["Stuff"] = value; }
  }

  // More properties ...
}

There may be other implementations of ISettingsBuilder. For example one that offers a nice API to build settings through code.

I ended up with implementing my getters by throwing a NotImplementedException.

  public string Stuff
  {
    get { throw new NotImplementedException(); }
    set { _settings["Stuff"] = value; }
  }

Can you think of a nicer way to do something like this?

  • 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-25T21:31:42+00:00Added an answer on May 25, 2026 at 9:31 pm

    I suspect the compiler is using a slightly misguided check to see whether you are accessing a private property here

    Edit “we” have now located the actual source. For informational purposes, here is the full breakdown, but feel free to skip to the bottom.
    (note how bug should be filed against the Mono compiler. I’ll think that one over for a while)

    Compiler Error CS0617

    • http://msdn.microsoft.com/en-us/library/978b3z1b(v=vs.71).aspx

    ‘reference’ is not a valid named attribute argument. Named attribute arguments must be fields which are not readonly, static or const, or read-write properties which are not static.

    An attempt was made to access a private member of an attribute class.

    It could seem that it is using some kind of lookup (akin to reflection) to make sure that the getter is accessible and if it isn’t, concludes that it must be private.

    Which, of course, it doesn’t need to be 🙂

    Mono Compatibility:

    For fun, observe that the mono compiler has no problem whatsoever accepting this attribute: https://ideone.com/45fCX

    Because of Reflection:

    Of course it could be that the compiler requires attribute parameters to have reflectable values. If the property wasn’t publicly readable, you could only use reflection to ‘observe’ that the attribute is present, not with what parameter it was initialized.

    I don’t know exactly, why such a design choicde would have been made, but it does make sense if you factor in reflection usage.

    Update @Arun posted the relevant quote that confirms this conjecture (thanks!):

    Accessing Attributes Through Reflection Once attributes have been associated with program elements, reflection can be used to query their existence and values. The main reflection methods to query attributes are contained in the System.Reflection.MemberInfo class (GetCustomAttributes family of methods).

    So the reason must be: Attribute parameters must have reflectible values

    Prize question: How does that work with positional parameters? How would one reflect those?

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

Sidebar

Related Questions

Consider the following: [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue)] public class NotNullAttribute : Attribute { } public
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
consider the following class and struct public class Entity { public IdType Id {get;set;}
Consider the following IOperationBehavior attributes: public class OperationCredentialsAttribute : Attribute, IOperationBehavior { } public
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following simple example: [DataContract({0}Base)] public class Base<T> where T : Entity<T> {
consider the following test class: [TestClass] public class ExampleTests { [TestMethod] public void FileDoesNotExists()
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider the following code in Python: class A(object): CLASS_ATTRIBUTE = 42 def f(self): return
Consider the following entity class, used with, for example, EclipseLink 2.0.2 - where the

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.