473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 7009
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: M | last post by:
Is it possible to define an attribute that describes a property of a class at runtime? For example, you have a class called "employee", and it has a property "name". The "name" property has an...
0
by: Ben Blair | last post by:
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...
0
by: RYoung | last post by:
I have a custom attribute with an "IsPresent" property with get; set accessors. When the attribute is applied to a field of a class, IsPresent will be false by default. At runtime, I'll read in...
3
by: Ryan Moore | last post by:
I am trying to "tune" some user controls in ASP.NET for optimal caching. Is it possible to do a vary by param - on a Session Variable, so the page caches when a Session variable stays the same,...
1
by: Eldon Ferran de Pol | last post by:
Hi all I've got a custom control that has several image based properties I wan't each of these properties to use the standard .Net dialog box for selecting a URL that appears when setting the...
8
by: a | last post by:
I'm trying to save data from a custom object into the profile object, but it is not structured the way that I want. I'm trying to get the custom object to serialize as xml to a Profile object...
0
by: a | last post by:
I need to create an instance of a custom object 'School.Teacher' and use it in a Profile object. I'm developing a bad case of "Pretzel Logic" thinking about this. Filling the custom object ...
1
by: bg | last post by:
hi all, I have this: // custom attribute internal class ThingAttribute : System.Attribute { ... } enum LotsOfThing
5
by: =?Utf-8?B?cGFnYXRlcw==?= | last post by:
Hello All, I am sure that I am just overlooking something, but here's something I can't quite get right... I want to be able to get the value of a parameter of an unknown custom attribute at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.