473,324 Members | 2,246 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,324 software developers and data experts.

Attributes with multiple objects as param for constructor

I am using an attribute on the properties of my object to help with
parsing of data. The attribute will determine if the current property
is to be populated based on the version of the data. As such I need to
be able to create the attribute with the version and some other values
for the version. So I tried the following (typed for the example, not
the actual code).

defined the constructor for the Attrib as:
public MyAttrib(params object[] versions)

and defined the class as:
public MyClass
{
private string prop1;

[MyAttribute(new VersionInfo("1.0", "Description"), new
VersionInfo("2.0", "Description"))]
public string Prop1
{
get { return prop1; }
set { prop1 = value; }
}
}

The class fails to compile with the follwoing error:
An attribute argument must be a constant expression, typeof
expression or array creation expression

Have I defined something incorrectly? Is this sort of thing supported
at all in Dot net?

If it can't be done as described above, any suggestion on how to
accomplish the desired task?

Thanks

Jun 8 '06 #1
3 2269
>Have I defined something incorrectly? Is this sort of thing supported
at all in Dot net?


Not the way you want it, no. Have you considered using multiple
attribute instances instead, one per VersionInfo?
public MyAttribute(string versionNumber, string description)

....

[MyAttribute("1.0", "Description"), MyAttribute("2.0", "Description")]
public string Prop1

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 8 '06 #2
Attributes are evaluated at compile-time, so values must be able to evaluate
to compile-time constants.

You are creating an object with a dynamic allocation operator ("new"). This
is only supported at run-time, so cannot be compiled. If you change your
attribute to take version string constants and create the VersionInfo objects
at run-time (when you'll really use them), you will probably have better luck.

-dM

"ThisBytes5" wrote:
I am using an attribute on the properties of my object to help with
parsing of data. The attribute will determine if the current property
is to be populated based on the version of the data. As such I need to
be able to create the attribute with the version and some other values
for the version. So I tried the following (typed for the example, not
the actual code).

defined the constructor for the Attrib as:
public MyAttrib(params object[] versions)

and defined the class as:
public MyClass
{
private string prop1;

[MyAttribute(new VersionInfo("1.0", "Description"), new
VersionInfo("2.0", "Description"))]
public string Prop1
{
get { return prop1; }
set { prop1 = value; }
}
}

The class fails to compile with the follwoing error:
An attribute argument must be a constant expression, typeof
expression or array creation expression

Have I defined something incorrectly? Is this sort of thing supported
at all in Dot net?

If it can't be done as described above, any suggestion on how to
accomplish the desired task?

Thanks

Jun 8 '06 #3
I had trid this in the past, but assumed it wouldn't work as I got an
error of multiple attributes defined. Today I hit F1 on the error and
saw I can specify that I want to allow multiples. Set that to true and
it all works!!!

Thanks
Wayne
Mattias Sjögren wrote:
Have I defined something incorrectly? Is this sort of thing supported
at all in Dot net?


Not the way you want it, no. Have you considered using multiple
attribute instances instead, one per VersionInfo?
public MyAttribute(string versionNumber, string description)

...

[MyAttribute("1.0", "Description"), MyAttribute("2.0", "Description")]
public string Prop1

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Jun 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
2
by: monsterpot | last post by:
Hello, I have a document like this: <parameters> <param ptype="init">somedata</param> <param ptype="process">moredata</param> <param ptype="kill">alsodata</param> </parameters> <param> has...
5
by: johkar | last post by:
This script works fine if all form fields have one value, but what do you do if one of the form fields was a multiple select? Example: status=blue&status=red function getParams() { var...
2
by: Barry Moon | last post by:
Hi Can anyone give me any help with passing an object across processes, via drag and drop? I've written a custom ListView control, which supports dragging and dropping of its items. The...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
1
by: LG | last post by:
I want to add attributes to a custom dropdownlist and I found some code on Internet that doesn't work after post back. First time the control has the attributes, but after postback the attributes...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
4
by: Inso Reiges | last post by:
Hi, I`m new to C++ and OOP in general and i come from long-time C mindset. So i have some style questions. I have a class that needs to be initialized by over nine parameters. So i decided to...
0
by: cdelautour | last post by:
Hi there, I am currently working with the property grid and I am implementing an indexed category system (sorts categories via index before alphabetically). I should say that at this point my...
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...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.