Using two attributes in 1 field.

  • Thread starter Thread starter wesley
  • Start date Start date
W

wesley

Hello,

I want to do the followling:

[XmlIgnoreAttribute]
[NonSerialized]
public string Id
{
get { return id; }
set { id = value; }
}

But the compiler won't let me have more than 1 attribute there. How do I
apply both attribute in my fields?

Thanks,
wes
 
What is the error. Attributes can be written to only work on certain
objects. Maybe one of those attributes cannot work on a property.

....

After looking at the documentation for NonSerialization, it can only be
applied to fields, not properties.
 
wesley said:
I want to do the followling:

[XmlIgnoreAttribute]
[NonSerialized]
public string Id
{
get { return id; }
set { id = value; }
}

But the compiler won't let me have more than 1 attribute there. How do I
apply both attribute in my fields?

The compiler message I get is fairly clear:

<quote>
Test.cs(9,6): error CS0592: Attribute 'NonSerialized' is not valid on
this declaration type. It is valid on 'field' declarations only.
</quote>

It's got nothing to do with having multiple attributes - it's just that
Id is a property, not a field, and the NonSerialized attribute can only
apply to fields.
 
Back
Top