Solved

Curious about Microsoft.CSharp.RuntimeBinder

Posted on 2010-11-30
2
697 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

735 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