Important alert: (current site time 11/23/2014 9:00:38 AM EDT)
 

VB icon

Late Binding to .NET objects

Email
Submitted on: 3/27/2007 4:37:23 AM
By: Igor Krupitsky  
Level: Intermediate
User Rating: By 1 Users
Compatibility: VB.NET
Views: 4457
author picture
(About the author)
 
     This example shows how to do late binding in .NET. The provided function LoadClass() is similar to CreateObject() function in that you can reference and use external libraries at run time.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Late Binding to .NET objects
// Description:This example shows how to do late binding in .NET. The provided function LoadClass() is similar to CreateObject() function in that you can reference and use external libraries at run time.
// By: Igor Krupitsky
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=5609&lngWId=10//for details.//**************************************

'Example of use
Dim oViewer As Object = LoadClass("CrystalDecisions.Web", "CrystalDecisions.Web.CrystalReportViewer")
Function LoadClass(ByVal sAssemblyName As String, ByVal sClassName As String) As Object
	'Get Assembly
	Dim oAssembly As Reflection.Assembly
	Try
		oAssembly = Reflection.[Assembly].LoadWithPartialName(sAssemblyName)
	Catch
		Throw New ArgumentException("Can't load assembly " + sAssemblyName)
	End Try
	Dim iVersion As Integer = oAssembly.GetName().Version.Major
	'Get Class
	Dim oType As Type = oAssembly.GetType(sClassName, False, False)
	If oType Is Nothing Then
		Throw New ArgumentException("Can't load type " + sClassName)
	End If
	'Instantiate
	Dim oTypes(-1) As Type
	Dim oInfo As Reflection.ConstructorInfo = oType.GetConstructor(oTypes)
	Dim oRetObj As Object = oInfo.Invoke(Nothing)
	If oRetObj Is Nothing Then
		Throw New ArgumentException("Can't instantiate type " + sClassName)
	End If
	Return oRetObj
End Function


Other 24 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/28/2007 5:54:22 AMHoyaSaxa93

Does this work if the desired target assembly is not referenced by the project and not in the GAC?
(If this comment was disrespectful, please report it.)

 
3/28/2007 4:23:43 PMIgor Krupitsky

You can find the documentation on Assembly.LoadWithPartialName function here:

http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.loadwit hpartialname.aspx

...Loads
an assembly from the application directory or from the global assembly cache using a partial name.
(If this comment was disrespectful, please report it.)

 
3/28/2007 6:15:05 PMSteffen Roßberg

pretty nice. excellent...
(If this comment was disrespectful, please report it.)

 
3/30/2007 10:10:43 AMHoyaSaxa93

I took a look at:

http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.loadwithp artialname
.aspx

it said that the Assembly.LoadWithPartialName method is now obsolete...
(If this comment was disrespectful, please report it.)

 
7/2/2010 7:36:52 AMzoran.horvat

Hi,

Please refer to my article at address: http://www.sysexpand.com/net/reflection/latebinding/man/intro.html

This article contains workable code on .NET late binding and invocation, which is in line with .NET Framework 2.0 - 4.0.

This site also contains a small library which wraps late binding facilities in a couple of utility classes, which saves you from digging inside the .NET Framework.

I hope this will help.

Zoran
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.