Custom Parameter Attribute for Set Accessors

G

Guest

Hi.

How does one assign a custom parameter attribute to the implicit "value" parameter of the set accessor method of a property? For example, I can assign a custom attribute to the return parameter of the get accessor as follows:

public string Foo
{
[return: MyAttribute]
get
{
return "foo";
}
set
{
m_foo = value;
}
}

Likewise, I can assign attributes to method parameters, as follows:

public void set_Foo([MyAttribute] string value)
{
m_foo = value;
}

I know how to do this with Reflection.Emit by explicitly assigning the attribute to the set_Foo(string value) method. But how can I do it directly in the C# source? Thank you for your help.

Regards,

- Ben Blair
{my name} {at} acm.org
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Ben,

From c# specs:
"An attribute specified on a set accessor for a property or indexer
declaration can apply either to the associated method or to its lone
implicit parameter. In the absence of an attribute-target-specifier, the
attribute applies to the method. The presence of the method
attribute-target-specifier indicates that the attribute applies to the
method; the presence of the param attribute-target-specifier indicates that
the attribute applies to the parameter."

if you want to apply an attribute to the set accessor parameter you should
do:
public int Prop
{
get{return 100;}
[param: Foo]
set
{
Console.WriteLine();
}
}


--
HTH
B\rgds
100

Ben Blair said:
Hi.

How does one assign a custom parameter attribute to the implicit "value"
parameter of the set accessor method of a property? For example, I can
assign a custom attribute to the return parameter of the get accessor as
follows:
public string Foo
{
[return: MyAttribute]
get
{
return "foo";
}
set
{
m_foo = value;
}
}

Likewise, I can assign attributes to method parameters, as follows:

public void set_Foo([MyAttribute] string value)
{
m_foo = value;
}

I know how to do this with Reflection.Emit by explicitly assigning the
attribute to the set_Foo(string value) method. But how can I do it directly
in the C# source? Thank you for your help.
 
G

Guest

Stoitcho,

Thanks for the quick and comprehensive reply. I probably should have searched the spec before posting my question, but your help is really appreciated. That's exactly what I wanted.

Regards,

- Ben Blair
{My Name} {at} acm.org

----- Stoitcho Goutsev (100) [C# MVP] wrote: -----

Hi Ben,

From c# specs:
"An attribute specified on a set accessor for a property or indexer
declaration can apply either to the associated method or to its lone
implicit parameter. In the absence of an attribute-target-specifier, the
attribute applies to the method. The presence of the method
attribute-target-specifier indicates that the attribute applies to the
method; the presence of the param attribute-target-specifier indicates that
the attribute applies to the parameter."

if you want to apply an attribute to the set accessor parameter you should
do:
public int Prop
{
get{return 100;}
[param: Foo]
set
{
Console.WriteLine();
}
}


--
HTH
B\rgds
100

Ben Blair said:
parameter of the set accessor method of a property? For example, I can
assign a custom attribute to the return parameter of the get accessor as
follows:
public string Foo
{
[return: MyAttribute]
get
{
return "foo";
}
set
{
m_foo = value;
}
}
Likewise, I can assign attributes to method parameters, as follows:
public void set_Foo([MyAttribute] string value)
{
m_foo = value;
}
I know how to do this with Reflection.Emit by explicitly assigning the
attribute to the set_Foo(string value) method. But how can I do it directly
in the C# source? Thank you for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top