Solved

Curious about Microsoft.CSharp.RuntimeBinder

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

777 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