C#.NET扩展的动态对象支持动态属性和方法以及Json格式与对象互转
C#.NET扩展的动态对象支持动态属性和方法以及Json格式与对象互转
C# Code:
dynamic theObj = new MyDynamicObject();
theObj.Name = "我的动态属性名称";//动态属性
theObj.Age = 10;
theObj.Model = "www.cscode.net C/S框架网修订(2017-09-18)";
string jsonString = JsonConvert.SerializeObject(theObj);
//{"Name":"我的动态属性名称","Age":10,"Model":"www.cscode.net C/S框架网修订(2017-09-18)"}
dynamic eo2 = JsonConvert.DeserializeObject<MyDynamicObject>(jsonString);
string value1 = eo2.Title;
string value2 = eo2.Content;
string value3 = eo2.Name;
string value4 = eo2.Age.ToString();
string value5 = eo2.Model;
//动态方法,这里不能没法定义参数,调用的时候可以是任意多参数,具体参数类型和含义就只能自己去小心处理了.
theObj.show = MyDynamicDelegateObj.CreateDelegate(OnTestDynamicDelegate);
theObj.show("Hello OK:");
theObj.shows("hello");//无此方法,抛出异常
//来源:C/S框架网(www.csframework.com) QQ:1980854898
dynamic theObj = new MyDynamicObject();
theObj.Name = "我的动态属性名称";//动态属性
theObj.Age = 10;
theObj.Model = "www.cscode.net C/S框架网修订(2017-09-18)";
string jsonString = JsonConvert.SerializeObject(theObj);
//{"Name":"我的动态属性名称","Age":10,"Model":"www.cscode.net C/S框架网修订(2017-09-18)"}
dynamic eo2 = JsonConvert.DeserializeObject<MyDynamicObject>(jsonString);
string value1 = eo2.Title;
string value2 = eo2.Content;
string value3 = eo2.Name;
string value4 = eo2.Age.ToString();
string value5 = eo2.Model;
//动态方法,这里不能没法定义参数,调用的时候可以是任意多参数,具体参数类型和含义就只能自己去小心处理了.
theObj.show = MyDynamicDelegateObj.CreateDelegate(OnTestDynamicDelegate);
theObj.show("Hello OK:");
theObj.shows("hello");//无此方法,抛出异常
//来源:C/S框架网(www.csframework.com) QQ:1980854898
C# Code:
private object OnTestDynamicDelegate(dynamic sender, params object[] args)
{
if (args != null && args.Length > 0)
{
MessageBox.Show(args[0].ToString() ":" sender.Name);
}
else
{
MessageBox.Show(sender.aaa);
}
return null;
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
private object OnTestDynamicDelegate(dynamic sender, params object[] args)
{
if (args != null && args.Length > 0)
{
MessageBox.Show(args[0].ToString() ":" sender.Name);
}
else
{
MessageBox.Show(sender.aaa);
}
return null;
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网