C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

How to load Assemblies dynamically using Reflection


Posted Date: 03 Aug 2004    Resource Type: Articles    Category: .NET Framework
Author: Muralidharan RamakrishnanMember Level: Gold    
Rating: 1 out of 5Points: 10



Introduction

One of the coolest things about .net is "Reflection". Reflection allows you to load assemblies on the fly and treat them as if they were part of your application.

Typically this can be used to extend your application after it is released to customers. For instance we use this feature to add additional reports to customers other than the standard reports that ship with the application, with out having to alter any base app code and with out having to perform any patches. Each of our report file is a .net class, the standard reports are compiled with in the Main assembly and if the customer requests for different sets of reports, we write those class files compile them to an assembly and drop in our app bin folder. Thatz all we need to do, the reflection code in the app will take care of the rest.

Look at how exactly this is possible

Now this is my CustomerReport class that is compiled in a seperate dll and copied to my apps bin directory. It has a constructor with
1 param , ReportID.


Public Class CustomerReport
public sub New(ReportID as integer)
'Do something in constructor as required by report
end sub

'Other Report Object Members
end Class



Now let us get this class loaded from the assembly. This following function is used to load external types in to my application.

It takes 3 parameters, the assembly name, report class name and the report id. Obviously even my standard reports are stored this way into the database, making both standard reports and custom reports process exactly same.


Private Function GetReportFromAssembly(ByVal ReportAssembly As String, ByVal ReportClassName As String, ByVal ReportID As Integer) As CustomerReport

' Get an assembly type to hold the loaded assembly
Dim oCurrentAssembly As [Assembly] = System.Reflection.Assembly.GetExecutingAssembly()
Dim oTemplateAssembly As [Assembly]

'Get the location of the current assembly
Dim oFileInfo As System.IO.FileInfo = New System.IO.FileInfo(oCurrentAssembly.Location)

' When you load this assembly, and initialize a class your class constructor may
' expect 'paramets, reflection has provision'to send constructor parameters as well.
' All you have to do is to declare a array of objects equivalent with number of such params
' and pass this array when creating the object. In my report class that is going to be
' initialized i have only one parameter i.e ReportID

Dim rptID(0) As Object
rptID(0) = ReportID

' Get a generic variable to hold the returned report class object
Dim oReport As Object

Dim sTemplateNameAndPath As String = mAssemblyDirectory + ReportAssembly

' Does the assembly we're looking to load actually exist
If System.IO.File.Exists(sTemplateNameAndPath) Then

' Yes it does, so attempt to load it
oTemplateAssembly = oCurrentAssembly.LoadFrom(sTemplateNameAndPath)

' Now we can extract the known public class from it in the form of Namespace.ClassName
' And create an instance of it, once this line is executed the constructor of our
' CustomerReport class is called istantly because you are instantatiating the object

oReport = Activator.CreateInstance(oTemplateAssembly.GetType(ReportClassName, True), rptID)


Else

' Just make sure it really is set to Nothing
oReport = Nothing
Throw New Exception("The Specified Report Assembly does not exist in the application directory")

End If

' Return our resulting report. I am doing a ctype to my class type because we
' have 'Option Explicit ON' for our projects
Return CType(oReport, CustomerReport)
End Function


Conclusion

Thus, when properly designed & used, Reflection will help you to extend your application even after it is past the release stage. Hope it helps.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to restrict a program to a single instance
Previous Resource: Resizing Images Dynamically
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use