I need to add a reference to a DLL from a script task. But I do not have the ability to put the DLL in the GAC. The DLL is a 3rd party DLL and they do not want to strong name it because that creates a chain effect having to strong name all DLL's that are used in their projects that share the DLL I need to refernce.
Ok my question is.. Can i reference that DLL somehow in the script task by manually adding the reference at runtime? I found this next snippet of code on the internet, i cant get it to work though.
Public Overridable Function CreateReferenceItem(ByVal itemName As String, ByVal assemblyName As String) As IVsaReferenceItem
Debug.Assert(Not (itemName Is Nothing) AndAlso Not (itemName = String.Empty))
Me.AssertEngineItemUnique(itemName)
Dim item As IVsaReferenceItem = CType(_engine.Items.CreateItem(itemName, VsaItemType.Reference, VsaItemFlag.None), IVsaReferenceItem)
item.AssemblyName = assemblyName
Return item
End Function
Have you thought about using Assembly.LoadFrom()?
I think that would be a lot easier. You can use any assembly you have access to, and don't need to reference it.
|||OK? how do i do that? in a script task? dts.assembly.loadfrom()? What do i need to import in order to get that assembly.loadfrom? do you have a sample code snippet? Thanks!|||Look at System.Reflection namespace, MSDN has lots of documentation and samples:http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.loadfrom.aspx|||Here is a script task that uses the Assembly object from the System.Reflection namespace. It loads a plane assembly that has not been strong named or put in the GAC and calls a method on it passing it a single String as a parameter. I tested this code with real values on one of my assemblies first. The names have been changed to protect the innocent. ;-)
It's a lot of code however.
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Reflection
Imports System.Windows.Forms
Public Class ScriptMain
Public Sub Main()
'
' Add your code here
'
Dim myAssembly As Assembly
'load the assembly
myAssembly = Assembly.LoadFrom("C:\Path\To\The\Assembly.dll")
'get a reference to the type
Dim t As Type = myAssembly.GetType("Namespace.Name.Of.Class")
' create an instance of the object
Dim obj As Object = Activator.CreateInstance(t, True)
' create an array Type objects for the parameters
' This method only accepts one parameter that is a string
Dim argtype(0) As Type
argtype(0) = "".GetType()
'get the method
Dim mi As MethodInfo = t.GetMethod("MethodName", argtype)
'similar to above. No specifying an array of the
' parameter values
Dim params(0) As Object
params(0) = "my parameter value"
'invoke the method and get the results
Dim results As Object = mi.Invoke(obj, params)
' Display the results as a string
MessageBox.Show(results.ToString())
Dts.TaskResult = Dts.Results.Success
End Sub
End Class|||Thanks that is just what I am looking for. Very helpful I will try this out. Thanks All!
No comments:
Post a Comment