System.ArgumentNullException in ResourceManager.GetString internals

拟墨画扇 提交于 2019-12-06 15:03:21

I also think that you should declare and use your own dictionary for storage since ResourceSets is marked as Obsolete under .NET 4.0

I found answer myself. Here are details: We have custom implementation of ResourceManager like this:

public class DatabaseResourceManager : System.Resources.ResourceManager 
{
  public DatabaseResourceManager(int applicationID, string bundle) 
  {
    foreach (int languageID in ResourceProvider.Provider.GetLanguages(applicationID))
    {
      DatabaseResourceReader r = new DatabaseResourceReader(applicationID, bundle, languageID);
      ResourceSets.Add(new CultureInfo(languageID), new ResourceSet(r));
    }
}

In .NET 2.0 it works well, but in .NET 4.0 something has changed in implementation of ResourceManager.I think that problem is in parameterless constructor which in .NET 2.0 instantiate private field this._resourceSets (which is later used in InternalGetResourceSet for Monitor.Enter). But in .NET 4.0 parameterless constructor does not instantiate private field this._resourceSets and thus it fails later (as described abobe).

I must rewrite my custom resource manager as this to work out:

public class DatabaseResourceManager : System.Resources.ResourceManager 
{
  public DatabaseResourceManager(int applicationID, string bundle) 
  {
    ResourceSets = new Hashtable();
    this.applicationID = applicationID;
    this.bundle = bundle;
  }

  protected override ResourceSet InternalGetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents)
  {
    if (this.ResourceSets.Contains(culture.Name))
        return this.ResourceSets[culture.Name] as ResourceSet;

    lock (syncLock)
    {
        if (this.ResourceSets.Contains(culture.Name))
            return this.ResourceSets[culture.Name] as ResourceSet;

        DatabaseResourceReader r = new DatabaseResourceReader(applicationID, bundle, culture.LCID);
        ResourceSet rs = new ResourceSet(r);

        this.ResourceSets.Add(culture.Name, rs);

        return rs;
    }
  }
}

"Magic" here is that i must overwrite method InternalGetResourceSet to load my resources from custom storage (db) and return back "ResourceSet" for specified culture. Now it works like a charm.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!