Search code examples
c#.netlinq-to-xsd

How do you tell the C# compiler that a symbol is a type and not a variable when they share the same name?


When you have a local variable named the same as a type, is there any way to tell the compiler that the symbol you have given is a type or a variable? For instance consider (and ignore all type return errors):

public class sometype { public static sometype DoSomething() {} }

public string sometype { get { return sometype.DoSomething(); } } //A
public string sometype { get { return sometype.Trim(); } } //B
public sometype sometype { get { return sometype.DoSomething(); } } //C
public sometype sometype { get { return sometype.Trim(); } } //D
  • A -> Error (no method DoSomething())
  • B -> Works
  • C -> Works
  • D -> error (no method Trim())

From a more applicative point of view

(you may want to skip this if XSD bores you):

I am currently trying to get LINQ to XSD working. In my XSD file there are xs:elements like this:

<xs:element name="car" type="car">

Where the 'car' type is defined as a simpleType like this
(probably some more restrictions but this is it in essence):

<xs:simpleType name="car">
 <xs:restriction base="xs:string" />
</xs:simpleType>

So naturally LINQ to XSD generates code that looks like this:

public string car {
    get {
        XElement x = this.GetElement(XName.Get("car", ""));
        return XTypedServices.ParseValue<string>(x, XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).Datatype);
    }
    set {
        this.SetElementWithValidation(XName.Get("car", ""), value, "car", car.TypeDefinition);
    }
}

But this won't compile due to the aforementioned problem.


Solution

  • You should fully qualify the namespace of the type.

    If the type does not have a namespace, then you can prefix it with global:: (in C# anyway).