Answer the question
In order to leave comments, you need to log in
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"] = "Сынақ"
};
}
}
}
var obj = English english = new English();
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();
}
}
}
Answer the question
In order to leave comments, you need to log in
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"] = "Сынақ"
};
}
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 questionAsk a Question
731 491 924 answers to any question