2

Script#コンパイラを使用して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;
        }
    }
}
4

1 に答える 1

2

潜在的に問題があると思われるいくつかのこと。

  1. TemplateFile は ac# コード ストリームに設定されます。これは有効なテンプレートではないため、未設定のままにしておきます。
  2. 参照には script# mscorlib を含める必要があり、さらに、有効な script# アセンブリへのフル パスのみを含める必要があります。System.dll は script# アセンブリではありません。
  3. MemoryStream から読み取る前に、ストリームの位置を先頭に戻す必要があります。そうしないと、コンパイラがストリームに書き込みを行った後に末尾になり、それ以上読み取るものはありません。
  4. 作成した Compiler インスタンスで Compile の呼び出しが表示されず、オプション インスタンスが渡されます。私の推測では、スタック オーバーフロー スニペットにはありませんでした。

基本的な作業が完了したら、おそらく IErrorHandler も実装し、それをコンパイラに渡して、発生したエラー メッセージを取得する必要があります。

参考までに、 https://github.com/nikhilk/scriptsharp/tree/master/tests/ScriptSharp/Coreで同様のことを行う単体テストを確認することもできます。

単一のスタンドアロン式ではなく、有効な C# ソース ファイルが必要になることに注意してください。ただし、結果のスクリプトの最初と最後から何かを取り除いて、関心のある式だけのスクリプトを取得することで、おそらくそれに対処できます。

それが役立つことを願っています。

私はあなたがこれをどのように使用しているか、そしてどこで c# をコンパイルしてスクリプトを動的に作成しているかを理解することに確かに興味があります...

于 2012-09-06T14:05:42.200 に答える