How to init named param for attribute with object type

S

Sajjad Akhter

If i try to set any named param in attribute declartion which is not string
or number type i get error that its not valid param etc

here is example code

I need to create some constant fields those are of object type (other than
value types)



public class TestData10

{

public TestData10(string name)

{}

public string Airport;

}





Class MyAttribute: Attribute

{



Public TestData10 Data{get {return new TestData10(); } set{}}

}



Class test

{



[My(Data= new TestData10 () )]

Void DoesntWorkBeacuseExpressionIsNotConst() {}



[My(Data.Airport = "Sea"]

Void
AgainGivesStrangeErrorsThatDataIsNotApproprieatparamEtc(){}



}
 
N

Nicholas Paldino [.NET/C# MVP]

Sajjad,

What you are doing can not be done. Attributes are part of the type
definition, and as such, need to be evaluated at compile time. Because of
this, you need to use constants when declaring them, which you can not do
with objects (using new).

Hope this helps.
 
S

Sajjad Akhter

how about
[My(Data.Airport = "Sea"]

i couldnt get whats the problem in this case

Nicholas Paldino said:
Sajjad,

What you are doing can not be done. Attributes are part of the type
definition, and as such, need to be evaluated at compile time. Because of
this, you need to use constants when declaring them, which you can not do
with objects (using new).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sajjad Akhter said:
If i try to set any named param in attribute declartion which is not
string or number type i get error that its not valid param etc

here is example code

I need to create some constant fields those are of object type (other
than value types)



public class TestData10

{

public TestData10(string name)

{}

public string Airport;

}





Class MyAttribute: Attribute

{



Public TestData10 Data{get {return new TestData10(); } set{}}

}



Class test

{



[My(Data= new TestData10 () )]

Void DoesntWorkBeacuseExpressionIsNotConst() {}



[My(Data.Airport = "Sea"]

Void
AgainGivesStrangeErrorsThatDataIsNotApproprieatparamEtc(){}



}
 
K

Kevin Yu [MSFT]

Hi Sajjad,

Based on my research, this seems to be not supported in C#.

The types of positional and named parameters for an attribute class are
limited to the attribute parameter types, which are:

1. One of the following types: bool, byte, char, double, float, int, long,
short, string.
2. The type object.
3. The type System.Type.
4. An enum type, provided it has public accessibility and the types in
which it is nested (if any) also have public accessibility (Section 17.2).
5. Single-dimensional arrays of the above types.

Since a custom type is not listed, I don't think it possible. For more
information, please check the following link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html
/vclrfcsharpspec_17_1_3.asp

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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