using System;
using System.ComponentModel;
using System.Resources;
namespace DevPrime.Design
{
/// <summary>
/// Specifies a localized description
/// for a property or event.
/// </summary>
/// <remarks>
/// Unlike the DescriptionAttribute class
/// , the Description property of
/// ResourceDescriptionAttribute returns
/// a description that is set in a
/// resource, which can be localized.
/// </remarks>
[AttributeUsage(AttributeTargets.All,
Inherited = true, AllowMultiple = false)]
public class ResourceDescriptionAttribute
: DescriptionAttribute
{
private Type resourceSourceValue;
private string descriptionResourceNameValue;
/// <summary>
/// Initializes an instance of the
/// ResourceDescriptionAttribute class.
/// </summary>
/// <param name="descriptionResourceName">
/// The name of the resource string
/// to use as the description.
/// </param>
public ResourceDescriptionAttribute(
string descriptionResourceName)
{
descriptionResourceNameValue
= descriptionResourceName;
}
/// <summary>
/// Initializes an instance of the
/// ResourceDescriptionAttribute class.
/// </summary>
/// <param name="descriptionResourceName">
/// The name of the resource string to
/// use as the description.
/// </param>
/// <param name="resourceSource">
/// The Type used to initialize the
/// ResourceManager for this instance.
/// </param>
public ResourceDescriptionAttribute(
string descriptionResourceName,
Type resourceSource)
{
descriptionResourceNameValue
= descriptionResourceName;
resourceSourceValue = resourceSource;
}
/// <summary>
/// Returns the name of the resource
/// string to use as the description.
/// </summary>
public string DescriptionResourceName
{
get { return descriptionResourceNameValue; }
}
/// <summary>
/// Specifies the Type used to initialize
/// the ResourceManager for this instance.
/// </summary>
/// <remarks>
/// The ResourceManager initialized here
/// will read the resource inferred by
/// the provided type. For example, if
/// you provide the type MyNamespace.MyType,
/// the ResourceManager will infer the
/// assembly of that type and read the
/// resource called
/// MyNamespace.MyType.[culture-name.]resources.
/// </remarks>
public Type ResourceSource
{
get { return resourceSourceValue; }
set { resourceSourceValue = value; }
}
/// <summary>
/// Returns a localized description
/// based on the DescriptionResourceName
/// provided in the constructor.
/// </summary>
public override string Description
{
get
{
if (string.IsNullOrEmpty(DescriptionValue))
{
ResourceManager resMgr =
(resourceSourceValue == null ?
Properties.Resources.ResourceManager :
new ResourceManager(resourceSourceValue));
base.DescriptionValue
= resMgr.GetString(
descriptionResourceNameValue);
}
return base.DescriptionValue;
}
}
}
}