469,623 Members | 1,380 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Custom Parameter Attribute for Set Accessors

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
Nov 15 '05 #1
4 6839
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" <an*******@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
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

Nov 15 '05 #2
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" <an*******@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
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

Nov 15 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
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


Nov 15 '05 #4
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" <an*******@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
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


Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by M | last post: by
reply views Thread by Ben Blair | last post: by
3 posts views Thread by Ryan Moore | last post: by
1 post views Thread by Eldon Ferran de Pol | last post: by
8 posts views Thread by a | last post: by
reply views Thread by a | last post: by
5 posts views Thread by =?Utf-8?B?cGFnYXRlcw==?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.