S
S
schufner2016-08-22 15:00:25
C++ / C#
schufner, 2016-08-22 15:00:25

How to implement dynamic class change in C#?

Good day! I'm in the process of learning C# so don't kick too hard.
How can you implement, best of all, a dynamic class change, or at least just change the class.
There is a dll library with five classes and each class has a Dictionary library.

using System.Collections.Generic;
namespace language_game {
    public class change_language {
        public class English {
            public Dictionary<string, string> language = new Dictionary<string, string> {
                ["test"] = "Test"
            };
        }
        public class Russian {
            public Dictionary<string, string> language = new Dictionary<string, string> {
                ["test"] = "Тест"
            };
        }
        public class Ukrainian {
            public Dictionary<string, string> language = new Dictionary<string, string> {
                ["test"] = "Тест"
            };
        }
        public class Kazakh {
            public Dictionary<string, string> language = new Dictionary<string, string> {
                ["test"] = "Сынақ"
            };
        }
    }
}

Compiling to dll.
I create instances of classes in another project and can easily call what I need.
And here is how in the line WriteLine(english.language["test"]); change the value of english.language["test"] for example to russian.language["test"] using a variable.
For example
var obj = English english = new English();
, the program code itself:
using System;
using System.Collections.Generic;
using static System.Console;
using static language_game.change_language;

namespace TestLib{
    class Program {        
        static void Main(string[] args) {
            English english = new English();
            Russian russian = new Russian();
            Ukrainian ukrainian = new Ukrainian();
            German german = new German();
            Kazakh kazakh = new Kazakh();

            WriteLine(english.language["test"]);
            ReadKey();
        }
    }
}

I may not be expressing myself correctly =) I apologize in advance! All the best!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fat Lorrie, 2016-08-22
@schufner

If I understand correctly, then you are talking about polymorphism)
Something like:

// Каждая локаль должна иметь в себе словарь с переводами
public interface class ILocale {
    Dictionary<string, string> Translation { get; }
}

// реализации разных локалей
public class English : ILocale {
    public Dictionary<string, string> Translation {get; set;} = new Dictionary<string, string> {
        ["test"] = "Test"
    };
}
public class Russian : ILocale {
    public Dictionary<string, string> Translation {get; set;}  = new Dictionary<string, string> {
        ["test"] = "Тест"
    };
}
public class Ukrainian : ILocale {
    public Dictionary<string, string> Translation {get; set;}  = new Dictionary<string, string> {
        ["test"] = "Тест"
    };
}
public class Kazakh : ILocale{
    public Dictionary<string, string> Translation {get; set;} = new Dictionary<string, string> {
        ["test"] = "Сынақ"
    };
}

And, let's say, we read from some config:
namespace TestLib{
    class Program {        
        static void Main(string[] args) {
            ILocale locale = GetLocale();
            WriteLine(locale.Translation["test"]);
            ReadKey();
        }
    }

    private static ILocale GetLocale() {
        // Мы можем получать ее динамически из конфига
        // но вызывающий код знает лишь, что это будет некая локаль, но какая
        // точно - неизвестно
        return Configuration.GetLocaleFromXml(); // псевдокод
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question