How to create C # configuration files for each of my classes?

If I go to Project -> Myproject Properties -> Settings, I can create a settings file for the entire project. However, it is assumed that each class requires its own configuration file. Is there a similar way to do this at the class level?

As an example, suppose I have a parent class Car with subclasses of Ford and Honda. I want to have one YEAR property and a single code to read the YEAR property. I could do this by having two configuration files with the same YEAR property. If I used Ford.YEAR and Honda.YEAR, I would need two separate pieces of code to analyze the data, which could become messy for a large number of classes.

+3
source share
6 answers

It is not intended for this.

You can use the System.Configuration.ConfigurationSettings classes to explicitly open the file in code to read your settings. This will work, but the designer will not give you any help in creating your settings files.

Do you have a problem with class settings?

Another way that can help you is to create a custom configuration section that you can put in a file. You can then split each of your settings into your own configuration section. Can this satisfy your goals?

+2
source

( , ) . , ClassName.ConfigName.

+2

: ConfigurationSection

.net, . .

, MyApplication.exe, MyLibrary.dll, , MyApplication.exe.config.

+1

, . , , , .

0

, , , ( ConfigManager).

, ?

. , , - .

, , ?

0

IoC (DI). , XML DI-, . IoC , XML , . IoC StructureMap:

IContainer c = new Container();
c.Configure(ce=>
  ce.For(typeof(A)).Use(typeof(A)).WithProperty("Test").EqualTo("Hello"));
var a = c.GetInstance<A>();
Debug.Assert(a.Test == "Hello");

XML , , , , API, , .

0
source

Source: https://habr.com/ru/post/1729728/


All Articles