Results 1 to 11 of 11

Thread: [2005] adding / importing references at runtime?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    [2005] adding / importing references at runtime?

    hi all,

    is this possible?

    i plan to create a MDI Form for the executable. then use classes for forms. can i reference this classes at runtime?

    thanks

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2005] adding / importing references at runtime?

    Are you trying to do some sort of plug-in architecture?

    You can do what you are asking, I am just trying to understand why you are asking for it.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: [2005] adding / importing references at runtime?

    Quote Originally Posted by Negative0
    Are you trying to do some sort of plug-in architecture?

    You can do what you are asking, I am just trying to understand why you are asking for it.
    hi,

    forgive me but i am not familiar with the plug-in architecture term.

    but what i'm trying to do is that for example, i already created the MDI Form which serves as the Main Form. i built this as an exe file. so basically this will be the file to run the application. this mdi form will have a menu that will be created at runtime as well. i will be using a database table to hold the items in the menu. then clicking a menu will basically load a child form.

    for all the child forms, i will be using classes.

    so instead of editing the whole project when adding child forms, i plan to just create a class for a form then built the .dll file. then copy it to a directory where all the applications .dll resides. then add a record to the table for this particular form.

    thanks.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2005] adding / importing references at runtime?

    So you want to load a DLL on the fly and use classes from it?

    If so, I have a plug-in example on my website that shows you how to create a plugin architecture in a program. It is in C# and it focuses on a plugin that draws on an image, but you should be able to take that and extrapolate some of the information that you need:

    http://johnkoerner.net/index.php?/ar...ture-in-C.html

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: [2005] adding / importing references at runtime?

    i'll check it out.

    thanks.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: [2005] adding / importing references at runtime?

    hi,

    i've been trying to follow the video provided on the link. at first i thought it would be easy to convert C# to VB.Net but it seems like its harder than i thought. so after spending some time trying, i'm wanting to give up doing this in C#.Net.

    my apologies as i'm not able to follow the instructions on the video. i'm having a hard-time understanding the codes well since i'm a VB.Net programmer and C#.Net is totally alien to me.

    do you have a vb.net sample?

    thanks.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  7. #7
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2005] adding / importing references at runtime?

    The basic steps are the same, don't worry so much about how it is coded in my example:

    1.) Create an interface.
    2.) Implement that interface in your plugin (or your form in your case)
    3.) In your main app, loop through all DLLs in a specific folder and if they implement your interface, add them to a list for use later.
    4.) When you are ready, call the plugins method that is implemented in the interface.

    Here is a quick example I threw together:

    For the interface:
    PROJECT1 - The other two projects need a reference to this project.
    Code:
    Public Interface TestInterface
        Sub DoWork(ByVal f As Form)
    End Interface
    In my class that implements the interface:
    PROJECT2
    Code:
    Imports InterfaceTesting
    Imports System.Windows.Forms
    Public Class Class1
        Implements TestInterface
    
    
        Public Sub DoWork(ByVal f As Form) Implements InterfaceTesting.TestInterface.DoWork
            Dim myForm As New Form1
            myForm.MdiParent = f
            myForm.Show()
        End Sub
    End Class
    PROJECT3
    In my program that loads the plugins:

    Code:
    Imports System.Reflection
    Imports InterfaceTesting
    Public Class Form1
    
        Dim Plugs As List(Of TestInterface)
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For Each p As TestInterface In Plugs
                p.DoWork(Me)
            Next
        End Sub
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Me.IsMdiContainer = True
            Plugs = New List(Of TestInterface)
    
            Dim files() As String = IO.Directory.GetFiles(Application.StartupPath & "\Plugins", "*.dll")
            Dim assm As Assembly
            For Each f As String In files
                assm = Assembly.LoadFile(f)
                For Each t As Type In assm.GetTypes()
                    If Not t.GetInterface("TestInterface") Is Nothing Then
                        Dim plugin As TestInterface = DirectCast(Activator.CreateInstance(t), TestInterface)
                        Plugs.Add(plugin)
                    End If
                Next
            Next
        End Sub
    End Class

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: [2005] adding / importing references at runtime?

    thanks for the detailed sample... found it really helpful. i'm able to load forms now using this.

    so basically as long as i load the form, each and every event in the form will work like when invoked the usual way?

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  9. #9
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2005] adding / importing references at runtime?

    Yes, just remember everything that is being done is in the class, not your main application, so if you need to communicate between forms, you are going to have to do a little bit more work.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: [2005] adding / importing references at runtime?

    thanks...

    i was just thinking that if two of my forms needed to communicate, maybe i should put both forms in one class instead as long as the forms are related to each other like a master-detail form? or is there any better way?

    since i think there will be situations that forms will need to communicate.

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    403

    Re: [2005] adding / importing references at runtime?

    hi again,

    i'm having the need to have my forms (not in same class) to communicate with each other. unfortunately i don't know how i can accomplish this.

    can someone help me?

    thanks

    VB Version: Microsoft Visual Studio 2008 Professional Edition
    .NET Version: Microsoft .NET Framework Version 3.5
    OS: Windows XP SP3

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