使用Script#编译代码(独立) [英] Use Script# to compile code (stand-alone)

查看:86
本文介绍了使用Script#编译代码(独立)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Script#编译器将C#的小片段编译为JavaScript.

I'm trying to compile small fragments of C# into JavaScript using the Script# compiler.

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

But I don't get anything in return, GetStream() in my MemoryStreamSource is not even being called, so I must be doing something wrong.

这是我的代码:

CodeScriptCompiler csc = new CodeScriptCompiler();

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

CodeScriptCompiler.cs

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

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

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;
        }
    }
}

推荐答案

我认为某些问题可能会引起问题.

Some things I see potentially problematic.

  1. TemplateFile设置为c#代码流.保留它为未设置状态,因为它不是有效的模板.
  2. 参考文献应包括脚本号mscorlib,此外,还应仅包含有效脚本号程序集的完整路径. System.dll不是脚本#程序集.
  3. 从MemoryStream读取数据之前,需要将流位置重新设置为开始,否则应在编译器写入流之后将其放置在末尾,并且没有其他内容可供读取.
  4. 在您创建的Compiler实例上看不到对Compile的调用,没有传入options实例.我的猜测是您确实这样做了,只是堆栈溢出片段中没有.

一旦基本工作正常,您可能还应该实现IErrorHandler并将其传递给编译器以获取错误消息(如果发生).

You probably should also implement IErrorHandler and pass that to the compiler to get error messages should they occur, once you have the basic thing working.

作为参考,您还可以在 https:/中查看单元测试/github.com/nikhilk/scriptsharp/tree/master/tests/ScriptSharp/Core 会执行类似的操作.

For reference you can also look at the unit tests at https://github.com/nikhilk/scriptsharp/tree/master/tests/ScriptSharp/Core which does something similar.

请注意,您将需要有效的c#源文件,而不是单个独立的表达式.但是,您可以通过从结果脚本的开头和结尾处剥离内容来获取所需的脚本,来解决该问题.

Note that you'll need a valid c# source file, rather than a single standalone expression. You can however likely deal with that by stripping off stuff from the start and end of the resulting script to get the script for just the expression you care about.

希望有帮助.

我当然有兴趣/好奇地了解您如何使用它,以及在何处编译c#以动态编写脚本...

I am certainly interested/curious to understand how you're using this, and where you're compiling c# to script dynamically...

这篇关于使用Script#编译代码(独立)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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