Tag: 参数

正确的方法来实现C#控制台应用程序?

将命令行工具实现和构建为C#控制台应用程序的正确方法是什么? 需要解决的问题包括正确解析命令行变量以及输出文本的正确方法。 虽然Console.WriteLine()是输出最明显的选择,但是应该选择写入标准错误流,.Error,.SetErrorStream等的情况是什么? 在将正确的返回代码返回给调用命令时,应用程序退出的正确方法是什么? 如何实现CancelKeyPress事件来中断程序? 它是否仅在单独线程上发生异步操作时使用? 是否有一个简明的C#命令行工具编程指南,或者更好的开源项目或模板,我可以使用它来正确实现一个相对简单的工具?

‘XDevkit.IXboxDebugTarget.GetMemory(uint,uint,byte ,out uint)’的最佳重载方法匹配有一些无效的参数

错误可以在代码下面找到! 基本代码: XDevkit.IXboxDebugTarget.GetMemory(uint, uint, byte[], out uint) 我有什么新的 : uint num1; uint num2; uint num4; num1 = Convert.ToUInt32(textBox2.Text); num2 = Convert.ToUInt32(textBox3.Text); num4 = Convert.ToUInt32(textBox5.Text); byte[] num3; num3 = BitConverter.GetBytes(Convert.ToInt32(textBox3.Text)); IXboxManager xbm = new XboxManager(); IXboxConsole xbc = xbm.OpenConsole(“textBox1.Text”); //Or Console Name in “” IXboxDebugTarget xdt = xbc.DebugTarget; xdt.ConnectAsDebugger(“XeDevMemPatcher”, XboxDebugConnectFlags.Force); // this isn’t always needed IXboxDebugTarget.GetMemory(num1, […]

将命令行参数从C#传递到外部exe

我有类似的问题已在这里解决了 。 但我无法弄清楚,问题是如何解决的。 我有一个程序,可以定义一个输入和输出文件。 从Commend行运行,一切正常: D:\Tools\siftDemoV4>siftWin32.exe -display result.pbm 但是通过System.Diagnostics.Process运行不起作用。 我收到错误“无效的命令行参数:<”,然后发生System.InvalidOperationException。 var process = new Process() { StartInfo = { Arguments = string.Format(@”-display {1}”, configuration.Source, configuration.Destination), FileName = configuration.PathToExternalSift, RedirectStandardError = true, RedirectStandardInput = true, RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true, ErrorDialog = false, } }; process.EnableRaisingEvents = true; process.Exited += OnProcessExited; process.OutputDataReceived […]

我能否检测到我是否已将新对象作为参数?

精简版 对于那些没有时间阅读我对以下问题的推理的人: 有没有办法为方法的参数强制实施“仅新对象”或“仅现有对象”的策略 ? 长版 有很多方法将对象作为参数,并且方法是否具有对象“all to their”并不重要。 例如: var people = new List(); Person bob = new Person(“Bob”); people.Add(bob); people.Add(new Person(“Larry”)); 这里List.Add方法采用了“现有” Person (Bob)以及“new” Person (Larry),该列表包含两个项目。 Bob可以作为bob或people[0] 。 拉里可以作为people[1]访问,如果需要,可以随后缓存和访问larry (或其他)。 好的。 但有时一个方法确实不应该传递给新对象。 以Array.Sort为例。 以下内容并没有多大意义: Array.Sort(new int[] {5, 6, 3, 7, 2, 1}); 所有上面的代码都是采用一个新数组,对它进行排序,然后忘记它(因为它的引用计数在Array.Sort退出后达到零,因此排序的数组将被垃圾收集,如果我没有记错的话) 。 因此Array.Sort 期望 “现有”数组作为其参数。 可以想象其他方法可能期望 “新”对象(尽管我通常认为这样的期望会是设计错误)。 一个不完美的例子是: DataTable firstTable = myDataSet.Tables[“FirstTable”]; […]

C#参数至少有一个值

如何使用至少有一个值的参数参数? public void Foo(params string[] s) { } public void main() { this.Foo(); // compile error this.Foo(new string[0]); // compile error this.Foo({ }); // compile error this.Foo(“foo”); // no error this.Foo(“foo1”, “foo2”); // no error }

如何在没有编译时常量的情况下设置可选参数

有没有办法编写下面的C#方法: public string Download(Encoding contentEncoding = null) { defaultEncoding = contentEncoding ?? Encoding.UTF8; // codes… } 添加了默认参数,使其如下所示: public string Download(Encoding contentEncoding = Encoding.UTF8) { // codes… } 不使用编译时常量?

确定参数是否在C#中使用reflection使用“params”?

考虑这个方法签名: public static void WriteLine(string input, params object[] myObjects) { // Do stuff. } 如何确定WriteLine方法的“myObjects”参数使用params关键字并且可以采用变量参数?

改进我传递调试命令行参数的方式

我是我的控制台应用程序我传递这样的参数: #Region ” DEBUG CommandLine Arguments ” Private Function Set_CommandLine_Arguments() As List(Of String) #If DEBUG Then ‘ Debug Commandline arguments for this application: Dim DebugArguments = “HotkeyMaker.exe /Hotkey=Escape /run=notepad.exe” Return DebugArguments.Split(” “).ToList #Else ‘ Nomal Commandline arguments: Return My.Application.CommandLineArgs.ToList #End If End Function #End Region 但这有一个很明显的问题,空格字符会产生误报,例如: MyProgram.exe /Run=”Process path with spaces.exe” 我们都知道,通常参数是用括号双引号” “字符或space字符分隔的标记分隔的,所以我会得到很多误报来定制我的调试参数。 在C#或VBNET如何才能改进函数以获取正确分隔的(自定义)参数列表? 更新2: […]

为什么我们不能在派生类中使用带参数的构造函数

为什么这不可能? 在使用constructor-parameter实例化“DerivedClass”时,我得到以下编译器错误: ‘GenericParameterizedConstructor.DerivedClass’不包含带有1个参数的构造函数 但调用一个非常相似的方法是有效的 为什么? class Program { static void Main(string[] args) { // This one produces a compile error // DerivedClass cls = new DerivedClass(“Some value”); // This one works; DerivedClass cls2 = new DerivedClass(); cls2.SomeMethod(“Some value”); } } public class BaseClass { internal T Value; public BaseClass() { } public BaseClass(T value) { […]

是否可以使用params关键字通过ref传递对象?

我想通过ref将一些参数传递给一个方法,但我不知道参数的数量。 我尝试过类似的东西,但它不起作用:D: public void myMethod(ref params object args) 我会考虑在params中传递指针,但它在C#中有点复杂:/ / 可能的解决方法? 编辑: 我想封装一部分代码,基本上是这样的: …. var collectionA = new List(); var myObject = // an object Optimizer.Optimize(ref collectionA, ref myObject); // cache //{ MaClass.Treatment(); // use collectionA stored in cache via Optimizer // the collectionA is modified in MaClass.Treatment() … //} Optimizer.EndOptimize(); … 目标,如果调用封装到我的优化器中,则不能一直请求我的服务器进行相同的处理(HTTPRequest)