Fixed Value Handling in LinqToXsd
LinqToXsd does have special code generation logic for fixed values on attributes and elements, but there is a nuance in how it interacts with complex type derivation by restriction (as seen in metalex_mcontainerTypeBug.xsd).
1. General Handling of Fixed Values for Attributes
When an attribute has fixed="value" in an XSD schema:
-
Extraction (XsdToTypesConverter.cs):
- In
SetFixedDefaultValue(XmlSchemaAttribute, ClrPropertyInfo), attribute.FixedValue is read and stored on ClrPropertyInfo.FixedValue.
-
Constant Field Emission (ClrPropertyInfo.CreateFixedDefaultValue):
- LinqToXsd generates a static
[DebuggerBrowsable(Never)] field holding the fixed value (e.g., <propertyName>FixedValue).
-
Getter Behavior (ClrPropertyInfo.AddGetStatements / ReturnFixedValue):
- For attributes with a fixed value, the getter skips reading from the underlying XML attribute and directly returns the fixed value constant (
return <propertyName>FixedValue;), adhering to XSD semantics where fixed attributes always evaluate to that fixed value.
-
Setter Validation (ClrPropertyInfo.AddFixedValueChecking):
- The setter generates an equality check against the fixed constant. If the assigned value does not match the fixed value, it throws a
LinqToXsdFixedValueException:
if (typeFixedValue.Equals(value)) {
// set attribute
} else {
throw new LinqToXsdFixedValueException(value, typeFixedValue);
}
2. Why It Is Not Applied in metalex_mcontainerTypeBug.xsd
In metalex_mcontainerTypeBug.xsd:
-
urType definition:
urType defines type through the typeopt attribute group without a fixed value (<xsd:attribute name="type" type="e:CMtype"/>).
- LinqToXsd generates a normal, read/write
type property on the C# urType class.
-
absMcontainerType restriction:
absMcontainerType derives from urType using <xsd:restriction base="e:urType"> and includes typemcontainer, which sets fixed="mcontainer" on the type attribute.
- However, in
XsdToTypesConverter.cs (lines 306–310):
if (ct.IsDerivedByRestriction())
{
//Do not handle restrictions on complex content?
return;
}
- LinqToXsd deliberately skips generating child particle and attribute properties for complex types derived by restriction from another complex type. Instead, the generated C# class (
absMcontainerType) simply inherits from urType (public partial class absMcontainerType : global::MetalEx_mcontainerBug.urType).
- As a result,
absMcontainerType inherits the unconstrained type property from urType rather than generating a specialized or overridden type property with the fixed value logic.
Fixed Value Handling in LinqToXsd
LinqToXsd does have special code generation logic for fixed values on attributes and elements, but there is a nuance in how it interacts with complex type derivation by restriction (as seen in
metalex_mcontainerTypeBug.xsd).1. General Handling of Fixed Values for Attributes
When an attribute has
fixed="value"in an XSD schema:Extraction (
XsdToTypesConverter.cs):SetFixedDefaultValue(XmlSchemaAttribute, ClrPropertyInfo),attribute.FixedValueis read and stored onClrPropertyInfo.FixedValue.Constant Field Emission (
ClrPropertyInfo.CreateFixedDefaultValue):[DebuggerBrowsable(Never)]field holding the fixed value (e.g.,<propertyName>FixedValue).Getter Behavior (
ClrPropertyInfo.AddGetStatements/ReturnFixedValue):return <propertyName>FixedValue;), adhering to XSD semantics where fixed attributes always evaluate to that fixed value.Setter Validation (
ClrPropertyInfo.AddFixedValueChecking):LinqToXsdFixedValueException:2. Why It Is Not Applied in
metalex_mcontainerTypeBug.xsdIn
metalex_mcontainerTypeBug.xsd:urTypedefinition:urTypedefinestypethrough thetypeoptattribute group without a fixed value (<xsd:attribute name="type" type="e:CMtype"/>).typeproperty on the C#urTypeclass.absMcontainerTyperestriction:absMcontainerTypederives fromurTypeusing<xsd:restriction base="e:urType">and includestypemcontainer, which setsfixed="mcontainer"on thetypeattribute.XsdToTypesConverter.cs(lines 306–310):absMcontainerType) simply inherits fromurType(public partial class absMcontainerType : global::MetalEx_mcontainerBug.urType).absMcontainerTypeinherits the unconstrainedtypeproperty fromurTyperather than generating a specialized or overriddentypeproperty with the fixed value logic.