Posted by: Zeeshan Amjad | April 1, 2015

ViewBag dynamically adding properties


I came across a ASP.Net MVC project and came across an interesting property ViewBag. ViewBas is an interesting property where we can add as many properties to it as we want at run time. When I saw the source code of it, it is declare dynamic that explains it. I tried to do the same thing in my simple project, but stuck with how to initialize dynamic variable. I came across EpandoObject that is for exactly same reason.

ExpandoObject implements lots of interface such as IDictionary, ICollection, IEnumerabl, and INotifyPropertyChanged. Perhaps the most interesting one is IDynamicMetaObjectProvider. This interface has only one method.

Code Snippet
public interface IDynamicMetaObjectProvider
{
    DynamicMetaObject GetMetaObject(Expression parameter);
}

 

Let’s take a look at the example to use ExpandoObject. Here is a simple program to demonstrate this.

Code Snippet
using System;
using System.Dynamic;

class Program
{
    static void Main(string[] args)
    {
        dynamic data = new ExpandoObject();

        data.FirstName = "Zeeshan";
        data.LastName = "Amjad";
        data.Company = "Bloomberg";

        Console.WriteLine(data);
        Console.WriteLine(data.FirstName);
        Console.WriteLine(data.LastName);
        Console.WriteLine(data.Company);
    }
}

The output of this program is as expected the value of properties.

Let’s try to create our own dynamic object that does something similar. .Net already provides one class named DynamicObject exactly for this purpose. All we have to do is override TryGetMeber and TrySetMember method overloaded.

Let’s create our own dynamic object class.

Code Snippet
public class MyDynamicObject : DynamicObject
{
    private readonly IDictionary<string, object> _properties = new Dictionary<string, object>();

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        return _properties.TryGetValue(binder.Name, out result);
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _properties[binder.Name] = value;
        return true;
    }

    public int Count
    {
        get { return _properties.Count; }
    }

    public IEnumerable<string> Properties
    {
        get { return _properties.Keys; }
    }
}

 

We also added two additional methods in this class to give the number of properties and the name of the properties.

Here is a complete C# program to demonstrate this.

Code Snippet
using System;
using System.Collections.Generic;
using System.Dynamic;

class Program
{
    static void Main(string[] args)
    {
        dynamic data = new MyDynamicObject();

        data.FirstName = "Zeeshan";
        data.LastName = "Amjad";
        data.Company = "Bloomberg";

        Console.WriteLine(data);
        Console.WriteLine(data.FirstName);
        Console.WriteLine(data.LastName);
        Console.WriteLine(data.Company);
        Console.WriteLine(data.Count);

        foreach (var name in data.Properties)
        {
            Console.WriteLine(name);
        }
    }
}

public class MyDynamicObject : DynamicObject
{
    private readonly IDictionary<string, object> _properties = new Dictionary<string, object>();

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        return _properties.TryGetValue(binder.Name, out result);
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _properties[binder.Name] = value;
        return true;
    }

    public int Count
    {
        get { return _properties.Count; }
    }

    public IEnumerable<string> Properties
    {
        get { return _properties.Keys; }
    }
}

Advertisements

Responses

  1. haven’t used it yey; will try though

  2. Reblogged this on Dinesh Ram Kali..


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: