Localizing attributes in properties

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have placed text within a resouce file.

The class has a reference to a Resource Manager LocRM created in the
constructor. I want to reference the text within the resource file when
declaring a property. See below.

[Description(this.LocRM.GetText("strTestCategoryID")),
Category(TestCaseStrings.strLabelDescription),
DefaultValueAttribute(TestingCategories.TestingCategory.Miscellaneous)]
public virtual TestingCategories.TestingCategory TestCategory
{
get { return category; }
set { category = value; }
}

Obviously the compiler generates an error because it is expecting a constant
string where I'd like the this.LocRM("....").

How do I reference text within a resource file and place it in an attribute
above?
 
Steve,
I don't have a link to an example handy of overriding it, my understanding
is you need to derive a new class from DescriptionAttribute & override the
Description Property, saving its value in the DescriptionValue property...

http://msdn.microsoft.com/library/d...iptionattributeclassdescriptionvaluetopic.asp

Hope this helps
Jay

|I have placed text within a resouce file.
|
| The class has a reference to a Resource Manager LocRM created in the
| constructor. I want to reference the text within the resource file when
| declaring a property. See below.
|
| [Description(this.LocRM.GetText("strTestCategoryID")),
| Category(TestCaseStrings.strLabelDescription),
| DefaultValueAttribute(TestingCategories.TestingCategory.Miscellaneous)]
| public virtual TestingCategories.TestingCategory TestCategory
| {
| get { return category; }
| set { category = value; }
| }
|
| Obviously the compiler generates an error because it is expecting a
constant
| string where I'd like the this.LocRM("....").
|
| How do I reference text within a resource file and place it in an
attribute
| above?
|
| --
| Steve
 
Jay,

I derived the class. See below. By calling MyDesciption in the property
it now correctly gets the string. Thanks so much.

/// <summary>
/// Overrides the DescriptionAttribute class in order to allow for
localization.
/// </summary>
public class MyDescriptionAttribute :
System.ComponentModel.DescriptionAttribute
{
public MyDescriptionAttribute(string attribute)
{
this.attribute = attribute;
}
#region Fields
protected CheckItResources LocRS = new CheckItResources();
protected string attribute;
#endregion // Fields

/// <summary>
/// Returns the localized string for the attribute.
/// </summary>
public override string Description
{
get
{
return this.DescriptionValue =
LocRS.GetText(this.attribute);
}
}
}
 
Back
Top