如何在ASP.Net Web表单中使用Roslyn执行C#代码? [英] How I can execute C# code using Roslyn in ASP.Net web form?

查看:81
本文介绍了如何在ASP.Net Web表单中使用Roslyn执行C#代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.Net中使用roslyn编译器执行C#的执行代码。但是,我使用ASP.Net Web表单来构建用于执行代码的用户界面。所以,我想从一个文本框中获取代码,然后通过编译按钮将结果显示在另一个文本框中。



所以,我有这个控件:

 <%@     Page    标题  =  主页   语言  =  C#    MasterPageFile   = 〜/ Site.Master    AutoEventWireup   =  true    CodeBehind   =  Default.aspx.cs   继承  =  WebApplication2._Default   %>  

< span class =code-keyword>< asp:Content runat = server ID = FeaturedContent ContentPlaceHolderID = < span class =code-keyword> FeaturedContent >
< section class = 精选 >
< div class = < span class =code-keyword> content-wrapper >
< hgroup class = title >
< span class =code-keyword>< h1 > 编译工具< / h1 >
< / hgroup >
< p >
您可以使用Microsoft的Roslyn编译器编译C#的源代码< / p >
< br / >

< p >
< <跨度class =code-leadattribute> asp:TextBox ID = TextBox3 runat = server 高度 = 185px 宽度 = 480px OnTextChanged = TextBox3_TextChanged > < / asp:TextBox >
< / p >
< p >

< asp:Button ID = Button2 runat = server onclick = Button_Click 文本 = 编译 ForeColor = 黑色 / >
< / p >
< p >
< asp:TextBox ID = TextBox2 runat = server 高度 = 138px 宽度 = 390px > < / asp:TextBox >
< / p >
< / div >
< / section < span class =code-keyword>>
< / asp:内容 >
< asp:内容 runat = 服务器 ID = BodyContent ContentPlaceHolderID = 主要内容 >
< / asp:Content >







这里使用C#的Handler事件:

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.ComponentModel;
使用 Roslyn.Scripting;
使用 Roslyn.Scripting.CSharp;


命名空间 WebApplication2
{
public partial class _Default:Page
{



受保护 void Page_Load( object sender,EventArgs e)
{

}

protected void Button_Click( object sender,EventArgs e)
{

var engine = new ScriptEngine();
var session = engine.CreateSession();
String code = TextBox3.Text;
session.Execute(code);

}

受保护 void TextBox3_TextChanged(< span class =code-keyword> object sender,EventArgs e)
{

}
}
}









如何在TextBox2中显示Code(TextBox3)的执行结果? br $> b $ b



我在ASP.Net环境中初学者,任何帮助??

解决方案

< blockquote>



如果你是asp.net的初学者,我想知道你想要实现的是什么。



如果您正在尝试构建一个工具,您可以粘贴代码片段并查看它们是否有效 - 类似于快速测试 - 市场上已有工具用于这个。例如



linqpad / [ ^ ]

dotnetfiddle / [ ^ ]

csharppad [ ^ ]

Snippet Compiler / [ ^ ]



还有许多其他人列出在哪里 [ ^ ]



但是,如果你是仍然有兴趣继续y你正在计划,然后请准备好被压垮一点并通过这篇文章 [ ^ ],相当全面。



[请接受/投票给你的答案或解决方案以鼓励参与]


I have peice of code in ASP.Net for execute code of C# using roslyn compiler. However, I use ASP.Net web forms to build user interface for execute the code. So,I want to take code from one textbox and by "Compile" button I show the result in another textbox.

So, I have this controls:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>Compiler Tool</h1>
            </hgroup>
            <p>
                You can compile your source code of C# using Roslyn Compiler from Microsoft</p>
            <br/>

            <p>
                <asp:TextBox ID="TextBox3" runat="server" Height="185px"  Width="480px" OnTextChanged="TextBox3_TextChanged"></asp:TextBox>
            </p>
            <p>

                <asp:Button ID="Button2" runat="server" onclick="Button_Click" Text="Compile" ForeColor="Black" />
            </p>
            <p>
                <asp:TextBox ID="TextBox2" runat="server" Height="138px" Width="390px" ></asp:TextBox>
            </p>
        </div>
    </section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    </asp:Content>




And here the Handler event for it using C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Roslyn.Scripting;
using Roslyn.Scripting.CSharp;


namespace WebApplication2
{
    public partial class _Default : Page
    {



        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button_Click(object sender, EventArgs e)
        {

            var engine = new ScriptEngine();
            var session = engine.CreateSession();
            String code = TextBox3.Text;
            session.Execute(code);

        }

        protected void TextBox3_TextChanged(object sender, EventArgs e)
        {

        }
    }
}





How could I show the result of execution of Code (TextBox3) in TextBox2??


Iam beginner in ASP.Net environment, Any help??

解决方案

Hi,

If you are a beginner in asp.net I wonder what you are trying to achieve by this.

If you are trying to build a tool where you would paste code snippets and see if they work - something like a quick test - there are tools in the market already for this. Such as

linqpad/[^]
dotnetfiddle/[^]
csharppad[^]
Snippet Compiler/[^]

And a host of others listed here[^]

However, if you are still interested in going ahead with what you are planning, then please be prepared to be overwhelmed a bit and go through this article[^], pretty comprehensive.

[Please accept/up-vote answers or solutions that work for you to encourage participation]


这篇关于如何在ASP.Net Web表单中使用Roslyn执行C#代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆