locked
How to access string resources in the assembly being tested? RRS feed

  • Question

  • I am attempting to write a unit test that, given a string resource key and a culture, will retrieve the associated language specific string resource from assembly being tested. How can this be accomplished?

    Richard Lewis Haggard

    Wednesday, June 8, 2016 7:18 PM

Answers

  • OK, finally got everything working. Here are the details. Here's the first part of the problem, gaining access to the resources.

    Assembly asm = Assembly.Load( AssemblyName.GetAssemblyName( [Path and filename of the assembly to be loaded] ) );
    ResourceManager rm = new ResourceManager( [namespace containing the strings], asm);
    

    I misunderstood the documentation on the Assembly.Load() method and was trying to directly supply the assembly pathname instead of utilizing the AssemblyName.GetAssemblyName() method. The second part of my difficulty was getting the namespace in ResourceManager right. I finally got that right by opening opening Resource.Designer.cs and combined the Namspace plus the class name to get the correct namespace string.

    Once the ResourceManager creation was correct the final piece was a simple one, rm.GetString( key, culture ); where key is the obvious string resource key and culture is an instantiation of a CultureInfo. If you are only going at the default local you will not need a CultureInfo but in my case the unit test cycles through all supported languages and thus must include the locale.


    Richard Lewis Haggard

    Tuesday, July 5, 2016 1:57 PM

All replies

  • Hi Richard.Haggard,

    According to your description, you want to get the tested assembly's string resources and culture information in Unit Test, is it right?

    It should be using the common C# code which reflect the assembly through the System.Reflection.Assembly. The sample code shown as below. Before using this method, please reference your assembly to the Unit Test project first.

    System.Reflection.Assembly myOtherAssembly;
                myOtherAssembly = System.Reflection.Assembly.Load("Your Assembly Name");

    Then please use the ResourceManager class to retrieve the Resource in the reflected assembly. The sample code please refer to following MSDN documents. Hope that can help you.

    https://msdn.microsoft.com/en-us/library/system.resources.resourcemanager(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/aa984408(VS.71).aspx

    Best Regards,
    Weiwei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Weiwei Cai Tuesday, June 14, 2016 8:25 AM
    • Marked as answer by Weiwei Cai Monday, June 20, 2016 8:39 AM
    • Unmarked as answer by Richard.Haggard Thursday, June 30, 2016 3:18 PM
    • Unproposed as answer by Weiwei Cai Friday, July 1, 2016 1:03 AM
    Friday, June 10, 2016 6:15 AM
  • In the future, it would be more helpful if you be a little more specific in your answers. That response was an answer in much the same vein as this: "How do you cure all of the diseases in the world? Go to school and learn everything needed to invent a serum that cures all ills."

    Richard Lewis Haggard

    Thursday, June 30, 2016 3:51 PM
  • Hi Richard.Haggard,

    In Unit Test, if we want to test the methods in a assembly, we need to add reference this assembly to the Unit Test project, is it right? So we can use System.Reflection.Assembly.Load() to reflect that assembly to get the content in this assembly in Unit Test code.

    If possible, please share a sample assembly which contains the associated language specific string resource and tell me which specific string resource do you want to get. I will create a sample Unit Test about how to get the string resource. Thanks.

    Best Regards,
    Weiwei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, July 1, 2016 9:05 AM
  • OK, finally got everything working. Here are the details. Here's the first part of the problem, gaining access to the resources.

    Assembly asm = Assembly.Load( AssemblyName.GetAssemblyName( [Path and filename of the assembly to be loaded] ) );
    ResourceManager rm = new ResourceManager( [namespace containing the strings], asm);
    

    I misunderstood the documentation on the Assembly.Load() method and was trying to directly supply the assembly pathname instead of utilizing the AssemblyName.GetAssemblyName() method. The second part of my difficulty was getting the namespace in ResourceManager right. I finally got that right by opening opening Resource.Designer.cs and combined the Namspace plus the class name to get the correct namespace string.

    Once the ResourceManager creation was correct the final piece was a simple one, rm.GetString( key, culture ); where key is the obvious string resource key and culture is an instantiation of a CultureInfo. If you are only going at the default local you will not need a CultureInfo but in my case the unit test cycles through all supported languages and thus must include the locale.


    Richard Lewis Haggard

    Tuesday, July 5, 2016 1:57 PM