This thing is something new .NET Framework 4.5 ..
found that this is probably just as excited discovered the New World, let me praise God .. Anders Hejlsberg once again
here are MSDN: http://msdn.microsoft.com/en -us / library / system.dynamic.expandoobject.aspx
simply look at his convenient place ..
I build an object:
1.
public
class
Product
2.
{
3.
public
string
Name {
get
;
set
; }
4.
}
This time if I was in 'implementation period', the need to add a dynamic property of ..
called the Description how to do it?!
01.
/// <summary>
02.
/// 转成可以扩充的对象
03.
/// </summary> www.it165.net
04.
/// <param name="obj"></param>
05.
/// <returns></returns>
06.
public
dynamic ConvertToDynamic(
object
obj)
07.
{
08.
IDictionary<
string
,
object
> result =
new
ExpandoObject();
09.
10.
foreach
(PropertyDescriptor pro
in
TypeDescriptor.GetProperties(obj.GetType()))
11.
{
12.
result.Add(pro.Name, pro.GetValue(obj));
13.
}
14.
15.
return
result
as
ExpandoObject;
16.
}
I will write an original method of Product objects, turn into Key Value patterns turn into ExpandoObject
then you can add a new property in the 'implementation period' ..
01.
Product p1 =
new
Product();
02.
p1.Name =
"商品名称"
;
03.
04.
05.
dynamic exProd = ConvertToDynamic(p1);
06.
//下面两个属性都是 在执行期可被扩充出来的
07.
exProd.Description =
"叙述"
;
08.
exProd.NewProperty1 =
"我是新属性"
;
09.
10.
Response.Write(exProd.Name +
","
+ exProd.Description +
","
+ exProd.NewProperty1);
result:
It really expanded in the above, are very useful things ...