Search code examples
c#vb.netdynamic-class-creation

Invoke dynamic object's method


How can I invoke/call an object's method from another object method when both objects are created dynamically?

The situation is as follows:

I have two objects created dynamically each object correspond to a different class

objA
   Method1A()
objB
   Method1B()

I want to call objA's method1A() from objB's method1B().

How can I accomplish that / what approach do you recommended ?


Solution

  • In order to invoke an instance method on some type you need an instance of that type. So if Method1B is supposed to invoke an instance method on objA you could pass this instance as parameter to the method:

    public void Method1B(ObjA objA)
    {
        objA.Method1A();
    }