我们是五月的花海 , 用青春拥抱时|

兴想事成

园龄:12年10个月粉丝:25关注:97

.net core 3.1 + 动态执行C#

 

1.使用 

using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;

2.定义 RoslynHelper 类, 添加一个 DataTable GetTableBySql(string sql = "") 方法

3. 先定义 待执行的  C# 脚本方法 

比如执行一个sql语句, 得到查询的结果集 DataTable 

==>去动态执行 Common.RoslynHelper().GetTableBySql(sql) 方法

string  = @"

using System;
using YXTASK.Common;
public class ScriptedClass
{
public System.Data.DataTable DataRes { get; set; }

public ScriptedClass()
{
string sql= "" select * from (select id,age.name  from StudentInfo) where age<=20"";
this.DataRes = new Common.RoslynHelper().GetTableBySql(sql);
}
}

";

 

4.调用

  

   1>传入需要注入的命名空间 

      var scriptOptions = ScriptOptions.Default.AddReferences(typeof(System.Data.DataTable).Assembly, typeof(RoslynHelper).Assembly);

   2>传入待执行的C# 文本

      var script1  = CSharpScript.RunAsync(code1, scriptOptions).Result;

   3>执行脚本

      var result1 =  script1.ContinueWithAsync<System.Data.DataTable>("new ScriptedClass().DataRes").Result;

   4>得到返回值

      DataTable   res =  result1.ReturnValue;

 

复制代码
    try
    {
        var scriptOptions = ScriptOptions.Default.AddReferences(typeof(System.Data.DataTable).Assembly, typeof(RoslynHelper).Assembly);
        var script1 = CSharpScript.RunAsync(code1, scriptOptions).Result;
        var result1 = script1.ContinueWithAsync<System.Data.DataTable>("new ScriptedClass().DataRes").Result;
        res = result1.ReturnValue;
        Console.WriteLine(res.Rows.Count);
        Console.WriteLine("");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        Console.WriteLine(ex.StackTrace);
        Console.WriteLine("");
    }
复制代码

 

posted @   兴想事成  阅读(206)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Good-bye My Loneliness ZARD
  2. 2 Say OK Vanessa Hudgens
  3. 3 All The Love In The World The Corrs
  4. 4 Adesso E Fortuna ~炎と永遠~ 加藤いづみ
Say OK - Vanessa Hudgens
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : BIRGISSON, ARNTHOR/KOTECHA, SAVAN

作曲 : Savan Kotecha/Arnthor Birgisson

Vanessa Hudgens - Say OK

Album: V

You are fine

You are fine

You are fine

You are fine

You are sweet

But I'm still a bit naive with my heart

When you're close I don't breathe

I can't find the words to speak

I feel sparks

But I don't wanna be into you

If you are not looking for true love, oh oh

No I don't wanna start seeing you

If I can't be your only one

So tell me when it's not alright

When it's not ok

Will you try to make me feel better?

Will you say alright? (say alright)

Will you say ok? (Say ok)

Will you stick with me through whatever?

Or run away

(Say that it's gonna be alright)

(That it's gonna be ok)

Say OK

When you call I don't know

If I should pick up the phone every time

I'm not like all my friends

Who keep calling up the boys, I'm so shy

But I don't wanna be into you

If you don't treat me the right way

See I can only start seeing you

If you can make my heart feel safe (feel safe)

When it's not alright

When it's not ok

Will you try to make me feel better?

Will you say alright? (say alright)

Will you say ok? (Say ok)

Will you stick with me through whatever?

Or run away

(Say that it's gonna be alright)

(That it's gonna be ok)

(Don't run away, don't run away)

Let me know if it's gonna be you

Boy, you've got some things to prove

Let me know that you'll keep me safe

I don't want you to run away so

Let me know that you'll call on time

Let me know that you won't be shy

Will you wipe my tears away

Will you hold me closer

When it's not alright

When it's not ok

Will you try to make me feel better

Will you say alright? (say alright)

Will you say ok? (Say ok)

Will you stick with me through whatever?

Or run away

(Say that it's gonna be alright)

(That it's gonna be ok)

Say OK

(Don't run away, don't run away)

(Say that it's gonna be alright)

(That it's gonna be ok)

(Don't run away)

Will you say OK

(Say that it's gonna be alright)

(That it's gonna be ok)

(Don't run away)

You are fine

You are fine