Solved

Curious about Microsoft.CSharp.RuntimeBinder

Posted on 2010-11-30
2
660 Views
Last Modified: 2012-06-27
I am curious to learn a little about Microsoft.CSharp.RuntimeBinder.  What can canyone tell me about it?

Thanks,
newbieweb
0
Comment
Question by:newbieweb
2 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 34244454
Looks to me like it's an internal part of the .Net Framework (i.e. not intended to be used by you) that provides the mechanisms that make late-binding in C# possible. http://msdn.microsoft.com/en-us/library/microsoft.csharp.runtimebinder.aspx

In Visual Basic it was always easy to use COM objects, because that language supported late-binding.  Meaning that you could write Dim obj = CreateObject("SomeActiveX.Control"), and even though the compiler had no idea what methods/properties this object supported it would let you write things like obj.SomeMethod().  It was only later, at run-time, that this object was inspected to see if it even had a method with that name, and if so it was run, and if not you got a run-time error.  This is late-binding.

In the past C# did not have this feature.  If you tried to write obj.SomeMethod() the compiler would immediately complain "obj" has no such method - your project wouldn't even compile, much less run.  To easily use COM objects in C# required the creation of interfaces and classes that mimic the COM object ahead of time.  Once you made a class in C# with a SomeMethod() method, you could declare an instance of that class and use it's methods.  Generally these classes/interfaces are made for you by Visual Studio when you add a COM reference, and in some cases .Net assemblies containing all the classes/interfaces needed for a given COM object might be provided to you seperately (like the Office Primary Interop Assemblies).  This is early binding.

The most recent version of C# introduces a new keyword: dynamic (http://msdn.microsoft.com/en-us/library/dd264736.aspx).  The dynamic keyword allows you to declare an object and essentially call any method or property you want - the compiler won't check to make sure you're using a method that actually exists, but when your application is running - after it's already been compiled - is when this check is done, and if you use a method name that doesn't exist you'll get a runtime exception.

Consider this code, which will not even compile (because the compiler knows Object has no such method) nor run:
using System;

class Program
{
	static void Main(string[] args)
	{
		object someDynamicObj = new object();

		someDynamicObj.ThisMethodDoesntExist();
	}
}

Open in new window


Whereas this code will compile, but if you try to run it you'll see you get an exception out of the RuntimeBinder namespace:
using System;

class Program
{
	static void Main(string[] args)
	{
		dynamic someDynamicObj = new object();

		someDynamicObj.ThisMethodDoesntExist();
	}
}

Open in new window


The point of the dynamic keyword is primarily to support late-bound COM objects, not to allow you to bypass compiler checks - and if you're not writing a compiler, you probably need not be concerned at all with Microsoft.CSharp.RuntimeBinder.
0
 

Author Closing Comment

by:newbieweb
ID: 34247848
Thanks!  Brilliant answer!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now