Cant open multiple Modules

  • VB.NET

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von mono424.

    Cant open multiple Modules

    I'm writing a Application where i need to list multiple Modules. The Problem is that every Module i load, is the actually the first Module i have loaded.

    Function to open a Module:

    Quellcode

    1. Public Function openModule(path As String) As ctxModule.ctxModule
    2. Dim asm As Assembly = Nothing
    3. Dim myType As System.Type = Nothing
    4. Dim implementsIPlugin As Boolean = Nothing
    5. Try
    6. asm = Assembly.LoadFrom(path)
    7. Dim fullClassName As String = "Main"
    8. myType = asm.GetType(fullClassName)
    9. implementsIPlugin = GetType(ctxModule.ctxModule).IsAssignableFrom(myType)
    10. If implementsIPlugin Then
    11. Return Activator.CreateInstance(myType)
    12. End If
    13. Catch ex As Exception
    14. End Try
    15. Return Nothing
    16. End Function


    The code which calls the function:

    Quellcode

    1. Public Sub load_modules()
    2. modules.Clear()
    3. modulePaths.Clear()
    4. For Each F As String In My.Computer.FileSystem.GetFiles(path_modules)
    5. If F.EndsWith(".03MDL") Then
    6. Dim m As ctxModule.ctxModule = Nothing
    7. m = openModule(F)
    8. If IsNothing(m) = False Then
    9. modules.Add(m)
    10. modulePaths.Add(m, F)
    11. Else : End If
    12. m = Nothing
    13. Else : End If
    14. Next
    15. End Sub


    Every Module inthe List "modules" is the the First Module. So the list looks like this:

    Module 1
    Module 1
    Module 1
    But i should look like this:

    Module 1
    Module 2
    Module 3
    Hope someone can help :S
    Are You able to create a simple solution which reproduces Your effect?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Hi
    you should set Option Strict to On. Activator.CreateInstance creates an object that needs a cast to ensure that the return value is of type ctxModule.ctxModule.

    As for the issue I can't track it just by looking at the code you have posted. If I have not missed some minor detail (usually causing such behaviour ;) ), it should actually work the way you want it to. Are there three different assemblies having three different Main classes? And how can you distinguish Module 1, etc.? Do these modules provide a getter for the module name? Is this getter or property correct?

    Some further remarks:
    The Else statement is not required if you do not execute any code in it. Just leave it out instead of writing Else : End If.
    Instead of IsNothing(m) = False you can write m IsNot Nothing. The other way is possible but obsolete and bad coding style, respectively.
    Instead of using the My-Namespace it is more common to use System.IO, etc. The My-Namespace, in general, wraps some stuff of the framework and brings it into a non-consistent way regarding the framework functionality (it actually is not a part of the framework but does not follow the same coding guidelines). My.Settings and My.Resources are excluded from this.

    Kind regards
    ~blaze~
    @ThePlexian
    The last one is Module 3 :)

    @RodFromGermany
    I dont really understand what you want? :)

    @~blaze~
    I will set it tomorrow :). Yeah there are 3 different Assemblies with three different Main Classes. If i delete Module1 out of the folder the list displays Module2, Module2. All three Modules have a Name Property so i can indentify them. All should be correct because, as i mentioned before, when i delete the first module, it makes a list with module2´s. Do dont really see anything wronge on my code :S
    Thanks for your Tipps :)
    If i understand right, there are in each file more than one module you want to get to your list.

    But your search-algorithm only searches for one module in each file.

    If you want to find more than one module in a file, you must change the openModule()-Function, that it can return multiple modules.
    The signature should look like:

    VB.NET-Quellcode

    1. Public Function openModule(path As String) As List(of ctxModule.ctxModule)
    and the function should contain a loop.

    Chainging the function that way will also requiere changes in the load_modules() function in order to get them fit and work together.

    moreover the name "openModule()" is no longer appropriate, since it does not load a module but multiple modules in each File.
    I suggest to name it GetModules() since that is thats functions job (on programming very important: Nomen est Omen!)

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „ErfinderDesRades“ ()

    mono424 schrieb:

    what you want?
    Not explain by words but explain by a really excample solution.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    @ErfinderDesRades
    Thank you for your advanced answer and sorry that i wasnt clear. No, every File has one Main Class but the main classes of the files are not duplicates. So one Module a File. I name everthing good i think ty for your advise :)

    @RodFromGermany
    For each loops through Folder with 3 Files. Each File has a different Module. But only the first Module could be loaded as it seems because after the loop the output looks like: module1 from file1, module1 from file1, module1 from file1.

    Its like i would call the open function 3 times witg file 1 but i call it with all 3 files and it everytime gives me the information of file1 back..

    dont know if you undrstand now ? :)
    @mono424 Please clean and zip Your solution, use the forum's file append function:
    "Erweiterte Antwort" => "Dateianhänge" "Hochladen"
    and append Your zipped solution to Your post.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Could you enumerate the assembly names of the module assemblies? Maybe it is equal therefore assumed to describe equal content.

    Edit: By the way: Consider using Assembly.LoadFile instead of Assembly.LoadFrom. If assembly identities are equal, LoadFrom returns the first assembly with the specified identity and thus skips loading the actual file containing it.

    Kind regards
    ~blaze~

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „~blaze~“ ()

    ~blaze~ schrieb:

    Could you enumerate the assembly names of the module assemblies? Maybe it is equal therefore assumed to describe equal content.

    Edit: By the way: Consider using Assembly.LoadFile instead of Assembly.LoadFrom. If assembly identities are equal, LoadFrom returns the first assembly with the specified identity and thus skips loading the actual file containing it.

    Kind regards
    ~blaze~


    Thats it !! Thank you so much for your help, now working like charm :))

    Thanks to everbody who tryed to help me ! :)