So I’m performing tests on a dll with the following:
--test.dll--
namespace MyNamespace
{
internal class foo
{
private static bar myBarClass = new bar();
]
internal class bar
{
public void aMethod();
}
}
----
Now in another program I have an existing instance of foo (obtained via reflection). Now I want to call the method aMethod() from the myBarClass within the existing instance of foo. Not sure how to do this.
foo myFooObject = GetInstanceOfFoo(); //i get an instance of foo via reflection
Assembly testAssembly= Assembly.LoadFrom(c:\test.dll);
object o = testAssembly.CreateInstance("MyNamespace.bar");
Type t = o.GetType();
BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic;
MethodInfo mi = t.GetMethod("aMethod", bf);
mi.Invoke(<notsurewhatgoeshere!>, null);
I am able to get the correct method info. My question is in mi.Invoke, how to I invoke the method on the already existing instance of foo.bar?
Thanks
Note that you do not need to construct your own instance (as you already are) because you won’t be using it anyway.
So the complete code you would use is: