1 安装 Microsoft.CodeAnalysis

我这里创建的是WPF的项目,首先再VS2015中用NuGet控制台进行安装

Install-Package Microsoft.CodeAnalysis

Install-Package Microsoft.CodeAnalysis.CSharp.Scripting

2 using 添加

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

3 Hello World

  #region 01 Hello world
//Hello Rolysn
Console.WriteLine(CSharpScript.RunAsync(
@"string ret =""Hello Rolysn"";")
.Result.GetVariable("ret").Value); #endregion

4 引用和命名空间

 #region 02 References and Namespaces
ScriptOptions scriptOptions = ScriptOptions.Default;
#region 引用配置
//Add reference to mscorlib
var mscorlib = typeof(System.Object).Assembly;
var systemCore = typeof(System.Linq.Enumerable).Assembly;
scriptOptions = scriptOptions.AddReferences(mscorlib);
scriptOptions = scriptOptions.AddReferences(systemCore);
scriptOptions = scriptOptions.AddImports("System", "System.Linq", "System.Collections.Generic");
#endregion
var state = CSharpScript.RunAsync(@"int x=2;int y=3;int z =x*y;return z+1;");
//int x=2 => 2
string x = state.Result.GetVariable("x").Value.ToString();
//return return z+1 => 7
string retvalue = state.Result.ReturnValue.ToString();
//Int32
Type xtype = state.Result.GetVariable("x").Value.GetType();
if(xtype.FullName == typeof(int).FullName)
{ }
//int z =x*y => 6
string z =state.Result.GetVariable("z").Value.ToString(); string sourceCode = state.Result.Script.Code; string output = "";
foreach (var p in state.Result.Variables)
{
output += string.Format("name:{0},type:{1},value:{2};", p.Name, p.Type, p.Value);
}
Console.WriteLine(output);
#endregion
 #region 03 SyntaxTree
var tree = CSharpSyntaxTree.ParseText(@"
public class MyClass
{
public int FnSum(int x,int y)
{
return x+y;
}
}"); var syntaxRoot = tree.GetRoot();
var MyClass = syntaxRoot.DescendantNodes().OfType<ClassDeclarationSyntax>().First();
var MyMethod = syntaxRoot.DescendantNodes().OfType<MethodDeclarationSyntax>().First(); Console.WriteLine(MyClass.Identifier.ToString());
Console.WriteLine(MyMethod.Identifier.ToString());
Console.WriteLine(MyMethod.ParameterList.ToString());
#endregion

5  宿主对象交互

 #region 04 Globals

 var state4 = CSharpScript.RunAsync(@"var ret = requestData + "" from globals""; ", globals: new Globals { requestData = "hello world" });
Console.WriteLine( state4.Result.GetVariable("ret").Value); #endregion
     public class Globals
{
/// <summary>
/// 可以在脚本中直接进行调用
/// </summary>
public string requestData { get; set; } }

Microsoft.CodeAnalysis 入门的更多相关文章

  1. VS2012 Unit Test——Microsoft Fakes入门

    如题,本文主要作为在VS2012使用Fakes的入门示例,开发工具必须是VS2012或更高版本. 关于Fakes的MSDN地址:http://msdn.microsoft.com/en-us/libr ...

  2. Microsoft Hololens 入门系列-01-开篇

    1.能做什么 Microsoft HoloLens是第一台运行Windows10系统的全息计算机,它已经不受任何限制——没有线缆和听筒,并且不需要连接电脑.Microsoft Hololens能够让你 ...

  3. Roslyn入门(二)-C#语义

    先决条件 Visual Studio 2017 .NET Compiler Platform SDK Rosyln入门(一)-C#语法分析 简介 今天,Visual Basic和C#编译器是黑盒子:输 ...

  4. Roslyn 入门:使用 .NET Core 版本的 Roslyn 编译并执行跨平台的静态的源码

    Roslyn 是微软为 C# 设计的一套分析器,它具有很强的扩展性.以至于我们只需要编写很少量的代码便能够编译并执行我们的代码. 作为 Roslyn 入门篇文章之一,你将可以通过本文学习如何开始编写一 ...

  5. Roslyn 入门:使用 Roslyn 静态分析现有项目中的代码

    Roslyn 是微软为 C# 设计的一套分析器,它具有很强的扩展性.以至于我们只需要编写很少量的代码便能够分析我们的项目文件. 作为 Roslyn 入门篇文章,你将可以通过本文学习如何开始编写一个 R ...

  6. Roslyn入门(一)-C#语法分析

    演示环境 Visual Studio 2017 .NET Compiler Platform SDK 简介 今天,Visual Basic和C#编译器是黑盒子:输入文本然后输出字节,编译管道的中间阶段 ...

  7. SourceGenerator入门指北

    SourceGenerator介绍 SourceGenerator于2020年4月29日在微软的.net blog首次介绍,大概说的是开发者编可以写分析器,在项目代码编译时,分析器分析项目既有的静态代 ...

  8. NET Core 1.0 RC2

    NET Core 1.0 RC2 历险之旅 文章背景:对于.NET Core大家应该并不陌生, 从它被 宣布 到现在已经有1-2年的时间了,其比较重要的一个版本1.0 RC2 也即将发布..Net C ...

  9. 2018-8-29-Roslyn-静态分析

    title author date CreateTime categories Roslyn 静态分析 lindexi 2018-08-29 09:10:19 +0800 2018-03-13 14: ...

随机推荐

  1. postgresql无法安装pldbgapi的问题

    要对函数进行调试需要安装插件pldbgapi,当初在windows上面的postgresql实例中执行了一下语句就安装上了: create extension pldbgapi; 但是在linux中执 ...

  2. mysql查询性能优化

    mysql查询过程: 客户端发送查询请求. 服务器检查查询缓存,如果命中缓存,则返回结果,否则,继续执行. 服务器进行sql解析,预处理,再由优化器生成执行计划. Mysql调用存储引擎API执行优化 ...

  3. IP报头

      位字段的值设置为二进制的0100表示IP版本4(IPv4).设置为0110表示IP版本6(IPv6)   位,它表示32位字长的IP报头长度,设计报头长度的原因是数据包可选字段大小会发生变化.IP ...

  4. 《CPU的工作过程》

    本文转载自inter官方网址:https://software.intel.com/zh-cn/articles/book-Processor-Architecture_CPU_work_proces ...

  5. Windows Server 2008 R2常规安全设置及基本安全策略

    这篇文章主要介绍了Windows Web Server 2008 R2服务器简单安全设置,需要的朋友可以参考下 用的腾讯云最早选购的时候悲催的只有Windows Server 2008 R2的系统,原 ...

  6. ubuntu-14.04-server配置Jexus --安装步骤记录

    作者:郝喜路   个人主页:http://www.cnicode.com      博客地址:http://haoxilu.cnblogs.com 说明:我是Linux菜鸟,自己尝试配置Jexus服务 ...

  7. Xamarin开发Android应用打包apk

    Visual Studio中用Xamarin开发Android应用,生成apk文件有3种方法 1.debug时,代码目录下bin\Debug中会自动生成调试用***-Signed.apk文件,但是文件 ...

  8. Mono+Jexus让C#运行在Linux(centos7_x64),学习笔记

    .h2cls { background: #6fa833 none repeat scroll 0 0 !important; color: #fff; font-family: "微软雅黑 ...

  9. ABP源码分析四十七:ABP中的异常处理

    ABP 中异常处理的思路是很清晰的.一共五种类型的异常类. AbpInitializationException用于封装ABP初始化过程中出现的异常,只要抛出AbpInitializationExce ...

  10. C语言图形库简单对比及EGE库的安装小手册

    近期在琢磨C语言的图形库,发现主要有如下几种选择: Turbo C 的graphics库 SDL EasyX EGE 1. 普遍认为Graphics库太老了,而且TurboC本身使用比较麻烦,网上一边 ...