ForAttributeAttribute Class
Marks a constructor parameter to be bound to the XML attribute.
[AttributeUsage(System.AttributeTargets.Parameter, Inherited = false, AllowMultiple = false)]
public sealed class ForAttributeAttribute : Attribute
- Inheritance
- Attributes
Examples
[ForElement("person")]
public record class Person(
[ForAttribute("id")] string? Id,
[ForAttribute("name")] string? Name);
// Corresponding XML:
// <person id="42" name="John Doe"/>
Remarks
The ForAttributeAttribute associates a constructor parameter
with an XML attribute name and namespace. Use an empty string (""
)
for ns
to specify the null namespace (i.e., no
namespace).
If the Oxbinder<T> encounters an XML attribute, it populates the constructor parameter corresponding to the attribute name with the attribute's value.
This attribute must mark a constructor parameter and the parameter must be one of the following types: Additionally, the class containing this constructor must be annotated with ForElementAttribute.
The attribute name specified in ForAttributeAttribute must be
unique within a single class. For example, if a constructor parameter is
annotated with [ForAttribute("foo")]
, no other parameter in the same
class can be annotated with [ForAttribute("foo")]
.
All parameters marked with ForAttributeAttribute must be placed at the beginning of the constructor parameter list, before any other parameters marked with other Oxbind binding attributes (such as RequiredAttribute, OptionalAttribute, MultipleAttribute, or ForTextAttribute).
If the XML attribute does not exist in the element, the parameter is
populated with null
. Therefore, the parameter type must
be a nullable reference type.
Constructors
For |
Marks a constructor parameter to be bound to the XML attribute. |
Properties
Methods
Equals(object) | (Inherited from Attribute) |
GetHashCode() | (Inherited from Attribute) |
IsDefaultAttribute() | (Inherited from Attribute) |
Match(object) | (Inherited from Attribute) |
GetType() | (Inherited from object) |
MemberwiseClone() | (Inherited from object) |
ToString() | (Inherited from object) |
Constructors Detail
ForAttributeAttribute(string, string)
Marks a constructor parameter to be bound to the XML attribute.
public ForAttributeAttribute(string name, [string ns = ""])
Parameters
- name
- string
The attribute name. This must be a valid XML attribute name.
- ns
- string
The namespace URI. Use an empty string ("") for attributes without a namespace.
Remarks
The ForAttributeAttribute associates a constructor parameter
with an XML attribute name and namespace. Use an empty string (""
)
for ns
to specify the null namespace (i.e., no
namespace).
If the Oxbinder<T> encounters an XML attribute, it populates the constructor parameter corresponding to the attribute name with the attribute's value.
This attribute must mark a constructor parameter and the parameter must be one of the following types: Additionally, the class containing this constructor must be annotated with ForElementAttribute.
The attribute name specified in ForAttributeAttribute must be
unique within a single class. For example, if a constructor parameter is
annotated with [ForAttribute("foo")]
, no other parameter in the same
class can be annotated with [ForAttribute("foo")]
.
All parameters marked with ForAttributeAttribute must be placed at the beginning of the constructor parameter list, before any other parameters marked with other Oxbind binding attributes (such as RequiredAttribute, OptionalAttribute, MultipleAttribute, or ForTextAttribute).
If the XML attribute does not exist in the element, the parameter is
populated with null
. Therefore, the parameter type must
be a nullable reference type.
Examples
[ForElement("person")]
public record class Person(
[ForAttribute("id")] string? Id,
[ForAttribute("name")] string? Name);
// Corresponding XML:
// <person id="42" name="John Doe"/>
Properties Detail
QName
Gets the qualified name of the attribute.
public XmlQualifiedName QName { get; }