c# dynamic的属性是个变量
2016-07-06 15:07
399 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 { public class Student : 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); } } class Program { static void Main(string[] args) { dynamic t = new Student(); string @a = "ggff"; t.set(@a, "galrj"); Console.WriteLine(t.ggff); Console.ReadKey(); } } }
相关文章推荐
- XNA 图形特效与文字显示
- C#学习篇(四)C#迭代器
- c#装B指南
- 复制幻灯片(包括格式、背景、图片等)到同/另一个PPT文档
- C#操作XML
- RichTextBox实现关键字自定义颜色显示(C#)
- C#加壳(ILMerge)
- 在 Visual C# 中,怎样为 TextBox 控件添加水印文字
- C# 数据导出Excel,及Excel各种操作
- c#获取图片的高和宽
- 一点一点学C#1
- C#正则表达式的零宽断言
- ArcGis:vs c#编程遇到问题The specified filename is invalid
- C# 深拷贝的具体代码的封装与调用
- C Sharp里的委托和事件
- c# BackGroundWorker 多线程操作的小例子
- C#中的TabControl控件的选项卡如何隐藏?
- C#文件的压缩和解压(ZIP)使用DotNetZip封装类操作zip文件(创建/读取/更新)实例
- 语法优化-多条件判断
- C# 判断时间格式是否正确,多种判断