调用程序集的方法是通过 .NET 中其所有者的反射创建的

发布于 2024-12-03 12:19:26 字数 710 浏览 0 评论 0 原文

我知道如何通过反射创建一些对象并传递一些参数。

Dim assembly As System.Reflection.Assembly
Dim control As Object

assembly = System.Reflection.Assembly.Load("WpfControlLibrary1")
control = assembly.CreateInstance("WpfControlLibrary1.Main")

control.Maximize("true")

我的问题是是否有一种方法可以将信息从“控件”获取到该“控件”的“所有者”。

所以我想应该是在所有者和创建的程序集之间进行双向交互的某种方式。

例如,在某些计时器中,我想定期获取“控件”的状态。

foreach(...)
{

var state = control.GetState(); // ????? Is it possible ?
Sleep(10000);

}

这里我们可以看到如何传递参数

所以我需要的是取回一些返回的对象。

提前感谢您在编程方面为我的兄弟姐妹提供任何有用的线索!

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 should 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍景流年 2024-12-10 12:19:26

要调用在另一个程序集中的类上定义的方法,您需要如下所示:

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" });

这将调用程序集 OtherAssembly 的类 OtherAssemblyClass 的方法 SetFullName > 在对象 control 上,使用参数 "FirstName""LastNameski"

object result = controlType.InvokeMember("GetFullName", 
                            BindingFlags.InvokeMethod, null, control, null);

这将调用名为 GetFullName 的方法> 同样的对象,它不接受任何参数(因此调用中最后一个 null)并返回一个字符串。

Console.WriteLine(result.GetType().FullName);

这将打印出“System.String”

Console.WriteLine(result);

这将打印出“FirstName LastNameski”


在示例中,另一个程序集包含此类:

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;
    }
  }
}

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;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文