2

I've read few posts, and I'm still having troubles with adding properties to a class in runtime. It should be simple, because I have a class like this:

public class MyClass
    {
        String Template;
        String Term;
     }

During runtime, I have to add few attributes, like Phone, Email (it depends...). Could someone please explain me how to add these properties during class initialization?

Srecko

2
  • Why do you feel you need to add them at runtime? Why can't you add them to your class definition? Commented Jun 21, 2012 at 8:29
  • It is little bit complicated to explain, but I need to add them when I instantiate class. I cannot add them in class definition, because database table contains few fields, and I need to add few more during runtime. Commented Jun 21, 2012 at 8:33

3 Answers 3

4

I don't think adding a property is the right thing to do here. The attributes like "Email" or "Phone" are just some additional pairs of a key and a value. You could use a Dictionary, but that would prevent you from using a key more than once (more than one email address for a contact for example). So you could just as well use a List<KeyValuePair<string, string>>. Like that:

public class MyClass
{
    String Template;
    String Term;
    public List<KeyValuePair<string, string>> Attributes { get; private set; }

    public MyClass() {
        Attributes = new List<KeyValuePair<string, string>();
    }

    public void AddAttribute(string key, string value) {
        Attributes.Add(new KeyValuePair<string, string>(key, value));
    }
}

// to be used like this: 
MyClass instance = new MyClass();
instance.AddAttribute("Email", "[email protected]");
instance.AddAttribute("Phone", "555-1234");
Sign up to request clarification or add additional context in comments.

5 Comments

I think this should work. Actually, I need to add property only once. I have to try...
@Joksimovic If you are sure that you only need to add a property once, then a Dictionary<string, string> would be a good choice, too.
@Joksimovic With a Dictionary, you could also check if keys exist easier, like someDictionary.ContainsKey("Email")
Yes, that is true. I think that Dictionary should do the work. I am going to try this in next few minutes...
Thank you for your help! As you, and Carsten suggested, Dictionary did the job.
3

If you have c# 4.0 you can use the Expando object.

for earlier versions of c#, the generally accepted way of doing this is to create a "property bag" i.e. a collection (or dictionary) of key value pairs

dynamic foo = new ExpandoObject();
foo.Bar = "test";

2 Comments

I have C# 4.0, but isn't Expando used for dinamically created objects? In this case, I have class already, and I want to add few more properties during initialization...
well, yes it is a dynamic object and I suppose you could create them dynamically. It pretty much is designed to work as a typed property bag, sounds like it would suit your purpose.
1

you could add an dictionary with for your Key/Value-Pairs. Then if you add your attributes you just add Key = Attributename, Value = YourValue to the dictionary. Reading is as easy - just get the Value to the Key = Attributename from your dictionary.

1 Comment

unfortunately, I cannot accept two answers. Your answer was helpful as well. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.