我创建了一个由3个项目组成的简单测试解决方案:
-
第一个项目是一个包含ITest接口的类库,其方法为void DoSomething()
-
第二个项目也是一个类库,包含一个实现ITest的类Test,在DoSomething中,它只是一个Console.WriteLine
-
第三个项目是一个Forms应用程序,它使用动态程序集加载通过ITest接口加载和实例化Test类 .
这是源代码:
public interface ITest
{
void DoSomething();
}
----------------------------------------------
public class Test : ITest
{
public void DoSomething()
{
Console.WriteLine("I've done something...");
}
}
----------------------------------------------
static class Program
{
[STAThread]
static void Main()
{
String[] files = Directory.GetFiles(Directory.GetCurrentDirectory(), "LibraryTest.dll");
Assembly assembly = Assembly.LoadFile(files[0]);
Type typeToStart = null;
foreach (Type t in assembly.GetTypes())
{
if (t.GetInterfaces().Contains(typeof(ITest)))
{
typeToStart = t;
}
}
ITest test = (ITest)Activator.CreateInstance(typeToStart);
test.DoSomething();
Console.WriteLine("Finished");
}
}
在带有Test类的LibraryProject中,我引用了表单应用程序项目,在Debug下的项目属性中,我选择了“启动外部程序”从库调试文件夹启动表单应用程序:
正如所料,我现在可以运行库项目了 . 这将从应用程序文件夹中将表单应用程序作为外部程序启动 . 在那里,表单找到库dll和dynamicall加载它并在我的Test类上执行DoSomething方法 .
但是,这是我的问题/我的问题,我在Visual Studio中没有得到任何控制台输出 . 当我运行库项目但控制台输出永远不会显示时,我可以完美地调试表单应用程序 . 当我使用控制台应用程序而不是表单应用程序时,打开一个外部cmd,我可以在那里看到输出,但我需要输出也可以使用表单,它必须在Visual Studios输出窗口中 .
你知道我怎么看不到输出吗?我想出一个获得输出的方法是使用Trace.WriteLine而不是Console.WriteLine,但我没有低估,为什么跟踪工作而控制台没有 . 任何帮助表示赞赏 .
1 回答
当您使用表单时,它将禁用控制台窗口 . 如果要查看输出,则需要手动启用它 .
How do I show a console output/window in a forms application?