473,803 Members | 4,139 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to access a resource string within a DescriptionAttr ibute

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(MyA pp.Properties.R esources.strADe sciption)].

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

--
-----------
Thanks,
Steve
May 30 '07 #1
5 6570
On May 30, 4:26 pm, SteveT <Ste...@newsgro ups.nospamwrote :
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(MyA pp.Properties.R esources.strADe sciption)].

The issue is that Description requires a constant string. Is there anyway
to reference my resource file to get the strings desired for "Descriptio n"?
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

May 30 '07 #2
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 DescriptionValu e property in the
constructor (it is protected) to the value from the resource.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"SteveT" <St****@newsgro ups.nospamwrote in message
news:BF******** *************** ***********@mic rosoft.com...
>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(MyA pp.Properties.R esources.strADe sciption)].

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

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

May 30 '07 #3
I just realized that I interpreted the question incorrectly, ignore the
previous post.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:eU******** ******@TK2MSFTN GP06.phx.gbl...
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 DescriptionValu e property in the
constructor (it is protected) to the value from the resource.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"SteveT" <St****@newsgro ups.nospamwrote in message
news:BF******** *************** ***********@mic rosoft.com...
>>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(MyA pp.Properties.R esources.strADe sciption)].

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


May 30 '07 #4
Can I refer you to how MS do it (via Reflector)? They have a subclass
of DescriptionAttr ibute, which updates itself at runtime; they then
use [SRDescription(" res name")]; worth a shot...

[AttributeUsage( AttributeTarget s.All)]
internal sealed class SRDescriptionAt tribute : DescriptionAttr ibute
{
// Fields
private bool replaced;

// Methods
public SRDescriptionAt tribute(string description) :
base(descriptio n)
{
}

// Properties
public override string Description
{
get
{
if (!this.replaced )
{
this.replaced = true;
base.Descriptio nValue =
SR.GetString(ba se.Description) ;
}
return base.Descriptio n;
}
}
}

May 30 '07 #5
>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(MyA pp.Properties.R esources.strADe sciption)].

The issue is that Description requires a constant string. Is there anyway
to reference my resource file to get the strings desired for "Descriptio n"?
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., "strADescriptio n"). 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.Componen tModel;
using System.Reflecti on
using MyApp.Propertie s;

class DescriptionReso urceAttribute : DescriptionAttr ibute
{
public DescriptionReso urceAttribute(s tring resourceName)
: base((string)ty peof(Resources) .InvokeMember(r esourceName,
BindingFlags.Ge tProperty | BindingFlags.No nPublic | BindingFlags.St atic,
null, null, null))
{
}
}
May 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3281
by: Jay Moore | last post by:
Greetings, all! I have a project for work, and I'm not sure how to efficiently do what I need to do. I'm hoping someone out there can help. Project is this: I'm creating a web-based interface where people at my company (operators) can enter data for service calls. All data entered is run thru one or more PHP scripts for error checking and then stored in a MySQL database on a server here in the office.
3
24039
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked tables in the database where the code resides. If we move the database with the data tables to a new directory, the links are no longer valid. I tried to update the links by changing the Connect property and refreshing: Set td = db.TableDefs(0)...
3
13906
by: Sanjay Pais | last post by:
I know that string/char enum is not possible in c# (.NET2.0) I need to create the equivalent of this: public enum HOW_GOOD { AWESOME = "A", GREAT= "G", NOT_TOO_BAD = "N", TERRIBLE="T" }
4
2917
by: Bruce | last post by:
I am developing an ASP.NET web service application. It works fine on my WinXP Prof development machine. But when I send it to a Windows Server 2003 system I get the following error (attached below). I suppose I have some kind of permissions issue on the server? What recommendations can anyone propose? Thanks, -- Bruce
2
1778
by: Cc | last post by:
is there a way to create enum that contain string?
1
3627
by: schneider | last post by:
Is it possible to get access to the local resource (App_LocalResources) of an aspx page from within a user control located somewhere else? I have created a user control which I want to add to a page and read values from the page's local resource. But within the code of the user control, I can only access the local resource of the user control itself in its own App_LocalResources folder. Can someone help me please? Cheers, Hannes
3
1615
by: Chevron Boyde | last post by:
Hi There I have some codes that represent Sale Types i.e. A = On Account, C = Cash, D = Debtor, V = Voucher I want to create an enum or struct to work with the logical names like "Cash" as opposed to "C" I see the enums cannot work with string values like public enum SaleType
5
1490
by: chrisbarber1 | last post by:
Can anyone help?? How can i get the string representation of a property? For example if I have a Client object that has a FirstName property, how in code can I get "FirstName" from Client.FirstName ? Chris
4
1613
by: Christopher | last post by:
I am surprised this hasn't come up for me more in the past, but the situation is: I need to have an interface that is usable for all I need to have an interface that is only usable for some I do not really know of a good way to achieve this. If I use friend functions, I can no longer make methods virtual, right? Example:
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10542
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10309
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.