C# ASP.net:如何运行反射。键入并将结果输出到webform文本框中?
我只写了一年多一点的代码,所以如果我似乎遗漏了一些明显的东西,请随意解释 我正在为我的C#类领导一个研究小组,并希望建立一个ASP.net页面,该页面可以通过VS和浏览器在本地运行,用户可以在命名空间中构建类,在webform文本框中编写一个主方法,并在屏幕右侧查看输出(就像一个网络教程,只是安全性不是一个问题:我只是想在Bitbucket上抛出一个项目) 因此,我正在构建、编译和实例化一个类和Main方法,并且已经知道了如何访问由CompiilerResults创建的DLL文件,但是我不知道如何从后面的代码中执行该文件,或者将任何输出转换为字符串变量。我会满足于执行,并在控制台中查看结果;导入ant的问题是,输入的代码必须运行。以下是我所拥有的(大多数注释代码都是用来测试输出的): 母版页:C# ASP.net:如何运行反射。键入并将结果输出到webform文本框中?,c#,asp.net,webforms,C#,Asp.net,Webforms,我只写了一年多一点的代码,所以如果我似乎遗漏了一些明显的东西,请随意解释 我正在为我的C#类领导一个研究小组,并希望建立一个ASP.net页面,该页面可以通过VS和浏览器在本地运行,用户可以在命名空间中构建类,在webform文本框中编写一个主方法,并在屏幕右侧查看输出(就像一个网络教程,只是安全性不是一个问题:我只是想在Bitbucket上抛出一个项目) 因此,我正在构建、编译和实例化一个类和Main方法,并且已经知道了如何访问由CompiilerResults创建的DLL文件,但是我不知道如
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="stylesheets" runat="server">
<link rel="stylesheet" href="MasterPage.master.css" type="text/css" />
</asp:ContentPlaceHolder>
</head>
<body>
<div>
<asp:ContentPlaceHolder ID="header" runat="server">
</asp:ContentPlaceHolder>
</div>
<div>
<asp:ContentPlaceHolder id="codeColumn" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="outputColumn" runat="server">
</asp:ContentPlaceHolder>
</div>
<form runat="server">
<asp:Label ID="headerLabel" Font-Size="X-Large" runat="server"></asp:Label>
<!-- User enters code here: -->
<asp:TextBox ID="codeTextArea" TextMode="MultiLine" Columns ="50" Rows ="30" runat="server"></asp:TextBox>
<!-- Results of code, or caught errors, should be output here: -->
<asp:TextBox ID="outputTextArea" TextMode="MultiLine" Rows="30" runat="server"></asp:TextBox>
<!-- Clicking the submit button should start the code behind to handle errors, and run clean code: -->
<asp:Button ID="submitButton" Text="Run!" OnClick="submitButton_Click" runat="server" />
</form>
<div>
<asp:ContentPlaceHolder ID="footer" runat="server">
<asp:HyperLink NavigateUrl="~/Default.aspx" runat="server">First Tutorial</asp:HyperLink>
<asp:HyperLink NavigateUrl="~/Default.aspx" runat="server">Next Tutorial</asp:HyperLink>
</asp:ContentPlaceHolder>
</div>
</body>
</html>
我已经让程序在控制台中运行,没有反射。键入,使用System.Diagnostics.Process.Start,将路径发送到DLL(或EXE)文件,由CompilerResults检索。但是,我无法将Console输出转换为变量。我将Console.Out设置为StreamWriter,然后设置为TextWriter,但这两种设置似乎都没有从我的代码内部产生任何影响,并将代码直接放入codeString变量,然后将Console输出写入文件,引发了一个错误(此文件正被另一个进程使用)。如果有人感兴趣,我可以发布更新的代码。而且,我想我已经找到了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.IO;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submitButton_Click(object sender, EventArgs e)
{
if (codeTextArea.Text != null)
{
// Build a class and assembly:
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
string codeString = @"using System; using System.IO; namespace StudyGroup { public class WebProgram { private String _outputString = ""output""; public static void Main() { ";
codeString += codeTextArea.Text;
codeString += @" } } }";
CompilerResults results = provider.CompileAssemblyFromSource(parameters, codeString);
var path = results.PathToAssembly;
// Output:
string outputString = "";
// Error handling:
if (results.Errors.HasErrors)
{
outputString += "You haz errors:\n";
foreach (CompilerError error in results.Errors)
{
outputString += error.ErrorText;
outputString += "\n";
}
//throw new InvalidOperationException(outputString);
outputTextArea.Text = outputString;
}
else
{
// Instantiate an instance and invoke Main method:
//var assembly = Assembly.LoadFrom(path);
var process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = path;
process.Start();
var moreOutput = process.StandardOutput.ReadToEnd();
outputString += moreOutput;
// Output:
Console.WriteLine(outputString);
Console.ReadLine();
outputTextArea.Text = outputString;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.IO;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submitButton_Click(object sender, EventArgs e)
{
if (codeTextArea.Text != null)
{
// Build a class and assembly:
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
string codeString = @"using System; using System.IO; namespace StudyGroup { public class WebProgram { private String _outputString = ""output""; public static void Main() { ";
codeString += codeTextArea.Text;
codeString += @" } } }";
CompilerResults results = provider.CompileAssemblyFromSource(parameters, codeString);
var path = results.PathToAssembly;
// Output:
string outputString = "";
// Error handling:
if (results.Errors.HasErrors)
{
outputString += "You haz errors:\n";
foreach (CompilerError error in results.Errors)
{
outputString += error.ErrorText;
outputString += "\n";
}
//throw new InvalidOperationException(outputString);
outputTextArea.Text = outputString;
}
else
{
// Instantiate an instance and invoke Main method:
//var assembly = Assembly.LoadFrom(path);
var process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = path;
process.Start();
var moreOutput = process.StandardOutput.ReadToEnd();
outputString += moreOutput;
// Output:
Console.WriteLine(outputString);
Console.ReadLine();
outputTextArea.Text = outputString;
}
}
}
}