Objective
This article will give a basic introduction of Dynamic Object in C# 4.0
DynamicObject class
-
This provides a base class for specifying dynamic behavior at run time.
-
This class must be inherited to use.
-
This class cannot be instantiated.
-
This class is inside namespace System.Dynamic.
-
This class implements IDynamicMetaObjectProvider.
-
This class enables to define which operation can be performed at the run time.
-
This class enables to decide how to perform operations on dynamic objects.
-
Own member can be added to class inherited from
DynamicObject.
Override TryInvokeMember Method
-
It provides the implementation for operations that invoke members.
-
Class derived from DynamicObject class can override this method to specify dynamic behavior for operation such as calling a method.
This method is defined as below,
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
}
There are three arguments for this method
Binder
-
This argument provides information about dynamic operation.
-
binder.Name provides the name of the member on which dynamic operation is performed.
-
Type of this argument is InvokeMemberBinder.
Let us say, myDynamicObject is name of the instance and myDynamicMethod is name of the dynamic method in class inherited from DynamicObject. If you are calling
myDynamicObject. myDynamicMethod then in that case binder.Name = myDynamicMethod
Args
This parameter defines the arguments pass as the input to the method of dynamic class. Let us say myDynamicMethod takes two input parameters int x and string y. so Args parameter will define these two input parameters of the method in dynamic class.
Type: array of System.Object []
Result
This is result of the system invocation.
If result = true then operation is successful.
If result=false then, then runtime binder of language determine the behavior. In most cases it will throw run time exception.
Note: If you override the TryInvokeMember method, the dynamic dispatch system first attempts to determine whatever specified method exists in the class. If it does not find the method , it uses the TryInvokeMember implementation.
Sample
-
Open Visual Studio 2010 and create a new console application.
-
Add a class. Give name. I am giving name here as MyDynamicClass
-
Inherit the class from DynamicObject
-
Overriding TryInvokeMember method.
MyDynamicClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic ;
namespace DynamicObjectsample
{
class MyDynamicClass : DynamicObject
{
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
if (string.Equals(binder.Name, "MyMethod"))
{
Console.WriteLine("U have hacked");
result = true;
return true;
}
else
{
Console.WriteLine(binder.Name + " Method not present in this class ");
result = true;
return true;
}
}
}
}
If you see the above class, we are overriding TryInvokeMember. We are checking if method name called is MyMethod or not?
Using the MyDynamicClass
namespace DynamicObjectsample
class Program
{
static void Main(string[] args)
{
dynamic obj = new MyDynamicClass();
obj.MyMethod();
Console.ReadKey();
obj.Abc();
Console.ReadKey();
}
}
}
Output