Guillaume Slashy
Guillaume Slashy

Reputation: 3624

Dynamically add a Property to a System.object?

I got a list of different objects that I defined in different classes and I'd like to add a string property "Name" to all these objects. Is that possible ?

I don't have that much code to provide as my classes are very simple/classic ones.

Thanks in advance for any help !

(edit : I don't want to inherit from an abstract class that adds this property ! In fact, I don't want to modify at all my class that define my object. That's what i call "Dynamically" in the title.

What I want is something like :

myObject.AddProperty(string, "Name");

or

myObject.AddAttribute(string, "Name");

(I don't know how it is exactly called)

and then I can do :

myObject.Name = "blaaa";

Upvotes: 1

Views: 2403

Answers (3)

Steven
Steven

Reputation: 172825

You want to use the new C# 4.0 dynamic keyword:

dynamic obj = new System.Dynamic.ExpandoObject();
obj.Value = 10;
var action = new Action<string>((l) => Console.WriteLine(l));
obj.WriteNow = action;
obj.WriteNow(obj.Value.ToString());

You can not do this with object, but the ExpandoObject will do just fine.

But... overuse dynamic typing and you'll find yourself in a maintenance nightmare in the near future.

Upvotes: 0

James Hill
James Hill

Reputation: 61842

Create an abstract class that all of your other classes could inherit:

public abstract class MyBaseClass
{
    public string MyCommonString { get; set; }
}

public class Foo : MyBaseClass
{
    public MyBaseClass() { }
}

//Create instance of foo
Foo myFoo = new Foo();

//MyCommonString is accessible since you inherited from base
string commonString = myFoo.MyCommonString;

EDIT (per new requirement)

Since you don't want to touch the original classes in the DLL, I'd take this [similar] approach:

public abstract class MyBaseClass
{
    public string MyCommonString { get; set; }
}

//This class definition lives in the DLL and remains untouched
public class Foo
{
    public Foo() { }
}

//This partial class definition lives in [insert new project name here]
public partial class Foo : MyBaseClass
{
    public Foo () { }
}

Notice that Foo is now a partial class. You're not touching the existing class definition in the DLL, you're extending it.

EDIT (per newer new requirement)

Given your requirements (no editing of original class), what you're asking is not possible.

Upvotes: 2

masterchris_99
masterchris_99

Reputation: 2733

What you can do is to hard code a Hashtable named CustomProperties Now you can fill this Hashtable with custom properties

Something like that:

        MyClass myClass = new MyClass();
        myClass.SetProperty("abc", 123);
        myClass.SetProperty("bcd", "bla");
        myClass.SetProperty("cde", DateTime.Now);

        MessageBox.Show(myClass.GetProperty("abc").ToString());

class MyClass
{
    private Hashtable MyProperties { get; set; }

    public MyClass()
    {
        MyProperties = new Hashtable();
    }

    public object GetProperty(string name)
    {
        return MyProperties.Contains(name) ? MyProperties[name] : null;
    }

    public void SetProperty(string name, object value)
    {
        if (MyProperties.Contains(name))
            MyProperties[name] = value;
        else
            MyProperties.Add(name, value);
    }
}

Upvotes: 0

Related Questions