摘要:结合控制台重定向和C#即使编译器(见我上面两篇文章)写的WinForm即时C#编译器,功能还不错。文本框就是你Main方法内的语句,可以输入任意测试代码,支持错误行号定位,编译结果捕获,自动拆分窗格等,程序按F5执行,F5…… 忘记在代码里面加说明了:( 时间不早了,上传睡觉,带批处理build代码和SharpDevelop方式源代码哦。可以拿下面的代码测试程序//F5:CompileandRuntheappF6:KillcurrentRunningApp//-----------------------------------------------------------System.D.
阅读全文
摘要:用visual studio编写测试程序需要新建solution,然后project,命名和存盘都是麻烦事。可以考虑写一个自己的编译环境。通过 Microsoft.CSharp.CSharpCodeProvider() 对象的 CompileAssemblyFromSource(parameters,codes) 方法就能得到编译结果对象 CompilerResults;然后结果对象再通过反射机制 result.CompiledAssembly.GetType("Phoenix") 就可以通过编译生成的程序集包含Main方法的类型。然后你可以 Phoenix.GetMeth
阅读全文
摘要:很多时候,我们需要捕获Console输出,然后在文本框等控件中显示。例如SnippetCompiler就实现了编译源代码并将结果在下面的ListView显示的功能。Console.SetOut(TextWriter)设置Console输出重定向,这样我们需要写一个TextWriter的派生类,这个类的构造函数我们传入要定向目标控件的引用,然后在 public override void Write(char value) 中修改引用控件的BeginInvoke方法挂一个Delegate关联控制台输出流到控件。代码如下,足够精简了。usingSystem;usingSystem.Windows.
阅读全文
摘要:///<summary>///referencetypeisallocateonHEAP,///theassignmentstatementjustsetapinttotheobject.///thereferenceobjectmemorycollectiononappexit!///</summary>usingSystem;usingSystem.Collections.Generic;///<summary>///classisreferencetype;structisvaluetype///</summary>class/*struc
阅读全文
摘要:///<summary>///参数修饰符params、out、ref///</summary>usingSystem;namespaceConDemo{classMainClass{staticdoubleAverage(paramsdouble[]values){doubleaverage=0;foreach(variteminvalues){average+=item;}average=average/values.Length;returnaverage;}publicstaticvoidAdd(intx,inty,outintans){ans=x+y;}stat
阅读全文
摘要:DEMO: Object Initializer, Anonymouse Method.usingSystem;usingSystem.Windows.Forms;publicclassMyClass{publicstaticvoidMain(){ButtonbtnHello=newButton();btnHello.Text="&Hello";btnHello.Click+=delegate{//AnonymouseMethodMessageBox.Show("Yes!It'stheAnonymouseMethod!");};Formf
阅读全文