Hi;
I rewrote your person class; here it is:
public class Person : DynamicObject
{
public string Name { get; set; }
public string Address { get; set; }
Dictionary<string, object> dictionary
= new Dictionary<string, object>();
public int Count
{
get
{
return dictionary.Count;
}
}
public override bool TryGetMember(
GetMemberBinder binder, out object result)
{
string name = binder.Name;
return dictionary.TryGetValue(name, out result);
}
public override bool TrySetMember(
SetMemberBinder binder, object value)
{
dictionary[binder.Name] = value;
return true;
}
public void AddProperty<TTValue>(string key, TTValue value = default(TTValue))
{
dictionary[key] = value;
}
public void AddProperty(string typeName, string key, object value = null)
{
Type type = Type.GetType(typeName);
dictionary[key] = Convert.ChangeType(value, type);
}
}
You can use it like this:
dynamic p = new Person();
p.Name = "john";
p.Address = "address1";
p.Manager = "My Manager";
p.AddProperty<DateTime>("BirthDate", DateTime.Now);
p.AddProperty("System.String", "Weigth", "70 kg");
Console.WriteLine("My name is {0}\nMy manager is {1}\nMy birth Date is {2}",
p.Name, p.Manager, p.BirthDate);
string w = p.Weigth;
Console.WriteLine("My Weigth is: " + w);
Console.ReadKey();
Hope i have been helpful.