Saturday, February 21, 2009

Load unreferenced dll at runtime

If you want to call methods in another dll which was not referenced in the current project, here is how:

Public Sub ExportXML(ByVal userID As Integer, _


ByVal oldPassword As String, _


ByVal newPassword As String)

Try


Dim isExportXML As String = WebConfigurationManager.AppSettings("IsExportXMLWhenPasswordChanged")


If isExportXML IsNot Nothing AndAlso isExportXML.ToLower() = "yes" Then


Dim ht As New Hashtable


Dim binPath As String = Path.Combine(HttpContext.Current.Server.MapPath("~"), "bin\Test.Common.dll")


Dim myDllAssembly As Assembly = Assembly.LoadFile(binPath)


Dim o As Object = myDllAssembly.CreateInstance("Test.Common.ExportXML")


Dim objUser As New clsUser(mstrConn)


objUser.getData(userID)


Dim logon As String = objUser.strLogon


Dim argstopass As Object() = New Object() {ht, "AgentID", logon}


o.GetType.GetMethod("InsertHash").Invoke(o, argstopass)


argstopass = New Object() {ht, "OldWebPassWord", oldPassword}


o.GetType.GetMethod("InsertHash").Invoke(o, argstopass)


argstopass = New Object() {ht, "NewWebPassWord", newPassword}


o.GetType.GetMethod("InsertHash").Invoke(o, argstopass)


Dim argstopass1 As Object() = New Object() {ht, 7}


o.GetType.GetMethod("ExportUsingHash").Invoke(o, argstopass1)


End If


Catch e As Exception


clsLog.log(e.ToString)


Throw e


End Try


Imports System.IO

Imports System.Reflection


Public Class ExportXML


Public Sub New()


End Sub


Public Sub ExportUsingHash(ByVal ht As Hashtable, ByVal exportType As ExportType)


Try


If ht.Count <= 0 Then


Return


End If


'header open


Dim sb As New StringBuilder()


sb.Append("<DataRoot>")


sb.Append("<TestNode>")


'loop content


For Each item As DictionaryEntry In ht


sb.AppendFormat("<{0}>{1}</{2}>", item.Key, item.Value, item.Key)


Next


sb.Append("</TestNode>")


sb.Append("</DataRoot>")


'output xml


Dim logFilePath As String = _


String.Format("{0}\Integration\{1}{2}({3}).xml", _


HttpContext.Current.Server.MapPath("~"), _


exportType.ToString(), _


String.Format("{0:ddMMyyyyHHmmss}", DateTime.Now), _


System.Guid.NewGuid().ToString())


Using sw As New StreamWriter(logFilePath, True)


sw.WriteLine("<?xml version=""1.0"" ?>")


sw.Write(sb.ToString())


sw.Close()


End Using


Catch ex As Exception


Throw ex


End Try


End Sub


'Compare object changes


Public Sub Export(ByVal obj_Before As Object, _


ByVal obj_After As Object, _


ByVal exportType As ExportType, _


ByVal actionMode As ActionMode)


Try


'header open


Dim sb As New StringBuilder()


sb.Append("<DataRoot>")


sb.Append("<TestNode>")


'if add mode, add all to the xml


If actionMode = Common.ActionMode.Add Then


If obj_After IsNot Nothing Then


Dim t2 As Type = obj_After.GetType()


For Each p2 As PropertyInfo In t2.GetProperties()


WriteContent(p2.Name, p2.GetValue(obj_After, Nothing), exportType, sb, actionMode, False)


Next


End If


ElseIf actionMode = Common.ActionMode.Edit Then


'if edit mode, only add changed ones to xml


If obj_Before IsNot Nothing AndAlso obj_After IsNot Nothing Then


Dim t1 As Type = obj_Before.GetType()


Dim t2 As Type = obj_After.GetType()


For Each p1 As PropertyInfo In t1.GetProperties()


For Each p2 As PropertyInfo In t2.GetProperties()


If p1.Name = p2.Name Then


Dim IsEqual As Boolean = False


IsEqual = p1.GetValue(obj_Before, Nothing).Equals(p2.GetValue(obj_After, Nothing))


WriteContent(p2.Name, p2.GetValue(obj_After, Nothing), exportType, sb, actionMode, IsEqual)


Exit For


End If


Next


Next


End If


ElseIf actionMode = Common.ActionMode.Delete Then


If obj_Before IsNot Nothing Then


Dim t1 As Type = obj_Before.GetType()


For Each p1 As PropertyInfo In t1.GetProperties()


WriteContent(p1.Name, p1.GetValue(obj_Before, Nothing), exportType, sb, actionMode, False)


Next


End If


End If


sb.Append("</TestNode>")


sb.Append("</DataRoot>")


'output xml


Dim logFilePath As String = _


String.Format("{0}\Integration\{1}{2}({3}).xml", _


HttpContext.Current.Server.MapPath("~"), _


exportType.ToString(), _


String.Format("{0:ddMMyyyyHHmmss}", DateTime.Now), _


System.Guid.NewGuid().ToString())


Using sw As New StreamWriter(logFilePath, True)


sw.WriteLine("<?xml version=""1.0"" ?>")


sw.Write(sb.ToString())


sw.Close()


End Using


Catch ex As Exception


Throw ex


End Try


End Sub


Public Sub InsertHash(ByVal ht As Hashtable, _


ByVal key As String, _


ByVal val As Object)


If Not IsDBNull(key) AndAlso key IsNot Nothing Then


ht(key) = val.ToString()


End If


End Sub


End Class


Reference:

Using Reflection to load unreferenced assemblies at runtime in C#

blog comments powered by Disqus