Results 1 to 1 of 1

Thread: [.NET 3.5+] DllResourceImportAttribute - Embedding Pre-Compiled Dlls Into Application

  1. #1

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    [.NET 3.5+] DllResourceImportAttribute - Embedding Pre-Compiled Dlls Into Application

    This was a quick idea I came up with for embedding dll's into an application's resources. One thing I've seen people wanting to do is embed a dll into their application as a resource and then use the said methods from that resource.

    I wanted my approach to be as similar as possible to the DllImportAttribute attribute. In the attached code you will find my version called the DllResourceImportAttribute.

    Let's assume there is a library that looks like this, with a base namespace of ClassLibrary1:

    ClassLibrary1:
    Code:
    Public Class Class1
    
        Public Shared Function GetValue(ByVal state As Boolean) As String
            Return "TEST"
        End Function
    
        Public Function GetState() As Boolean
            Return False
        End Function
    
    End Class
    
    Public Class Class2
    
        Public Function PerformActionAndReturn() As Boolean
            '//perform actions
            Return True
        End Function
    
    End Class
    You would have the following types from this library:
    • ClassLibrary1.Class1
    • ClassLibrary1.Class2

    Now, you would compile this project and add the dll into your other project's resources. You can then create a class inside your other project similar to this:

    WindowsApplication2:
    Code:
    Public Class Class1
    
        <DllResourceImport("ClassLibrary1.dll")> _
        Public Shared Function GetValue(ByVal state As Boolean) As String
    
            '//will search for ClassLibrary1.Class1.GetValue(Boolean) method
            Return CStr(DllResourceImportAttribute.Execute(state))
        End Function
    
        <DllResourceImport("ClassLibrary1.dll")> _
        Public Function GetState() As Boolean
    
            '//will create instance of Class1 and 
            '//search for ClassLibrary1.Class1.GetState() method
            Return CBool(DllResourceImportAttribute.Execute(Nothing))
        End Function
    
        <DllResourceImport("ClassLibrary1.dll", _
                           DllResourceImportAttribute.DefaultBindingFlags, _
                           Nothing, "ClassLibrary1.Class2")> _
        Public Function PerformActionAndReturn() As Boolean
    
            '//will create instance of ClassLibrary1.Class2 and
            '//search for ClassLibrary1.Class2.PerformActionAndReturn()
            '//in the ClassLibrary1.dll resource
            Return CBool(DllResourceImportAttribute.Execute(Nothing))
        End Function
    
    End Class
    Some of the overloads of the constructors allow the ability for you to basically group all the functions you want to use in one class. You can see that from the third function which is actually located in the ClassLibrary.Class2 type, but resides in the Class1 type in the WindowsApplication2 project. The default implementation of the attribute if you only specify the library name is to search the resource using the same structure that you've built in code and using the name of the library you specified as the root namespace.

    Anyway, it was just a quick idea I made, if there are any problems let me know, or if you use it let me know as I'd be interested. Screenshot sort of gives a visual as to what I am talking about.
    Attached Images Attached Images  
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width