简体   繁体   English

C#:MethodInfo.Invoke一个方法,需要用户输入才能进行处理

[英]C#: MethodInfo.Invoke a method that needs input from user to process

I have a DLL file that has a method inside it which creates a FolderBrowserDialog and waits for user selection to proceed. 我有一个DLL文件,其中包含一个创建了FolderBrowserDialog并等待用户选择继续的方法。 Here is its code: 这是它的代码:

        public void setRoot() {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        if (fbd.ShowDialog() == DialogResult.OK)
        {
            root = fbd.SelectedPath;
            searchRoot();
        }
    }

However, on another program I am trying to invoke this method and I get the program waiting forever. 但是,在另一个程序上,我试图调用此方法,并且使程序永远等待。 My guess is that the invoking program is waiting for the FileBrowserDialog to select a file. 我的猜测是调用程序正在等待FileBrowserDialog选择文件。 This is the output: MethodInfo.Invoke(class object, parameters) How can I get past that and make the invoking program select the directory for that invoked method? 这是输出: MethodInfo.Invoke(class object,parameters)如何克服这个问题,并使调用程序选择该调用方法的目录?

A method called with MethodInfo.Invoke is not different than one called directly from code. MethodInfo.Invoke调用的方法与直接从代码调用的方法没有什么不同。 It's the same code that's being run, will still run on the same thread, with the same permissions as a direct call. 它与正在运行的代码相同,仍将在同一线程上运行,并且具有与直接调用相同的权限。 If your Reflection-invoked method hangs while working fine without reflection, there's something else that's causing it. 如果您的反射调用方法在正常工作而没有反射时挂起,则可能是由其他原因引起的。

It looks, from your screenshot, that you're running it in a Console application. 从屏幕截图中可以看出,您正在控制台应用程序中运行它。 I'm guessing the other app, isn't a console app, which means it has a running message pump waiting for Windows messages, making the dialog work. 我猜想另一个应用程序不是控制台应用程序,这意味着它有一个正在运行的消息泵,正在等待Windows消息,从而使对话框正常工作。 Running Windows dialogs in a console application won't work so easily. 在控制台应用程序中运行Windows对话框不太容易。

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

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