1

I have some dlls in my project that only contain many .resx files as embedded resources. In each project the .resx files are put into different folders. The property "Custom Tool Name Space" of all is set to the namespace of each project. When I try to use ResourceManager to get a string I get an error that for example "MyTemplate.resources" is not found but I only have "MyTemplate.resx" in the dll.

How can I access my resources?

new ResourceManager(typeof(MyTemplate.resx)).GetString("FirstNameTooltip");

As I said in comment below the Resources are dynamicly changed. and i have no direct access to its properties.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193

2 Answers2

1

Here's how I usually do:

string mystring = YourResourceNamespace.MyTemplate.FirstNameTooltip;

If you don't know the napespace, click on the .resx and double click on the Designer.cs, then check the namespace, all the keywords are defined as simple static variable with gettor, so just call those ones.

If ressource is in one other project, then simply incude it onto your project in order to access them from your project.

I hope this helps

clement
  • 4,204
  • 10
  • 65
  • 133
  • thanks. my problem is that my .resx file classes are dyamicly changed. and i dont know realy which class is now need. type of resource and key value of resource are changed in variable. i use it to Implementation of IDataErrorInfo DataAnnotation Validation. – Mohammad Haji Hosseini Jun 09 '15 at 06:56
  • if you don't know the namespace of class, I didn't have any solutions for you. Maybe store ressources in DB could helps? – clement Jun 09 '15 at 06:57
  • I can get the name space. i have the information of class, I khow the typeof(class) but its dynamic in variable. how can I get instance? – Mohammad Haji Hosseini Jun 09 '15 at 07:02
  • I have a ValidationAttribute Property. that contains ErrorMessageresourceName & ErrorMessageResourceType & ErrorMessage . I need The value of ErrorMessageresourceName in resource type of ErrorMessageResourceType. – Mohammad Haji Hosseini Jun 09 '15 at 07:23
  • You just want to have error messages in different languages for UI validation in fact? – clement Jun 09 '15 at 07:35
  • No, It is in one language. but i must use mutipleResources.resx to handle the validation. – Mohammad Haji Hosseini Jun 09 '15 at 07:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80133/discussion-between-mohammad-haji-hosseini-and-clement). – Mohammad Haji Hosseini Jun 10 '15 at 07:14
0

You need to load the assembly first:

Assembly ressourceAssembly = Assembly.Load("ResourceAssembly");

(There are a couple of issues you could have with this. Read about dynamic assembly loading specificly.)

Then create the resource manager:

ResourceManager myManager = new 
   ResourceManager("<default namespace>.<Resx-Folder>.<Resx-File>", ressourceAssembly);

Then load the string:

myManager.GetString("FirstNameTooltip");
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • thanks for your time. But as i said ValidationAttribute class has the assembly. the problem is ResourceManager try to locate resource file whit .resources extention and i need to use .resx extention. another point is the resource file are not in the root of dll project,they are put in folders by subject. – Mohammad Haji Hosseini Jun 09 '15 at 09:28