首页 新闻 会员 周边

.net dynamic 里怎样才能这样使用

0
悬赏园豆:50 [已解决问题] 解决于 2010-07-29 08:31

   dynamic t = new ExpandoObject();
            string @a="gg";
            t.@a = "galrj";
            Console.WriteLine(t.gg);

飞翔的小菜鸟的主页 飞翔的小菜鸟 | 初学一级 | 园豆:10
提问于:2010-07-22 11:53
< >
分享
最佳答案
0

不行的吧,这样行不?

class Test : System.Dynamic.DynamicObject
{
public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result)
{
if (map != null)
{
string name = binder.Name;
object value;
if (map.TryGetValue(name, out value))
{
result
= value;
return true;
}
}
return base.TryGetMember(binder, out result);
}

System.Collections.Generic.Dictionary
<string, object> map;

public override bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result)
{
if (binder.Name == "set" && binder.CallInfo.ArgumentCount == 2)
{
string name = args[0] as string;
if (name == null)
{
//throw new ArgumentException("name");
result = null;
return false;
}
if (map == null)
{
map
= new System.Collections.Generic.Dictionary<string, object>();
}
object value = args[1];
map.Add(name, value);
result
= value;
return true;

}
return base.TryInvokeMember(binder, args, out result);
}
}
static void Main(string[] args)
{
dynamic t
= new Test();
string @a = "gg";
t.
set(@a,"galrj");
Console.WriteLine(t.gg);
}
收获园豆:50
neutra | 菜鸟二级 |园豆:450 | 2010-07-24 22:40
谢谢了
飞翔的小菜鸟 | 园豆:10 (初学一级) | 2010-07-29 08:30
其他回答(1)
0

那这个对象又如何转换成json呢。。。

赤狐(zcm123) | 园豆:204 (菜鸟二级) | 2016-11-17 12:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册