How to access a resource string within a DescriptionAttribute

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

Guest

I am using [Description("A description")] in my code.

I want to be able to reference a text string within my "Resource file" like
this:

[Description(MyApp.Properties.Resources.strADesciption)].

The issue is that Description requires a constant string. Is there anyway
to reference my resource file to get the strings desired for "Description"?
 
I am using [Description("A description")] in my code.

I want to be able to reference a text string within my "Resource file" like
this:

[Description(MyApp.Properties.Resources.strADesciption)].

The issue is that Description requires a constant string. Is there anyway
to reference my resource file to get the strings desired for "Description"?

No - one of the things about attributes is that their values are
absolutely constant, baked into the assembly. You can't use properties
or anything like that.

You could autogenerate your file, perhaps, to get the descriptions,
but that would be a fairly extreme measure.

Jon
 
Steve,

In this case, I would derive a class from the Description attribute and
then have the constructor take a key that you can use to get the appropriate
resource.

Once you have it, you can set the DescriptionValue property in the
constructor (it is protected) to the value from the resource.
 
I just realized that I interpreted the question incorrectly, ignore the
previous post.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nicholas Paldino said:
Steve,

In this case, I would derive a class from the Description attribute and
then have the constructor take a key that you can use to get the
appropriate resource.

Once you have it, you can set the DescriptionValue property in the
constructor (it is protected) to the value from the resource.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SteveT said:
I am using [Description("A description")] in my code.

I want to be able to reference a text string within my "Resource file"
like
this:

[Description(MyApp.Properties.Resources.strADesciption)].

The issue is that Description requires a constant string. Is there
anyway
to reference my resource file to get the strings desired for
"Description"?
 
Can I refer you to how MS do it (via Reflector)? They have a subclass
of DescriptionAttribute, which updates itself at runtime; they then
use [SRDescription("res name")]; worth a shot...

[AttributeUsage(AttributeTargets.All)]
internal sealed class SRDescriptionAttribute : DescriptionAttribute
{
// Fields
private bool replaced;

// Methods
public SRDescriptionAttribute(string description) :
base(description)
{
}

// Properties
public override string Description
{
get
{
if (!this.replaced)
{
this.replaced = true;
base.DescriptionValue =
SR.GetString(base.Description);
}
return base.Description;
}
}
}
 
I am using [Description("A description")] in my code.
I want to be able to reference a text string within my "Resource file" like
this:

[Description(MyApp.Properties.Resources.strADesciption)].

The issue is that Description requires a constant string. Is there anyway
to reference my resource file to get the strings desired for "Description"?

There are various ways of doing it. Here's one simple way I came up with for my own code (adapted for your example). Just pass the name of the resource as a literal (e.g., "strADescription"). It could stand a little work if you really want to make it completely portable/generic (and to safeguard against changes to resource names) but it does the job.

using System.ComponentModel;
using System.Reflection
using MyApp.Properties;

class DescriptionResourceAttribute : DescriptionAttribute
{
public DescriptionResourceAttribute(string resourceName)
: base((string)typeof(Resources).InvokeMember(resourceName,
BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Static,
null, null, null))
{
}
}
 
Back
Top