[英]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
(you may want to skip this if XSD bores you): (如果XSD让您感到厌烦,则可以跳过此步骤):
I am currently trying to get LINQ to XSD working. 我目前正在尝试使LINQ to XSD工作。 In my XSD file there are xs:elements like this:
在我的XSD文件中,有xs:elements像这样:
<xs:element name="car" type="car">
Where the 'car' type is defined as a simpleType like this 像这样将“汽车”类型定义为simpleType的情况
(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: 因此,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);
}
}
But this won't compile due to the aforementioned problem. 但这由于上述问题而无法编译。
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). 如果类型没有名称空间,则可以在其前面加上
global::
:(无论如何在C#中)。
You can look into C# Specification to get more information on this behavior. 您可以研究C#规范以获取有关此行为的更多信息。 Here is the start of the chapter that describes it:
这是描述它的章节的开头:
7.3 Member lookup
7.3会员查询
A member lookup is the process whereby the meaning of a name in the context of a type is determined.成员查找是确定类型上下文中名称含义的过程。 A member lookup can occur as part of evaluating a simple-name (§7.5.2) or a member-access (§7.5.4) in an expression.
成员查找可以作为评估表达式中的简单名称(第7.5.2节)或成员访问(第7.5.4节)的一部分而发生。 If the simple-name or member-access occurs as the simple-expression of an invocation-expression (§7.5.5.1), the member is said to be invoked.
如果简单名称或成员访问作为调用表达式的简单表达式(第7.5.5.1节)发生,则称该成员已被调用。 If a member is a method or event, or if it is a constant, field or property of a delegate type (§15), then the member is said to be invocable.
如果成员是方法或事件,或者它是委托类型的常量,字段或属性(第15节),则该成员被称为可调用的。 Member lookup considers not only the name of a member but also the number of type parameters the member has and whether the member is accessible.
成员查找不仅考虑成员的名称,还考虑该成员具有的类型参数的数量以及该成员是否可访问。 For the purposes of member lookup, generic methods and nested generic types have the number of type parameters indicated in their respective declarations and all other members have zero type parameters.
为了成员查找的目的,泛型方法和嵌套的泛型类型在其各自的声明中指定了类型参数的数量,而所有其他成员的类型参数均为零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.