I know how to create some object by reflection and pass some arguments.
Dim assembly As System.Reflection.Assembly
Dim control As Object
assembly = System.Reflection.Assembly.Load("WpfControlLibrary1")
control = assembly.CreateInstance("WpfControlLibrary1.Main")
control.Maximize("true")
My question is if there is an approach to get info from "the control" into "the owner" of that "control".
So I guess shoul开发者_JAVA百科d be some way to have bidirectional interaction between the owner and created assembly.
For example within some Timer I want get the states of the "control" periodically.
foreach(...)
{
var state = control.GetState(); // ????? Is it possible ?
Sleep(10000);
}
Here we can see how to pass the parameters
So what I need is to get back some returned object up.
Thank you in advance for any useful clue my brothers and sisters in programming!
To invoke method defined on classes in another assembly, you need something like this:
Assembly assembly = Assembly.Load("OtherAssembly");
Type controlType = assembly.GetType("OtherAssembly.OtherAssemblyClass");
object control = Activator.CreateInstance(controlType);
controlType.InvokeMember("SetFullName", BindingFlags.InvokeMethod, null,
control, new object[] { "FirstName", "LastNameski" });
This will invoke the method SetFullName
of the class OtherAssemblyClass
of the assembly OtherAssembly
on the object control
, using the parameters "FirstName"
and "LastNameski"
object result = controlType.InvokeMember("GetFullName",
BindingFlags.InvokeMethod, null, control, null);
This will invoke a method called GetFullName
on that same object, which accepts no parameters (hence the last null
in the call) and returns a string.
Console.WriteLine(result.GetType().FullName);
This will print out "System.String"
Console.WriteLine(result);
This will print out "FirstName LastNameski"
.
in the example, the other assembly contains this class:
namespace OtherAssembly
{
public class OtherAssemblyClass
{
private string firstName;
private string lastName;
public string GetFullName()
{
return firstName + " " + lastName;
}
public void SetFullName(string first, string last)
{
firstName = first;
lastName = last;
}
}
}
精彩评论