下載app免費領取會員
class Program
{
static void Main(string[] args)
{
//dynamic v = new ExpandoObject();
//v.Name = "123";
//v.Value = "456";
dynamic v = new TestClass();
v.Name = "Name";
v.Value = "Value";
Console.WriteLine(v.Name);
Console.ReadLine();
}
}
public class TestClass:DynamicObject
{
private Dictionary<string, object> _dynamicData = new Dictionary<string, object>();
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = null;
if(_dynamicData.Keys.Contains(binder.Name))
{
result = _dynamicData[binder.Name];
return true;
}
return false;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if(_dynamicData.Keys.Contains(binder.Name))
{
_dynamicData[binder.Name] = value;
}
else
{
_dynamicData.Add(binder.Name, value);
}
return true;
}
}
本文版權歸腿腿教學網及原創(chuàng)作者所有,未經授權,謝絕轉載。