C# 如何使一个类对整个应用程序全局化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/185282/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:03:25  来源:igfitidea点击:

How can I make a class global to the entire application?

c#.net

提问by Pokus

I would like to access a class everywhere in my application, how can I do this?

我想在我的应用程序中的任何地方访问一个类,我该怎么做?

To make it more clear, I have a class somewhere that use some code. I have an other class that use the same code. I do not want to duplicate so I would like to call the same code in both place by using something. In php I would just include("abc.php") in both... I do not want to create the object everytime I want to use the code.

为了更清楚,我在某处有一个使用一些代码的类。我还有一个使用相同代码的类。我不想重复,所以我想通过使用某些东西在两个地方调用相同的代码。在 php 中,我只想在两者中都包含(“abc.php”)......我不想每次都想使用代码时创建对象。

采纳答案by Patrick Desjardins

The concept of global classes in C# is really just a simple matter of referencing the appropriate assembly containing the class. Once you have reference the needed assembly, you can refer to the class of choice either by it's fully qualified Type name, or by importing the namespace that contains the class. (Concrete instance or Static accessto that class)

C# 中全局类的概念实际上只是引用包含类的适当程序集的简单问题。一旦引用了所需的程序集,就可以通过它的完全限定类型名称或通过导入包含该类的命名空间来引用选择的类。(对该类的具体实例或静态访问

Or

或者

You can have a Singleton classto use it everywhere but some people won't recommend you this way to proceed.

您可以使用Singleton 类在任何地方使用它,但有些人不会推荐您以这种方式进行。

回答by Max Schmeling

Do you want to access the class or access an instance of the class from everywhere?

您想从任何地方访问该类还是访问该类的实例?

You can either make it a static class - public static class MyClass { }- or you can use the Singleton Pattern.

您可以将其设为静态类public static class MyClass { }- 或者您可以使用单例模式

For the singleton pattern in its simplest form you can simply add a static property to the class (or some other class) that returns the same instance of the class like this:

对于最简单形式的单例模式,您可以简单地向类(或某个其他类)添加一个静态属性,该类返回该类的相同实例,如下所示:

public class MyClass
{
     private static MyClass myClass;

     public static MyClass MyClass
     {
          get { return myClass ?? (myClass = new MyClass()); }
     }

     private MyClass()
     {
          //private constructor makes it where this class can only be created by itself
     }
}

回答by Andrew

The other answers that you've been given about using a static class or a singleton pattern are correct.

您获得的有关使用静态类或单例模式的其他答案是正确的。

Please consider, however, the fact that doing so does compromise your ability to test. In general, if you can, prefer dependency injection over globally accessed classes. I know this isn't always possible (or practical).

但是,请考虑这样一个事实,即这样做会损害您的测试能力。一般来说,如果可以的话,更喜欢依赖注入而不是全局访问的类。我知道这并不总是可能的(或实用的)。

Just on that, you should also look up the abstract factory pattern. It allows you to have a well known factory class that produces the actual instance of a class that you're using. To have a globally accessed logging class, for example, don't directly create a logging class. Instead, use a logging factory to create it for you and return an interface to a logging class. That way it's easier to swap in and out different logging classes.

就此而言,您还应该查找抽象工厂模式。它允许您拥有一个众所周知的工厂类,该类生成您正在使用的类的实际实例。例如,要有一个全局访问的日志类,不要直接创建一个日志类。相反,使用日志工厂为您创建它并返回一个日志类的接口。这样可以更轻松地换入和换出不同的日志记录类。

回答by Declan Shanaghy

Since you do not want to create the object every time and it sounds like you are talking about some sort of utility methods...

由于您不想每次都创建对象,而且听起来您在谈论某种实用方法......

I suggest you use static methodsin an assembly which you can reference where needed

我建议您在程序集中使用静态方法,您可以在需要的地方引用