0

I have a setting file that is formatted like so:

[Database]
Host=localhost
Username=root
Password=root

Now, I have a class named Settings which is a DynamicObject so I can use dynamic settings. When I parse the file, the key for the dictionary that contains the settings is the header + key. So, for example, the key in the Host case would be Database.Host and the value would be localhost.

Now, I want the user to use it like so:

dynamic settings = new Settings("Config.ini");

string host = settings.Database.Host;

However, when I override TryGetMember, GetMemberBinder only contains Name, and the Name is Database and not Database.Host as it should be. How can I "split" the GetMemberBinder Name property? When I use . the Name property doesn't contain what's after the ..

For example,

when I use:

settings.Database.Host

then

GetBinderName Name only contains Database, but it should contain:

Database.Host.

Thanks.

**EDIT: ** I have another question. Let's say I have string name = "Database", and then I do settings.Name.Host. It doesn't resolve Name as Database. Is there a solution to it?

user3865229
  • 125
  • 2
  • 9

1 Answers1

0

settings.Database is an object not a dynamic, you have no control over it. Writing settings.Database_Host is a simple solution even not so beatiful than settings.Database.Host.

If you don't know the section, only the property, you can still "cheat" in your resolver: if the property starts with a special string (e.g. __) it means to search for all section: settings.__Host should find Host entry's value under [Database] or [Server] or any other place. You can even return string[] in TryGetMember in this case instead of the first match if you wish.

Peter Krassoi
  • 571
  • 3
  • 11
  • I have another question. Let's say I have string name = "Database", and then I do settings.Name.Host. It doesn't resolve Name as Database. Is there a solution to it? – user3865229 Aug 14 '14 at 06:57