开发者

Is ResourceManager in .NET a singleton?

开发者 https://www.devze.com 2022-12-10 01:17 出处:网络
Is R开发者_如何学GoesourceManager in .NET a singleton?If you look into the designer generated code when dealing with a resource file you can see each of the generated resource properties accesses an i

Is R开发者_如何学GoesourceManager in .NET a singleton?


If you look into the designer generated code when dealing with a resource file you can see each of the generated resource properties accesses an internal static property that will create a ResourceManager instance if it is not already created, and then reuse this instance. From that point of view it looks like Singleton to me (even if the concrete Singleton implementation may not be the best one, perhaps).

Example (using a resource file called MyResourceFile.resx, with a string resource called SomeStringResource):

// You access the resource like so
MyResourceFile.SomeStringResource;

// The generated SomeStringResource property
internal static string SomeStringResource {
    get {
        return ResourceManager.GetString("SomeStringResource", resourceCulture);
    }
}

// The generated ResourceManager property
private static global::System.Resources.ResourceManager resourceMan;
internal static global::System.Resources.ResourceManager ResourceManager {
    get {
        if (object.ReferenceEquals(resourceMan, null)) {
            global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SampleApplication.MyResourceFile", typeof(MyResourceFile).Assembly);
            resourceMan = temp;
        }
        return resourceMan;
    }
}

As you can see in the ResourceManager property, it checks if resourceMan is null, creates a new ResourceManager only if it is. Subsequent calls will use the then already created ResourceManager instance. This means that exactly one instance will be created and then reused, which is the point of the Singleton pattern.


System.Resources.ResourceManager has public constructors, so is not a singleton.


Judging from it's usage I would say no. I think it's more likely a factory class or something akin to that.

Being that the usage is something like this:

Resources.SomeResourceYouMade;

If it was a singleton you would have to access the instance first, and then the resource, so it would look more like this:

Resources.Instance.SomeResourceYouMade;

Also, here's some information on instantiating resource managers, even from different assemblies (link).

0

精彩评论

暂无评论...
验证码 换一张
取 消