Dictionary Values โ€‹โ€‹by Link

In our application, we have some lines coming from the translation, which may contain variables. For example, the Can i have a {beverage}?part {beverage}should be replaced by a variable. My current implementation works by having a dictionary of the name and values โ€‹โ€‹of all variables and just replacing the correct line. However, I would like to register the variables by reference, so if the value always changes, the resulting string also changes. Normally passing a parameter with a keyword refmight do the trick, but I'm not sure how to store them in a dictionary.

TranslationParser:

static class TranslationParser
{
    private const string regex = "{([a-z]+)}";
    private static Dictionary<string, object> variables = new Dictionary<string,object>();

    public static void RegisterVariable(string name, object value)
    {
        if (variables.ContainsKey(name))
            variables[name] = value;
        else
            variables.Add(name, value);
    }

    public static string ParseText(string text)
    {
        return Regex.Replace(text, regex, match =>
        {
            string varName = match.Groups[1].Value;

            if (variables.ContainsKey(varName))
                return variables[varName].ToString();
            else
                return match.Value;
        });
    }
}

main.cs

        string bev = "cola";
        TranslationParser.RegisterVariable("beverage", bev);
        //Expected: "Can i have a cola?"
        Console.WriteLine(TranslationParser.ParseText("Can i have a {beverage}?")); 
        bev = "fanta";
        //Expected: "Can i have a fanta?"
        Console.WriteLine(TranslationParser.ParseText("Can i have a {beverage}?")); 

Is this possible at all, or am I just getting the problem wrong? I am afraid that the only solution would be unsafe code (pointers).

, , . ref.

+3
2

. , .

class ObjectWrapper
{
    private object _value;

    public ObjectWrapper(object value) 
    {
        _value = value;
    }

    public override string ToString()
    {
        return _value.ToString();
    }
}

static class TranslationParser
{
    private const string regex = "{([a-z]+)}";
    private static Dictionary<string, ObjectWrapper> variables = new Dictionary<string, ObjectWrapper>();

    public static void RegisterVariable(string name, object value)
    {
        var wrapped = new ObjectWrapper(value);
        if (variables.ContainsKey(name))
            variables[name] = wrapped;
        else
            variables.Add(name, wrapped);
    }

    public static string ParseText(string text)
    {
        return Regex.Replace(text, regex, match =>
        {
            string varName = match.Groups[1].Value;

            if (variables.ContainsKey(varName))
                return variables[varName].ToString();
            else
                return match.Value;
        });
    }
}

Edit:

, , . , , , ( , ). (, ) .

: !

varaible :

class Wrapper
{
    private readonly Dictionary<string, MemberExpression> _registrations = 
        new Dictionary<string, MemberExpression>();

    public void Register<T>(string name, Expression<Func<T>> expr)
    {
        _registrations[name] = (MemberExpression)expr.Body;
    }

    public object GetValue(string name)
    {
        var expr = _registrations[name];
        var fieldInfo = (FieldInfo)expr.Member;
        var obj = ((ConstantExpression)expr.Expression).Value;
        return fieldInfo.GetValue(obj);
    }
}
private static void Main(string[] args)
{
    var wrapper = new Wrapper();
    int x = 0;
    storage.Register("x", () => x);
    Console.WriteLine(wrapper.GetValue("x")); //0
    x = 1;
    Console.WriteLine(wrapper.GetValue("x")); //1
}
+1

string bev = "cola";
TranslationParser.RegisterVariable("beverage", bev);
//Expected: "Can i have a cola?"
Console.WriteLine(TranslationParser.ParseText("Can i have a {beverage}?")); 
bev = "fanta";
//Expected: "Can i have a fanta?"

{}, "cola", , : "fanta". Thie : ParseText , "" ?

:

public static string ParseText(string text, string preferedValue=null)
{
        return Regex.Replace(text, regex, match =>
        {
            string varName = match.Groups[1].Value;

            if (variables.ContainsKey(varName))
            {
                if(!string.IsNullOrEmpty(preferedValue)) //IF THERE ISPREFERED VALUE                                                         
                       return preferedValue;             //RETURN THAT ONE

                return variables[varName].ToString();
            }
            else
                return match.Value;
        });
}
0

All Articles