Dynamic Object包含在.Net4.0中,該對象可以允許我們在運行時中改變它的類型,有了這種萬金油還有什麼問題是我們不能解決的呢?我們來看看它的幾個使用例子吧:
Var類型和Dynamic類型
[csharp]
public static void TestDynamicObject()
{
//var object and dynamic object
var varObj = "this is a var object";
dynamic dyObj = "this is a dynamic object";
Console.WriteLine(varObj);
Console.WriteLine(dyObj);
dyObj = new string[] { dyObj, "I also can do it liking this!" };
Console.WriteLine(dyObj[0]);
Console.WriteLine(dyObj[1]);
/*OUT PUT
this is a var object
this is a dynamic object
this is a dynamic object
I also can do it liking this!
*/
}
public static void TestDynamicObject()
{
//var object and dynamic object
var varObj = "this is a var object";
dynamic dyObj = "this is a dynamic object";
Console.WriteLine(varObj);
Console.WriteLine(dyObj);
dyObj = new string[] { dyObj, "I also can do it liking this!" };
Console.WriteLine(dyObj[0]);
Console.WriteLine(dyObj[1]);
/*OUT PUT
this is a var object
this is a dynamic object
this is a dynamic object
I also can do it liking this!
*/
}
ExpandObject
[csharp]
public static void TestExpandObject()
{
dynamic data = new ExpandoObject();
data.FirstAttribute = "FirstAttribute";
data.SecondAttribute = "SecondAttribute";
data.ThirdAttribute = "ThirdAttribute";
ArrayList nums = new ArrayList();
for (int i = 1; i <= 10; i++)
{
nums.Add(i);
}
data.Nums = nums;
foreach (int tmp in data.Nums)
{
Console.WriteLine(tmp);
}
Console.WriteLine(data.FirstAttribute);
Console.WriteLine(data.SecondAttribute);
Console.WriteLine(data.ThirdAttribute);
Action GB = () => { Console.WriteLine("This is dynamic method."); };
data.MyMethod2 = GB;
data.MyMethod2();
Action<string> GhostBear = a => Console.WriteLine(a);
data.MyMethod = GhostBear;
data.MyMethod("Go!Go!Fighting!");
/*OUT PUT
1
2
3
4
5
6
7
8
9
10
FirstAttribute
SecondAttribute
ThirdAttribute
This is dynamic method.
Go!Go!Fighting!
*/
}
public static void TestExpandObject()
{
dynamic data = new ExpandoObject();
data.FirstAttribute = "FirstAttribute";
data.SecondAttribute = "SecondAttribute";
data.ThirdAttribute = "ThirdAttribute";
ArrayList nums = new ArrayList();
for (int i = 1; i <= 10; i++)
{
nums.Add(i);
}
data.Nums = nums;
foreach (int tmp in data.Nums)
{
Console.WriteLine(tmp);
}
Console.WriteLine(data.FirstAttribute);
Console.WriteLine(data.SecondAttribute);
Console.WriteLine(data.ThirdAttribute);
Action GB = () => { Console.WriteLine("This is dynamic method."); };
data.MyMethod2 = GB;
data.MyMethod2();
Action<string> GhostBear = a => Console.WriteLine(a);
data.MyMethod = GhostBear;
data.MyMethod("Go!Go!Fighting!");
/*OUT PUT
1
2
3
4
5
6
7
8
9
10
FirstAttribute
SecondAttribute
ThirdAttribute
This is dynamic method.
Go!Go!Fighting!
*/
}
自定義擴展的Dynamic對象(需要繼承DynamicObject類)
[csharp]
class MyDynamicObject : DynamicObject
{
Dictionary<string, object> data = new Dictionary<string, object>();
/// <summary>
///
/// </summary>
/// <param name="binder">傳進來的參數,包含了調用信息</param>
/// <param name="result">返回包含的對象</param>
/// <returns>返回是否成功</returns>
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
bool isContain = false;
if (data.ContainsKey(binder.Name))
{
result = data[binder.Name];
isContain = true;
}
else
{
result = "Not Find";
}
return isContain;
}
/// <summary>
///
/// </summary>
/// <param name="binder">傳進來的參數,包含了調用信息</param>
/// <param name="value">傳進來的參數,包含了調用信息</param>
/// <returns></returns>
public override bool TrySetMember(SetMemberBinder binder, object value)
{
bool result = false;
if (!data.ContainsKey(binder.Name))
{
data[binder.Name] = value;
result = true;
}
return result;
}
/// <summary>
///
/// </summary>
/// <param name="binder">傳進來的參數,包含了調用信息</param>
/// <param name="args">傳進來的參數,包含了調用信息</param>
/// <param name="result">方法體執行後返回的結果</param>
/// <returns>調用是否成功</returns>
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
dynamic tmp = data[binder.Name];
result = null;
tmp((string)args[0]);
return true;
}
}
class MyDynamicObject : DynamicObject
{
Dictionary<string, object> data = new Dictionary<string, object>();
/// <summary>
///
/// </summary>
/// <param name="binder">傳進來的參數,包含了調用信息</param>
/// <param name="result">返回包含的對象</param>
/// <returns>返回是否成功</returns>
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
bool isContain = false;
if (data.ContainsKey(binder.Name))
{
result = data[binder.Name];
isContain = true;
}
else
{
result = "Not Find";
}
return isContain;
}
/// <summary>
///
/// </summary>
/// <param name="binder">傳進來的參數,包含了調用信息</param>
/// <param name="value">傳進來的參數,包含了調用信息</param>
/// <returns></returns>
public override bool TrySetMember(SetMemberBinder binder, object value)
{
bool result = false;
if (!data.ContainsKey(binder.Name))
{
data[binder.Name] = value;
result = true;
}
return result;
}
/// <summary>
///
/// </summary>
/// <param name="binder">傳進來的參數,包含了調用信息</param>
/// <param name="args">傳進來的參數,包含了調用信息</param>
/// <param name="result">方法體執行後返回的結果</param>
/// <returns>調用是否成功</returns>
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
dynamic tmp = data[binder.Name];
result = null;
tmp((string)args[0]);
return true;
}
}
[csharp]
public static void TestExtendDynamicObject()
{
dynamic myDynamicObject = new MyDynamicObject();
myDynamicObject.FirstAttribute = "FirstAttribute";
Action<string> myMethod = a => Console.WriteLine(a);
myDynamicObject.myMethod = myMethod;
Console.WriteLine(myDynamicObject.FirstAttribute);
myDynamicObject.myMethod("this is Dynamic Object");
/*OUT PUT
FirstAttribute
this is Dynamic Object
*/
}
public static void TestExtendDynamicObject()
{
dynamic myDynamicObject = new MyDynamicObject();
myDynamicObject.FirstAttribute = "FirstAttribute";
Action<string> myMethod = a => Console.WriteLine(a);
myDynamicObject.myMethod = myMethod;
Console.WriteLine(myDynamicObject.FirstAttribute);
myDynamicObject.myMethod("this is Dynamic Object");
/*OUT PUT
FirstAttribute
this is Dynamic Object
*/
}