Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

How to access a resource string within a DescriptionAttribute


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"?

--
-----------
Thanks,
Steve

On May 30, 4:26 pm, SteveT <Ste@newsgroups.nospam> wrote:

> 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

-----------------------------------------------Reply-----------------------------------------------

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]
          - m@spam.guard.caspershouse.com

"SteveT" <Ste@newsgroups.nospam> wrote in message

news:BF4B959C-BD16-4843-8049-6A95BD787A79@microsoft.com...

    I just realized that I interpreted the question incorrectly, ignore the
previous post.

--
          - Nicholas Paldino [.NET/C# MVP]
          - m@spam.guard.caspershouse.com

"Nicholas Paldino [.NET/C# MVP]" <m@spam.guard.caspershouse.com> wrote in
message news:eUfvWCtoHHA.4196@TK2MSFTNGP06.phx.gbl...

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))
    {
    }

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc