首页 > 代码库 > .net dynamic动态加属性
.net dynamic动态加属性
- 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);
- }
http://blog.csdn.net/cqims21/article/details/17613733
https://q.cnblogs.com/q/15970/
.net dynamic动态加属性
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。