简体   繁体   English

当它们共享相同的名称时,如何告诉C#编译器符号是类型而不是变量?

[英]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()) A->错误(无方法DoSomething())
  • B -> Works B->作品
  • C -> Works C->作品
  • D -> error (no method Trim()) D->错误(无方法Trim())

From a more applicative point of view 从更实用的角度来看

(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.

相关问题 如何将C#变量声明为HTML类型变量? - How do you declare a C# variable into an HTML type variable? C#中类型名称中的&lt;&gt;符号 - <> symbol in type name in C# 当c#中没有扩展名时,如何检查文件类型? - How do you check a file type when there is no extension in c# 当JSON变量名称无效且根数组不在键,值对中时,如何在C#中反序列化JSON? - How do you deserialize JSON in C# when a JSON variable name is invalid and the root array isn't in Key, Value pairs? 如何将 at 符号 (@) 和美元符号 ($) 与 C# 字符串结合起来? - How do you combine the at symbol (@) and the dollar sign ($) with C# strings? 如何在C#中共享MS Access数据库文件? - How do you share a MS Access Database File in C#? 如何告诉C#编译器不要优化未使用的类? - How do I tell the C# compiler not to optimize away an unused class? 如何告知编译器我的可空 c# 通用约束? - How do I tell the compiler about my nullable c# generic constraints? 如何在 C# 中运行可变数量的并发参数化无限循环类型的线程? - How do you run a variable number of concurrent parametrizable infinite loop type of threads in C#? 当您在另一个变量中有名称时,如何设置 C# 4 动态 object 的属性 - How to set a property of a C# 4 dynamic object when you have the name in another variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM