首页 > 代码库 > dynamic 使用
dynamic 使用
dynamic a = new { A = 1, B = 2 }; Console.WriteLine("a.A=" + a.A); dynamic b = new Dictionary<string,int>(); b["A"] = 6; Console.WriteLine("b[\"A\"]=" + b["A"]); //Following opration is not supported //b.A=6; //Console.WriteLine("b[\"A\"]=" + b.A); var c = func(); Console.WriteLine("c[0].A=" + c[0].A); //Access c[0].UnknowProperty will cause a RuntimeBinderException dynamic test = new TestClass(); test.P1 = 34; test.P2 = func(); Console.WriteLine("test.P1=" + test.P1); Console.WriteLine("test.P2[1].B=" + test.P2[1].B); static dynamic[] func() { return new dynamic[]{new { A = 124, B = 345 },new { A = 2345, B = 890 }}; } public class TestClass { public int P1 { get; set; } public dynamic P2 { get; set; } }
[Serializable] public class DynamicObject : IDynamicMetaObjectProvider { protected DynamicObject(); public virtual IEnumerable<string> GetDynamicMemberNames(); public virtual DynamicMetaObject GetMetaObject(Expression parameter); public virtual bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result); public virtual bool TryConvert(ConvertBinder binder, out object result); public virtual bool TryCreateInstance(CreateInstanceBinder binder, object[] args, out object result); public virtual bool TryDeleteIndex(DeleteIndexBinder binder, object[] indexes); public virtual bool TryDeleteMember(DeleteMemberBinder binder); public virtual bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result); public virtual bool TryGetMember(GetMemberBinder binder, out object result); public virtual bool TryInvoke(InvokeBinder binder, object[] args, out object result); public virtual bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result); public virtual bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value); public virtual bool TrySetMember(SetMemberBinder binder, object value); public virtual bool TryUnaryOperation(UnaryOperationBinder binder, out object result); }
dynamic 使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。