C#DynamicObject和ExpandoObject用法
class Program
{
static void Main(string[] args)
{
//dynamic v = new ExpandoObject();
//v.Name = "123";
//v.Value = "456";
dynamic v = new MyDynamicObject();
v.Name = "Name";
v.Value = "Value";
Console.WriteLine(v.Name);
Console.ReadLine();
}
}
public class MyDynamicObject : DynamicObject
{
// The inner dictionary.
private Dictionary<string, object> _Values = new Dictionary<string, object>();
// Getting a property.
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return _Values.TryGetValue(binder.Name, out result);
}
// Setting a property.
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_Values[binder.Name] = value;
return true;
}
public string ToJson()
{
return JsonConvert.SerializeObject(_Values);
}
public Dictionary<string, object> Values
{
get { return this._Values; }
}
}
public class MyDynamicObjectCollection : IEnumerable
{
private IList _Items = new ArrayList();
public void Add(MyDynamicObject dynamicObject)
{
_Items.Add(dynamicObject.Values);
}
public string ToJson()
{
return JsonConvert.SerializeObject(_Items);
}
public IEnumerator GetEnumerator()
{
return _Items.GetEnumerator();
}
}
相關文章
- C# ExpandoObject用法C#Object
- c# dynamic和ExpandoObjectC#Object
- C# ExpandoObject的使用C#Object
- C# 4.0 新物件ExpandoObjectC#物件Object
- C#-----ExpandoObject動態屬性C#Object
- c# ExpandoObject動態擴充套件物件C#Object套件物件
- 理解C# 4 dynamic(2) – ExpandoObject的使用C#Object
- NSHashTable和NSMapTable用法APT
- HttpWebRequest和WebClient的用法HTTPWebclient
- @Async的用法和示例
- startActivityForResult()和onSaveInstanceState()用法
- oncopy和onpaste事件用法AST事件
- jQuery map和each用法jQuery
- java和redies的用法Java
- linux中#和## 用法Linux
- AT NEW 和AT END OF的用法
- AT NEW 和AT END OF的用法。
- Laravel 模型的 scope 普通用法和全域性用法Laravel模型
- C++【vector】用法和例子C++
- getAttribute()和setAttribute()用法
- Hbase簡介和基本用法
- rowspan和colspan用法詳解
- Makefile和GCC日常用法GC
- Linux shell 用法和技巧Linux
- Linux shell用法和技巧Linux
- sql pivot、unpivot和partition by用法SQL
- zookeeper 用法和日常運維運維
- CRSCTL和SRVCTL的命令用法
- react useMemo 和 useCallBack 的用法React
- RecyclerView用法和原始碼深度解析View原始碼
- 淺談Invoke 和 BegionInvoke的用法
- C++【stack/queue】用法和例子C++
- C++【string】用法和例子C++
- js中try和catch的用法JS
- 【Python】*args 和 **kwargs的用法Python
- javascript callee和caller屬性用法JavaScript
- sql語句中as的用法和作用SQL
- jQuery offset()和position()用法詳解jQuery