Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Adding Properties to an object at run time
Message
From
04/02/2011 07:00:08
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01498603
Message ID:
01498701
Views:
34
>>Hi,
>>
>>I have an EntityList with the following "fields" App_item, App_Type and App_Value.
>>
>>It contains things like:
>>
>>"PremiumReminderDays", "I", "10"
>>"LapseDays", "I", "5"
>>"CompanyName", "C", "Samaan Systems Ltd."
>>
>>I have an AppInfo object that I want to add properties to from this list so I end up with properties like this:
>>
>>AppInfo.PremiumReminderDays = 10
>>AppInfo.LapseDays = 10
>>AppInfo.CompanyName = "Samaan Systems Ltd."
>>
>>How do I do this in C#?
>
>Thinking about it you could make my second suggestion from the previous post more 'user-friendly' but using an indexer:
   public class LooseProperties
>    {
>        public Dictionary<string, object> Properties { get; set; }
>        public LooseProperties() { Properties = new Dictionary<string, object>(); }
>
>        public object this[string property]
>        {
>            get
>            {
>                if (Properties.ContainsKey(property))
>                    return Properties[property];
>                else
>                    return null;
>            }
>            set
>            {
>                if (!Properties.ContainsKey(property))
>                    Properties.Add(property, null);
>                Properties[property] = value;
>            }
>        }
>    }
then just:
lp["CompanyName"] = "Samaan Systems Ltd";
>lp["PremiumReminderDays"] = 10;
>//etc
>string company = (string)lp["CompanyName"];
>int days = (int) lp["PremiumReminderDays"];
OK, the indexer is interesting, let me see if I can understand the code :) But maybe using pre-defined properties is the better way?
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform