C# Default Binder


In an earlier post while discussing about the new dynamic keyword in C# 4, I mentioned about DLR (Dynamic Language Runtime). DLR is part of .NET Framework 4 and allows late dynamic binding for dynamic languages like IronPython and IronRuby. Under the hood C# also uses DLR for resolving methods/properties while using dynamic in C#.

In the same blog post I mentioned that normally for all other scenarios, C# compiler uses the default static Binder, which binds members statically during compilation. The Default Binder is exposed through Type.DefaultBinder property, as part of .NET Reflection API. Following example shows how to use it from C# code.

I declared a public class called Calculator which has a method called Add with three overloads –

public class Calculator

    {

        public int Add(int val1, int val2) { return val1 + val2; }

        public double Add(double val1, double val2) { return val1 + val2; }

        public float Add(float val1, float val2) { return val1 + val2; }

    }

I tried to use the Default Binder to binding a method call to particular overload –

     // Get a reference of the Default Binder

            Binder defaulttBinder = Type.DefaultBinder;

            // Create a MethodInfo array of all public methods of Calculator class

            MethodInfo[] calculatorMetods = Assembly.GetExecutingAssembly().GetExportedTypes()[0].GetMethods();           

            // declare an object array of parameter values

            object[] parameters = {50, 100};

            // declare an object array of parameter names

            string[] parameterNames = {"val1","val2"};

            object state; 

            // Call BindToMethod of Default Binder passing MethodInfo array of all public methods of Calculator

            // class, parameter values, parameter names. Parameter Modifiers and culture info is passed as null

            // BindToMethod returns the selectd method from the MethodInfo array passed as input parameter

            MethodBase selectedMethod = defaulttBinder.BindToMethod(BindingFlags.Instance, calculatorMetods,

                ref parameters, null, null, parameterNames, out state);

            // Display the parameter name and type of the returned method

            foreach (var parameter in selectedMethod.GetParameters())

            {

                Console.WriteLine("Parameter Name: {0}, Type: {1}", parameter.Name, parameter.ParameterType);

            }

            Console.ReadLine();

 Here’s the output. As expected the Binder binds it to the Add overload taking two integers and returning integer.

 

 Reflection APIs use the Default Binder internally.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s