.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share

 Subscribe to Articles

Dynamic Object in C# 4.0 tutorial : Part I

Posted By :Dhananjay Kumar      Posted Date :11/03/2010   Points :25   Category: .Net Framework    URL: http://www.dhananjaykumar.net

Dynamic Object in C# 4.0 tutorial. This tutorial will give a basic introduction of Dynamic Object in C# 4.0.
 


Objective

This article will give a basic introduction of Dynamic Object in C# 4.0

DynamicObject class

  1. This provides a base class for specifying dynamic behavior at run time.
  2. This class must be inherited to use.
  3. This class cannot be instantiated.
  4. This class is inside namespace System.Dynamic.
  5. This class implements IDynamicMetaObjectProvider.
  6. This class enables to define which operation can be performed at the run time.
  7. This class enables to decide how to perform operations on dynamic objects.
  8. Own member can be added to class inherited from DynamicObject.

    dynamic1.gif

Override TryInvokeMember Method

  1. It provides the implementation for operations that invoke members.
  2. 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

  1. This argument provides information about dynamic operation.
  2. binder.Name provides the name of the member on which dynamic operation is performed.
  3. 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

  1. Open Visual Studio 2010 and create a new console application.
  2. Add a class. Give name. I am giving name here as MyDynamicClass
  3. Inherit the class from DynamicObject
  4. 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

dynamic2.gif



Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
Responses

No response found. Be the first to respond this post

Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend