Tuesday, April 15, 2008

Loading Assembly at runtime

Reflection is a wonderful feature to get any information about a class at the runtime. Following are the different ways of loading an assembly which I would like to share with everyone.

VB.Net
Imports System.Reflection.Assembly

Public ReadOnly property MyAssembly as Assembly
Get
dim assy as assembly = nothing

dim assypath as string = "C:\yourfilepath.dll"

dim assyfullname as string = "MyAssemblyName,Version = 1.0.0.0" + _
"Culture = neutral,PublicKeyToken = ba0014rg1234809"

dim assypartialname as string = "MyAssemblyPartialName"

'If you know the path from where to load the assembly
assy = LoadFile(assypath)
'If you want to load the executing assembly
assy = GetExecutingAssembly()
'If you want to load the assembly that started the process
assy = GetEntryAssembly()
'If you want to load the assembly from one level up in the stack
assy = GetCallingAssembly()
'If you want to load the assembly from GAC,however in .Net 2.0 this method has become obsolete but you can still use it.
assy = LoadWithPartialName(assypartialname)
'If you want to load the assembly only for interoggation but not for creating instance
assy = ReflectionOnlyLoad(assyfullname)
Or
assy = ReflectionOnlyLoadFrom(assypath)
return assy
End Get

I have mainly used LoadWithPartialName,GetExecutingAssembly

No comments:

 
Google