java expandoobject_使用ExpandoObject的dynamic关键字来附加和删除属性(dynamic keyword with ExpandoObject to attach a...

I know that now in C# 3.0 we can use dynamic keyword to add/remove properties in run time like the Object as general container's JavaScript concept, but i have a question.

Is it really Expand the object of the type i want ??

For Example: We have class:

class Person

{

public string Name { get; set; }

public int Age { get; set;}

}

I will create a new object from Person Class:

dynamic p = new ExpandoObject();

Now is this object really an object of class Person ? it dose not related to Person class anymore ??

Console.WriteLine(p.GetType()); // System.Dynamic.ExpandoObject

Now i will set values for properties and Expand object p with new property 'Foo':

p.Age = 25;

p.Foo = "foo";

I attached new property 'Foo' and set value for property 'Age' but i do that for an object of type System.Dynamic.ExpandoObject not of type Person so i have mismatch in this part, is i really expand the object from the type i needed, can i uses cast to refer to the object type like that:

Console.WriteLine(((Person)p).Name);

An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值