473,322 Members | 1,473 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,322 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 7008
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.