Solved

Curious about Microsoft.CSharp.RuntimeBinder

Posted on 2010-11-30
2
708 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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