开发者

Create object attributes on the fly from a name/value pair catalogue?

开发者 https://www.devze.com 2023-01-05 22:20 出处:网络
I have this DB Table of \'Settings\' that\'s like: MopedID, Name, Value I want to make an object in my Base Controller that is populated with attributes from whatever is stored there.

I have this DB Table of 'Settings' that's like:

MopedID, Name, Value

I want to make an object in my Base Controller that is populated with attributes from whatever is stored there.

So if it's:

32, "EnableHyperCanon", "True"

The object at run time when I access it from any Controllers that inherit from the Base will have access to MopedSettings.EnableHyperCanon which would be the string value "True".

The object would only ever have attributes that it find for ID 32.

Any ide开发者_如何学运维a?

EDIT: Maybe a better idea would be MopedSettings["EnableHyperCanon"] is there a way to create an object that works this way?


If you're running on .NET 4.0, you can use ExpandoObject.


To make your object indexable:

class MopedSettings
{
    private Dictionary<string,object> _dic;
    public object this[string s] { get { return _dic[s]; }; set { _dic[s] = value; } }
}

I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消