简体   繁体   English

具有基类限定符的Enum TryParse

[英]Enum TryParse with base class qualifier

I have an enum (let's say DemoEnum) and I want to parse a value to this enum. 我有一个枚举(比如说DemoEnum),我想解析一个值给这个枚举。 I am writing 我正在写

DemoEnum value;
if(DemoEnum.TryParse("input", out value))
{
   this.Value = value;
}

Now the resharper suggests me, to use the base class qualifier. 现在,resharper建议我使用基类限定符。

I just want to know what's the benefit of using the base class qualifier? 我只想知道使用基类限定符有什么好处?

Generally, it is a good idea to use the most generic solution possible. 通常,最好使用最通用的解决方案。

DemoEnum.TryParse("input", out value)

Is the same call as (you're just making the static call from an inherited class rather than the base class): 与(与您只是从继承的类而不是基类进行静态调用)相同:

Enum.TryParse<DemoEnum>("input", out value)

Using the base class qualifier ( Enum ) instead of your specific enum ( DemoEnum ) would insulate you from possible side effects of changing DemoEnum in the future. 使用基类限定符( Enum )而不是特定的枚举( DemoEnum )将使您免受将来更改DemoEnum可能副作用的影响。 The reality is that you're really only going to run into issues if you change DemoEnum to a class without changing the name. 现实情况是,如果将DemoEnum更改为类而不更改名称,则实际上只会遇到问题。

This is generally a larger issue when using classes (and ReSharper will give the same guidance in those situations). 使用类时,这通常是一个更大的问题(在这种情况下,ReSharper将提供相同的指导)。

The TryParse() method is a static method defined in the Enum class. TryParse()方法是Enum类中定义的静态方法。 Since your enum inherits everything from the Enum class, it also "inherits" static members. 由于您的枚举继承自Enum类的所有内容,因此它也“继承”了静态成员。 It's not true inheritance, the static members are just visible from the class. 这不是真正的继承,静态成员仅在类中可见。

The other answers are wrong in that some special translation is being done for you. 其他答案是错误的,因为某些特殊翻译正在为您完成。 All you are doing is accessing the static member from a derived class since all static members are accessible through your enum. 因为您可以通过枚举访问所有静态成员,所以您所做的就是从派生类访问静态成员。 The type of the identifier that you are accessing the static method from has no bearing at all on what the generic parameters are, only what the compiler is able to infer from them (or you explicitly provide). 您要从其访问静态方法的标识符的类型与通用参数的含义完全无关,而与编译器能够从中推断出(或您明确提供的)参数完全无关。

To illustrate my point, consider these two enums: 为了说明我的观点,请考虑以下两个枚举:

enum First { A, B }
enum Second { A, B }

First firstVar;
Second secondVar;
// note we're using the `First` name
First.TryParse("A", out firstVar);  // valid, firstVar <= First.A
First.TryParse("B", out secondVar); // still valid, secondVar <= Second.B

// is equivalent to
Enum.TryParse<First>("A", out firstVar); // generic type is inferred from type of firstVar
Enum.TryParse<Second>("B", out secondVar); // generic type is inferred from type of secondVar

What ReSharper is telling you is that you should be accessing the static member from the class that defined actually the member. ReSharper告诉您的是,您应该从实际定义了成员的类中访问静态成员。 Why should you do this? 为什么要这样做?

Consider what would happen if your derived type defined a static member with the same exact name. 考虑一下如果您的派生类型定义了一个具有相同确切名称的静态成员会发生什么。 (it's not possible in this case with enums but applicable to classes in general) What would happen to your code then? (在这种情况下,枚举是不可能的,但通常适用于类)那么代码会发生什么? Any code that accessed the static member through the derived class will take on the new value and you will probably get no new warning about it. 通过派生类访问静态成员的任何代码都将采用新值,并且您可能不会收到关于它的新警告。 This may or may not be desired behavior so ReSharper is preemptively warning you that you should use the actual class that defined it (the base). 这可能是预期的行为,也可能不是预期的行为,因此ReSharper会先行警告您应使用定义它的实际类(基类)。

Looks like Resharper is suggesting you use Enum.TryParse - http://msdn.microsoft.com/en-us/library/dd783499.aspx 看起来Resharper 建议您使用Enum.TryParse- http://msdn.microsoft.com/zh-CN/library/dd783499.aspx

I bet if you look at the IL... DemoEnum.TryParse is just doing a base.TryParse 我敢打赌,如果您看看IL ... DemoEnum.TryParse只是在做base.TryParse

When you write DemoEnum.TryParse , the compiler transforms it to Enum.TryParse<DemoEnum> . 当您编写DemoEnum.TryParse ,编译器DemoEnum.TryParse其转换为Enum.TryParse<DemoEnum> There is no functional difference, and you don't really need to be insulated from possible side effects because you can't define methods in an enum type, so TryParse can't be redefined. 没有功能上的区别,并且您真的不需要与可能的副作用隔离开来,因为您无法以enum类型定义方法,因此无法重新定义TryParse My best guess is that it's a style preference. 我最好的猜测是它是一种样式偏好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM