C# 在派生类型上使用类的静态成员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/660132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a class's static member on a derived type?
提问by Swim
Using Resharper 4.1, I have come across this interesting warning: "Access to a static member of a type via a derived type". Here is a code sample of where this occurs:
使用 Resharper 4.1,我遇到了这个有趣的警告:“通过派生类型访问类型的静态成员”。这是发生这种情况的代码示例:
class A {
public static void SomethingStatic() {
//[do that thing you do...]
}
}
class B : A {
}
class SampleUsage {
public static void Usage() {
B.SomethingStatic(); // <-- Resharper warning occurs here
}
}
Does anybody know what issues there are (if any) when making use of A's static members via B?
有人知道通过 B 使用 A 的静态成员时存在哪些问题(如果有)?
采纳答案by Greg Beech
One place where it might be misleading is when the static is a factory method, e.g. the WebRequest
class has a factory method Create
which would allow this type of code to be written if accessed via a derived class.
一个可能会引起误解的地方是当静态是工厂方法时,例如,WebRequest
类具有工厂方法Create
,如果通过派生类访问,则允许编写此类代码。
var request = (FtpWebRequest)HttpWebRequest.Create("ftp://ftp.example.com");
Here request
is of type FtpWebRequest
but it's confusing because it looks like it was created from an HttpWebRequest
(a sibling class) even though the Create
method is actually defined on WebRequest
(the base class). The following code is identical in meaning, but is clearer:
这request
是类型,FtpWebRequest
但它令人困惑,因为它看起来像是从HttpWebRequest
(兄弟类)创建的,即使该Create
方法实际上是在WebRequest
(基类)上定义的。以下代码含义相同,但更清晰:
var request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com");
Ultimately there's no major problem accessing a static via a derived type, but code is often clearer by not doing so.
最终通过派生类型访问静态没有大问题,但如果不这样做,代码通常会更清晰。
回答by Daniel Earwicker
It's not a warning, usually, just a suggestion. You're creating a dependency on something unnecessarily.
这通常不是警告,只是建议。你正在不必要地建立对某些东西的依赖。
Suppose you later decide that B doesn't need to inherit A. If you follow Resharper's advice, you won't need to modify that line of code.
假设您后来决定 B 不需要继承 A。如果您遵循 Resharper 的建议,您将不需要修改该行代码。
回答by Ray
Yeah I've seen this too, I've always assumed it was just warning me because it was unnecessary. A.SomethingStatic();
would do the same thing.
是的,我也见过这个,我一直认为它只是警告我,因为它没有必要。A.SomethingStatic();
会做同样的事情。
回答by P Daddy
B.SomethingStatic()
makes the statement that SomethingStatic
is a member of B
. This is not true. SomethingStatic
is unequivocally a member of A
. The fact that it's accessible unqualified to members of B
(as if it were a member of B
) is a matter of convenience. The fact that it's accessible when qualified with a B
is, IMO, a mistake.
B.SomethingStatic()
声明SomethingStatic
是 的成员B
。这不是真的。 SomethingStatic
毫无疑问是 的成员A
。事实上,它的成员B
(就好像它是 的成员一样B
)可以无条件访问是一个方便的问题。用 a 限定它可以访问的事实B
是,IMO,一个错误。