Marco
Marco

Reputation: 2336

C# Dynamic extends a object

is it possible to extends a existing object ?

i have the code

var record = new
{
    id,
    name
};

and have a list of anonymous objects

var list = new List<object>(){ object1, object2 };

Can i add them later to the object ? Like something as

foreach (var o in list)
{
    record.add(o);
}

that i will get this as result

var record = new
{
    id,
    name,
    object1,
    object2
};

Upvotes: 4

Views: 7221

Answers (4)

Rophuine
Rophuine

Reputation: 724

In case anyone runs into this question in the future, I have recently published a library to do exactly this. You can find it on nuget.org - it's called (unsurprisingly) ObjectExtend.

You can install it by grabbing it from Nuget or via your favourite package manager. You can also check out the source code, a brief introduction, or a detailed overview of how it works.

The short version is - install the package, make sure you import the namespace with using Rophuine.LINQPad.ObjectExtend;, and now you should be able to call .Extend on your objects.

A caveat: this is a great technique for exploratory coding, but I recommend against it for anything which will be maintained or go to production.

Upvotes: 1

rscarvalho
rscarvalho

Reputation: 552

What you can do is create a class Extension. It is not possible to add new methods in the runtime, but you can do something like this:

public class OneClass
{
  private List<object> items;
  public List<object> Items { get { return items; } }
  public void AddOne(object item)
  {
    items.Add(item);
  }
}

if you want to extend this class behavior, you can write an extension class. Like this:

public static class OneClassExtensions
{
  public void AddMany(this OneClass self, params object[] items)
  {
    foreach(object item in items)
    {
      self.Items.Add(item);
    }
  }
}

This way you can call this extension method from your OneClass objects:

OneClass obj = new OneClass();
obj.AddOne("hello");
obj.AddMany("Hello", "world"); // Extension method

There are some rules to follow:

  1. The extension class must have the `static' modifier
  2. you need to put the `this' prefix before the first argument. This argument would be the object itself.
  3. In order to use this extension class in your code, you must use the namespace that contains that extension class, like `using Some.Namespace.That.Has.An.Extension' in every .cs file where you want to use extension methods.

Upvotes: 1

CaptainPlanet
CaptainPlanet

Reputation: 735

Since .net4 you could use ExpandoObject to do stuff like that.

For example:

        var objs = new List<ExpandoObject>();

        for (var i = 0; i < 10; i++)
        {
            dynamic eObj = new ExpandoObject();
            eObj.Property = i;
            objs.Add(eObj);
        }

        foreach (dynamic obj in objs)
        {
            obj.Property2 = "bubuValue" + obj.Property;
            obj.Property3 = "bubuValue" + obj.Property2;
        }

        foreach (dynamic obj in objs)
        {
            Console.WriteLine(obj.Property3);
        }

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1064244

In short, no. At least, not with anonymous types. There are two approaches here; dynamic might give you what you want, but is fiddly for combining. Other than that, a basic property bag - even simply Dictionary<string,object> would do. The only difference being that:

obj.id

becomes

obj["id"]

There is a more fundamental problem, though, in trying to combine a list (each of which is largely anonymous) with properties in a single step. You can do this for data-binding purpose via custom property models, but it is... tricky.

Upvotes: 2

Related Questions