简体   繁体   English

在DLL中使用主要项目类方法

[英]Using main project classes methods within dll

I am wrking on a project that uses class library and I need my class communicate with host application. 我在使用类库的项目中工作,我需要我的类与主机应用程序进行通信。 how can I use main project classes methods? 如何使用主要的项目类方法?

I load my custom assembly using Reflection into the app domain. 我使用Reflection将我的自定义程序集加载到应用程序域中。

private void myButton(object sender, RoutedEventArgs e)
{
   MainAppNameSpace.handeling_buttons.MethodOfClass(sender as Button);
}

Update: My question is simple, how to use a class that comes later and doesn't exist in time of writing plugin class. 更新:我的问题很简单,如何使用后来出现的类,并且在编写插件类时不存在。 my approach to this demand is define classes in external libraries that can be used by main assembly and plugins both. 我满足此需求的方法是在外部库中定义类,这些类可由主程序集和插件使用。 but I'm not sure how it would be efficient and reliable in WPF environment. 但是我不确定在WPF环境中它将如何有效和可靠。

You should find your type(class) from your application domain by this code: 您应该通过以下代码从应用程序域中找到您的类型(类):

Type objectType = (from asm in AppDomain.CurrentDomain.GetAssemblies()
                        from type in asm.GetTypes()
                        where type.IsClass && type.Name == "ClassName"
                        select type).Single();

And then Invoke your desired method. 然后调用所需的方法。 that means you should know your methods name: 这意味着您应该知道您的方法名称:

  objectType.GetMethod("YourMethod").Invoke(Activator.CreateInstance(type),null);

In a case that there is parameters to pass, you could do that by using this code: 如果有要传递的参数,则可以使用以下代码来实现:

 objectType.GetMethod("YourMethod").Invoke(Activator.CreateInstance(type, "MainFunctionParameter"), new object[] { "ParameterToPass" });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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