0

I have a class which has a property that has an attribute on it:

public class MyClass
{
    [CustomAttribute]
    public CustomObject Data { get; set; }
}

The CustomAttribute has a parameter who's value I'd like to be able to be set at various points in code with a compile-time constant.

My question is: Is it possible to do something like shown below

public class MyClass
{
    // --- Based on the below code, Parameter should be 123
    [CustomAttribute(Parameter = HelperAttribute.Value)]
    public CustomObject Data { get; set; }
}

// ...

// --- Create a new instance of MyClass, and use the value 
// --- 123 for Data's CustomAttribute Parameter.
[HelperAttribute(Value = 123)]
public MyClass Obj { get; set; }

Note: I am aware that I can't do the reflection part at compile time. I'm curious if there's a way to do it that's built into C#.

David
  • 4,744
  • 5
  • 33
  • 64
  • How would the compiler know which "instance" of `HelperAttribute.Value` to use if you used that attribute multiple times? – Kenneth K. Nov 30 '17 at 23:48
  • What stack is this? You would use a feature from the stack you're using to pass values around (ex. in Web Api 2 it'd be httpcontext). – RandomUs1r Nov 30 '17 at 23:51
  • @RandomUs1r, basic C#. – David Nov 30 '17 at 23:51
  • @KennethK. compilers are really smart is the best answer I have. Although, you do bring up a good point. – David Nov 30 '17 at 23:53
  • @David Ah above my head then, never tried... take a look at https://stackoverflow.com/questions/6637679/reflection-get-attribute-name-and-value-on-property and https://stackoverflow.com/questions/6637679/reflection-get-attribute-name-and-value-on-property . Notice the common theme of reflection, not sure past that. – RandomUs1r Nov 30 '17 at 23:56

4 Answers4

0

this is a good idea. But it make no sence for attribute designer. Attribute value should be retrived by runtine compiler metadata by reflection then to create the attribute instance.

Raymond He
  • 127
  • 5
0

I think it is not possible to change attribute property value at run-time with reflection because attributes are meta-data serialized which are used at compile-time

esiprogrammer
  • 1,438
  • 1
  • 17
  • 22
  • Yeah, I know I can't use reflection. I was hoping there was some way built into C# to do it. The code I've shown is all compile time constants, it would just allow one instance to be 123, and another to be 321. – David Nov 30 '17 at 23:46
0

Attributes are associated with the type (the class), not the instance. There is one value and it is fixed at compile time.

You could however use the attribute to initialize the value.

[MyInitializer(Value = "Test")]
class Test
{
    public Test()
    {
        this.Value = this.GetType().GetAttributes().OfType<MyInitializerAttribute>().Value;
    }

    public string Value { get; set; }
}

I don't much see the point of this, as you could just as easily initialize it to a constant value, which would require less typing than adding the attribute, and probably be clearer.

class Test
{
    public Test()
    {
        this.Value = "Test"; 
    }

    public string Value { get; set; }
}

or with C# 6

class Test
{
    public string Value { get; set; } = "Test";
}
John Wu
  • 50,556
  • 8
  • 44
  • 80
0

This is not possible, since the attribute constructor only allows compile time constants. You can't even do something like this:

public class MyClass
{
    static int test = 5;
    [CustomAttribute(Parameter = test)]
    public object Data { get; set; }
}
adjan
  • 13,371
  • 2
  • 31
  • 48
  • Technically, everything in my code is a compile time constant. – David Nov 30 '17 at 23:52
  • no, HelperAttribute.Value would have to be a variable. How else would you set it in the constructor? – adjan Nov 30 '17 at 23:53
  • That specific part is fake, since I didn't know if there was a right way (I'm assuming there's not at this point). – David Nov 30 '17 at 23:54