Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Curious about Microsoft.CSharp.RuntimeBinder

Posted on 2010-11-30
2
Medium Priority
?
711 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 2000 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

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

688 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