使用脚本#编译代码(独立)

时间:2012-09-06 09:41:19

标签: c# javascript script#

我正在尝试使用Script#compiler将C#的小片段编译成JavaScript。

但我没有得到任何回报,GetStream() MemoryStreamSource甚至没有被调用,所以我一定做错了。

这是我的代码:

CodeScriptCompiler csc = new CodeScriptCompiler();

return csc.CompileCSharp("String.IsNullOrWhiteSpace(Model.MobilePhoneNumber)");

CodeScriptCompiler.cs

using System;
using System.Collections.Generic;
using ScriptSharp;

namespace CodeToScriptCompiler
{
    public class CodeScriptCompiler
    {
        ScriptCompiler sc = new ScriptCompiler();

        public string CompileCSharp(string csharpCode)
        {
            string errorMessages = String.Empty;

            CompilerOptions options = new CompilerOptions();
            options.Defines = new List<string>();
            options.References = new List<string>();
            options.References.Add("System.dll");
            options.Resources = new List<IStreamSource>();
            options.Sources = new List<IStreamSource>();
            options.Sources.Add(new MemoryStreamSource(csharpCode));
            options.TemplateFile = new MemoryStreamSource(csharpCode);

            MemoryStreamDestination output = new MemoryStreamDestination();
            options.ScriptFile = output;

            if (!options.Validate(out errorMessages))
            {
                return errorMessages;
            }

            return output.GetCompiledCode();
        }
    }
}

MemoryStreamSource.cs

using System.IO;
using System.Text;
using ScriptSharp;

namespace CodeToScriptCompiler
{
    public class MemoryStreamSource : IStreamSource
    {
        private string _code;

        private MemoryStream _memoryStream;

        public MemoryStreamSource(string code)
        {
            this._code = code;
        }

        public string Name
        {
            get { return "InMemoryCode"; }
        }

        public string FullName
        {
            get { return "InMemoryCode"; }
        }

        public void CloseStream(Stream stream)
        {
            stream.Close();
        }

        public Stream GetStream()
        {
            this._memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(this._code));

            return this._memoryStream;
        }
    }
}

MemoryStreamDestination.cs

using System;
using System.IO;
using ScriptSharp;

namespace CodeToScriptCompiler
{
    public class MemoryStreamDestination : IStreamSource
    {
        private MemoryStream _memoryStream;

        private string _compiledCode;

        public string Name
        {
            get { return "MemoryStreamDestination"; }
        }

        public string FullName
        {
            get { return "MemoryStreamDestination"; }
        }

        public void CloseStream(Stream stream)
        {
            if (String.IsNullOrWhiteSpace(this._compiledCode))
            {
                this._compiledCode = this.GetCompiledCode();
            }

            stream.Close();
        }

        public Stream GetStream()
        {
            this._memoryStream = new MemoryStream();

            return this._memoryStream;
        }

        public string GetCompiledCode()
        {
            if (!String.IsNullOrWhiteSpace(this._compiledCode))
            {
                return this._compiledCode;
            }

            if (this._memoryStream != null)
            {
                using (StreamReader sr = new StreamReader(this._memoryStream))
                {
                    return sr.ReadToEnd();
                }
            }

            return String.Empty;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我认为有些事情可能存在问题。

  1. TemplateFile设置为c#代码流。保持不设置,因为那不是有效的模板。
  2. 引用应包括脚本#mscorlib,此外,只包含有效脚本#程序集的完整路径。 System.dll不是脚本#assembly。
  3. 在您从MemoryStream读取之前,您需要将流位置设置回到开头,否则它在编译器写入之后就结束了,并且没有其他内容可供阅读。
  4. 在您创建的编译器实例上没有看到对Compile的调用,传入了options实例。我的猜测是你做到了,只是没有堆栈溢出代码段。
  5. 你可能还应该实现IErrorHandler并将其传递给编译器,以便在基本的工作正常时发生错误消息。

    作为参考,您还可以查看https://github.com/nikhilk/scriptsharp/tree/master/tests/ScriptSharp/Core处的类似测试的单元测试。

    请注意,您需要一个有效的c#源文件,而不是一个独立的表达式。但是,您可以通过从生成的脚本的开头和结尾剥离内容来获取该内容,以获取您关注的表达式的脚本。

    希望有所帮助。

    我当然感兴趣/好奇了解你是如何使用它的,以及你在哪里动态编译c#...

相关问题