简体   繁体   中英

Implementing an interface then calling the Initialize() function

this wont be easy to explain clearly but here goes...

i have something that works fine in VB but not in C#.

i have 2 classes, each class implements a different interface. each interface exposes an Initialize() function that should get called automatically when the plugin loads. since each class has an Initialize() function, i can watch both functions get called in the log. this works fine in VB.

when i do this same thing in C#, only the Initialize() in the first class gets called. anyone have a clue as to why this might be happening?

vb code:

Public Class class1
    Implements Interface1

    Public Sub Initialize() Implements Interface1.Initialize
        msgbox("initialize from class1")
    End Sub
End Class

Public Class class2
    Implements Interface2

    Public Sub Initialize() Implements Interface2.Initialize
        msgbox("initialize from class2")
    End Sub
End Class

C# code:

public class class1 : interface1
{
    public void Initialize()
    {
        messagebox.show("initialize from class1");
    }
}

public class class2 : interface2
{
    public void Initialize()
    {
        messagebox.show("initialize from class2");
    }
}

as you can see the code is identical in both languages. why does it work in VB and not in C#?

Is the constructor of both classes called? Or is only the fist class initialized?
Please try adding a (static) constructor to check which classes are initialzed.

Static Constructor for one Class:

public class class2 : interface2
{
    static class2(){
        messagebox.show("Static Constructor from class2");
    }

    public void Initialize()
    {
        messagebox.show("initialize from class2");
    }
}

Thanks everyone for your help! It turns out the problem was in the calling code. Long story short, each plugin has a command ID as part of the interface. In this case both have ID 1000. When the calling code loops through plugins to load, if it sees a duplicate ID it skips that plugin. My problem was that I was loading other plugins at the same time with the same ID. You all helped find a bug in the calling code (which is pretty well-known software) so thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM