Categories
VB.NET Упътвания

Вграждане на dll в assembly

Примерен код на VB.Net

1. Добавяне на Event Handler преди извикване на ресурс от dll файла

AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf AssemblyResolve

2. Функция за извикване на dll файла

Private Function AssemblyResolve(ByVal sender As Object, ByVal e As ResolveEventArgs) As Assembly
Dim resourceFullName As String = String.Format("Seo.{0}.dll", e.Name.Split(","
  c)(0))
Dim thisAssembly As Assembly = Assembly.GetExecutingAssembly()
Using resource As Stream = thisAssembly.GetManifestResourceStream(resourceFullName)
If resource IsNot Nothing Then
Return Assembly.Load(ToBytes(resource))
End If
Return Nothing
End Using
End Function

Private Function ToBytes(ByVal instance As Stream) As Byte()
Dim capacity As Integer = If(instance.CanSeek, Convert.ToInt32(instance.Length), 0)

Using result As New MemoryStream(capacity)
Dim readLength As Integer
Dim buffer(4096) As Byte

Do
readLength = instance.Read(buffer, 0, buffer.Length)
result.Write(buffer, 0, readLength)
Loop While readLength > 0

Return result.ToArray()
End Using
End Function