Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有多个DLL的c#单例模式_C#_.net_Dll_Singleton - Fatal编程技术网

具有多个DLL的c#单例模式

具有多个DLL的c#单例模式,c#,.net,dll,singleton,C#,.net,Dll,Singleton,我的问题是关于以下程序集和库组合中使用的单例模式 想象一下下面的程序集 CommonLibrary.dll MainProcess.exe Plugin1.dll Plugin2.dll MainProcess.exe、Plugin1.dll和Plugin2.dll使用CommonLibrary.dll中的类Manager。CommonLibrary.dll在编译时链接到其他程序集。Manager类使用单例模式: public class Manager { private stat

我的问题是关于以下程序集和库组合中使用的单例模式

想象一下下面的程序集

  • CommonLibrary.dll
  • MainProcess.exe
  • Plugin1.dll
  • Plugin2.dll
MainProcess.exe、Plugin1.dll和Plugin2.dll使用CommonLibrary.dll中的类
Manager
。CommonLibrary.dll在编译时链接到其他程序集。
Manager
类使用单例模式:

public class Manager
{
    private static Manager instance;

    public static Manager Instance
    {
        [MethodImpl(MethodImplOptions.Synchronized)]
        get { 
            if (instance == null)
            {
                instance = new Manager();
            }
            return instance;
        }
    }

    private Manager()
    {

    }

    // methods ...
}
singleton模式仅用于创建
管理器
类的一个实例(显然)。这在
Manager
的方法中很重要,因为对资源的访问需要使用不同的锁进行协调。例如,通过使用锁:

// Method inside Manager
public void DoSomething()
{
    lock(someLockObject)
    {
         // do something
    }
}
MainProcess.exe、Plugin1.dll和Plugin2.dll都通过调用

Manager manager = Manager.Instance;
// do something with manager
现在想象以下场景: MainProcess.exe在运行时使用Assembly.LoadFrom()使用反射加载Plugin1.dll和Plugin2.dll

问题:MainProcess.exe、Plugin1.dll和Plugin2.dll是否共享相同的Manager实例?这很重要,因为如果它们不共享同一个Manager实例,我需要对资源执行一些进程间锁定

谢谢

MainProcess.exe、Plugin1.dll和Plugin2.dll是否共享同一个Manager实例


是的,有。在应用程序域中(以及在整个过程中,除非您明确创建另一个
AppDomain
),将只有一个
Manager

感谢您确认我所希望的。我不确定,因为DLL的运行时/反射加载。+1我正在使用一个共享项目的类似场景,并且担心我的静态对象只对主程序可用。