Load string variables dynamically from resources file and use these same name as our variables in code? RRS feed

  • Question

  • Dear All,

    I know there is a resource file to define multilingual strings for shows text of variable control.

    If these strings are not used in control but in code used.

    Is it possible import the defined name in resource file as our variables in code?

    For example,

    We would like to show one root name and three branches name in local language,

    For English, the variable names and their values are

    Root = "Root"

    BranchType1 = "Apple"

    BranchType2 = "Banana"

    BranchType3 = "Cherry"

    For zh-TW, the variable names and their values are

    Root = "根"

    BranchType1 = "蘋果"

    BranchType2 = "香蕉"

    BranchType3 = "櫻桃"

    Is it possible we get the variable name Root, BranchType1, BranchType2, BranchType3 as defined in resource file 

    and use these variable in our code?



    Thus we don't need to define twice and there may be mismatch if we define twice?

    Thanks and Best regards,


    Friday, May 4, 2018 9:37 AM

All replies

  • I think you've asked variants of this question before and the answer continues to be the same. Variables/identifiers are baked into the code at compilation time. You cannot dynamically (at runtime) change the variable names you're pointing to directly in C#. Variables are nothing more than named memory locations so "finding" them at runtime doesn't really even make sense. You're attaching entirely too much meaning between arbitrary variable name A and something in your code. Imagine this, suppose you F2 one of the identifiers and rename it. Did you just break any of your code at runtime? If so then you've probably done something wrong.

    You really need to stop trying to put any sort of meaning (at runtime) to the name of arbitrary variables at compile time. They don't translate. Instead approach the problem from the point of view of what problem you're trying to solve. That will more cleared indicate the direction you need to go. For example if you want to associate a resource value (at runtime) to some variable then have the variable look up the resource value itself.

    var root = //Get resource named "Root"
    var branchType1 = //Get resource named "BranchType1"

    given your casing it appears you may be referring to properties on a type. You do have access to reflection which would allow you to (given an instance) find a property at runtime.

    public class SomeType
       public string Root { get; set; }
       public string BranchType1 { get; set; }
    var instance = new SomeType();
    //Get the Root property
    var prop = instance.GetType().GetProperty("Root");
    //Set the value
    prop.SetValue(instance, "Test");

    But using reflection is slow so it isn't something you'd want to do when there are better ways to solve the problem. Other than for readability, the name of an identifier shouldn't have any impact on your code. Now suppose you did want to be able to "map" properties to resource values. In that case use an attribute.

    public class SomeType
       public string Root { get;set; }
    Here you're associating an arbitrary string value with an arbitrary property. The fact that the names match is convenience. At runtime you could then enumerate the properties of the type and get the "resource" associated with each one using reflection. It wouldn't be the fastest code in the world but it would work.

    Michael Taylor

    Friday, May 4, 2018 3:10 PM