当你有一个与类型同名的局部变量时,有没有办法告诉编译器你给出的符号是类型还是变量?例如考虑(并忽略所有类型返回错误):
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 -> 错误(没有方法 DoSomething())
- B -> 作品
- C -> 作品
- D -> 错误(没有方法 Trim())
从更适用的角度来看
(如果 XSD 让你感到厌烦,你可能想跳过这个):
我目前正在尝试让 LINQ to XSD 正常工作。在我的 XSD 文件中有这样的 xs:elements:
<xs:element name="car" type="car">
'car' 类型被定义为这样的 simpleType
(可能还有一些限制,但本质上就是这样):
<xs:simpleType name="car">
<xs:restriction base="xs:string" />
</xs:simpleType>
所以很自然地 LINQ to XSD 会生成如下所示的代码:
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);
}
}
但是由于上述问题,这不会编译。