IResourceProvider.GetObject Method (String, CultureInfo)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Returns a resource object for the key and culture.

Namespace:   System.Web.Compilation
Assembly:  System.Web (in System.Web.dll)

object GetObject(
	string resourceKey,
	CultureInfo culture
)

Parameters

resourceKey
Type: System.String

The key identifying a particular resource.

culture
Type: System.Globalization.CultureInfo

The culture identifying a localized value for the resource.

Return Value

Type: System.Object

An Object that contains the resource value for the resourceKey and culture.

When implemented, the GetObject method returns the localized value for a resource key based on the current culture. The implementing class could retrieve the CurrentUICulture property if a value for the culture parameter is not passed in.

The following code example shows a customized resource provider factory that retrieves cached values. The resource provider factory creates an instance of a customized resource provider that implements IResourceProvider. The resource provider includes a customized GetObject method.

[
    DesignTimeResourceProviderFactoryAttribute(
        typeof(CustomDesignTimeResourceProviderFactory))
]
public class CustomResourceProviderFactory : ResourceProviderFactory
{
    public override IResourceProvider
      CreateGlobalResourceProvider(string classname)
    {
        return new CustomResourceProvider(null, classname);
    }
    public override IResourceProvider
      CreateLocalResourceProvider(string virtualPath)
    {
        return new CustomResourceProvider(virtualPath, null);
    }
}

// Define the resource provider for global and local resources.
internal class CustomResourceProvider : IResourceProvider
{
    string _virtualPath;
    string _className;

    public CustomResourceProvider(string virtualPath, string classname)
    {
        _virtualPath = virtualPath;
        _className = classname;
    }

    private IDictionary GetResourceCache(string culturename)
    {
        return (IDictionary)
            System.Web.HttpContext.Current.Cache[culturename];
    }

    object IResourceProvider.GetObject
        (string resourceKey, CultureInfo culture)
    {
        object value;

        string cultureName = null;
        if (culture != null)
        {
            cultureName = culture.Name;
        }
        else
        {
            cultureName = CultureInfo.CurrentUICulture.Name;
        }

        value = GetResourceCache(cultureName)[resourceKey];
        if (value == null)
        {
            value = GetResourceCache(null)[resourceKey];
        }
        return value;
    }


    IResourceReader IResourceProvider.ResourceReader
    {
        get
        {
            string cultureName = null;
            CultureInfo currentUICulture = CultureInfo.CurrentUICulture;
            if (!String.Equals(currentUICulture.Name,
                CultureInfo.InstalledUICulture.Name))
            {
                cultureName = currentUICulture.Name;
            }

            return new CustomResourceReader
                (GetResourceCache(cultureName));
        }
    }
}


internal sealed class CustomResourceReader : IResourceReader
{
    private IDictionary _resources;

    public CustomResourceReader(IDictionary resources)
    {
        _resources = resources;
    }

    IDictionaryEnumerator IResourceReader.GetEnumerator()
    {
        return _resources.GetEnumerator();
    }

    void IResourceReader.Close() { }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return _resources.GetEnumerator();
    }

    void IDisposable.Dispose() { return; }
}

.NET Framework
Available since 2.0
Return to top
Show: