避免"通过派生类型&QUOT访问类的静态成员; [英] Avoiding "Access to a static member of a type via a derived type"

查看:463
本文介绍了避免"通过派生类型&QUOT访问类的静态成员;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这是一个纯粹的ReSharper的警告,但它背后的推理(解释的这里)是有道理的。什么格雷格山毛榉的意思是,你还可以从同级类的基类的静态方法中...他的榜样,他用:

I believe this is purely a Resharper warning, but the reasoning behind it (explained here) makes good sense. What Greg Beech is saying is that you can call a base-class static method from a sibling class... in his example he uses:

var request = (FtpWebRequest)HttpWebRequest.Create(...)

... ...这是一种误导。

... which is misleading.

那么,有一个设计,可以让我避免在下面的类此警告?

So is there a design that would allow me to avoid this warning in the following classes?

public abstract class BaseLog {

    //  I omitted several other properties for clarity
    protected static string category;
    protected static TraceEventType severity;

    static BaseLog() {
        category = "General";
        severity = TraceEventType.Information;
    }

    public static void Write(string message) {
        Write(message, category, severity);
    }

    //  Writes to a log file... it's the same code for 
    //  every derived class.  Only the category and severity will change
    protected static void Write(string message, string messageCategory, TraceEventType messageSeverity) {

        LogEntry logEntry = new LogEntry(message, messageCategory, messageSeverity);

        //  This is Microsoft's static class for logging... I'm wrapping it to 
        //  simplify how it's called, but the basic principle is the same:
        //  A static class to log messages
        Logger.Write(logEntry);

    }

}


public class ErrorLog : BaseLog {

    static ErrorLog() {
        category = "Errors";
        severity = TraceEventType.Error;
    }

    //  I can add more functionality to the derived classes, but
    //  the basic logging functionality in the base doesn't change
    public static void Write(Exception exc) {
        Write(exc.Message);
    }

}


//  Code that could call this...
catch (Exception exc) {
    //  This line gives me the warning
    ErrorLog.Write("You did something bad");
    ErrorLog.Write(exc);
}

一个错误日志服务的应用程序,它的设置不会改变(也有一个追踪日志和ThreadLog)。我不想重复日志代码,因为它的每一个派生类一模一样......保持它在BaseLog完美。那么,如何设计这个,我不是违反本设计原理?

One ErrorLog serves the application, and its settings never change (there's also a TraceLog and a ThreadLog). I don't want to duplicate the logging code, because it's exactly the same for every derived class... keeping it in BaseLog works perfectly. So how do I design this that I'm not violating this design principle?

的类是静态的,因为我不希望实例化一个新的错误日志对象我想记录的东西,我不想让他们在50左右浮动在每节课我写一个成员级变量的形式每次。该日志记录使用微软的企业库,如果有差别。

The classes are static because I don't want to instantiate a new ErrorLog object every time I want to log something, and I don't want 50 of them floating around in the form of a member-level variable in every class I write. The logging is using Microsoft's Enterprise Library, if that makes a difference.

TIA!

詹姆斯

TIA!
James

推荐答案

好像你要保持车门打开扩展而不是修改又名开闭原则。而它的一个有价值的目标。

It seems like you want to keep the door open for extension but not for modification aka the Open Closed principle. And its a worthy goal.

我的建议是为失去了静电吸附 - 关闭该功能,holder类为对象。这使您可以覆盖(而不是混淆其他读者)的要求 - 多态性只适用于实例

My advice would be to lose the static cling - turn the function-holder classes into objects. This allows you to override (and not confuse other readers) as required - polymorphism only works with instances.

接下来关心的是需要有一个全局对象VS传球记录器实例各地。 建立另一种类型,提供访问日志对象的单个实例。 (老单)

The next concern would be the need to have a global object vs passing a logger instance around. Create another type that provides access to a single instance of a logger object. (the old singleton)

e.g. ErrorLogProvider.Instance.Write(something)



PS:免费的东西 - 更容易得测试这些对象

PS: Freebie - easier to test these objects too.

这篇关于避免"通过派生类型&QUOT访问类的静态成员;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆