Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Adding Properties to an object at run time
Message
From
03/02/2011 14:10:26
 
 
To
03/02/2011 13:08:22
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01498603
Message ID:
01498624
Views:
47
>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#?

A couple of ideas:
(a) See the DynamicObject class (actually I think you need .net 4 for that)
(b) Use an embedded dictionary. Something like:
public class LooseProperties
    {
        public Dictionary<string, object> Properties { get; set; }
        public LooseProperties() { Properties = new Dictionary<string, object>(); }
       // Add other standard properties 
    }
then:
LooseProperties lp = new LooseProperties();
//To store:
lp.Properties.Add("PremiumReminderDays", 1);
lp.Properties.Add("CompanyName","Samaan Systems Ltd");
//To retrieve:
string company = (string) lp.Properties["CompanyName"];
But, TBH, it introduces a lot of opportunities to cause exceptions at run time and, at least to me, smacks of bad design.....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform