我正在制作一个使用SharpPcap分析网络流量的C# DLL。其中一个我想要实现的功能是可切换的控制台输出。我的想法是在我的类中拥有一个方法
public void ConsoleOutputOn(bool outputOn)
如果需要控制台输出,该参数传入true;如果不需要,传入false。我不知道如何实现这个功能。
LUA中可这样编写:
local dprint2 = function() end
function consoleOutput(outputOn)
if (outputON == true) then
dprint2 = function(...)
print(...)
end
else
dprint2 = function() end
end
end
如果调用consoleOutput(true),则dprint2将变为print,每次调用dprint2时,输入参数将传递给print并打印在控制台输出上。如果调用consoleOutput(false),那么dprint2将是一个空函数,什么也不会做。
我尝试在C#中做同样的事情,我的类将有一个私有变量“consoleOn”,而不是print,我将调用。
public void ConsoleOuptput(...) {
if(outputOn) {
Console.WriteLine(...);
}
}
需要检查“consoleOn”是否为真,若是,则将参数发送到Console.WriteLine()。
问题在于Console.WriteLine()有19种不同类型的输入参数。是否有一种方法可以编写“如果consoleOn传递所有参数给Console.WriteLine()”的代码。或者有更好的方法来制作可切换的控制台输出。
请记住我正在创建DLL。我不能完全关闭控制台。
ConsoleOutput
这样的包装方法就足够了呢? - NicoE