1

実行時にコンパイルされたクラスに問題があります。私はこの2つのクラスのようなものを持っています:

ファーストクラス

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Program.Bullet {
  public class Class1{
    private int i;
    public Class1(int j){
      i=j;
    }
  }
} 

そして二等

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Program.Bullet;
namespace Program.Object {
  public class Class2{    
    public Class2(){
      Class1 c1 = new Class1(5);
    }
  }
}

この 2 つのクラスを実行時にコンパイルして、プロジェクトで使用したいと考えています。だから私はそれをコンパイルする機能を持っています(XmlNodeにはfullPathなどに関するデータがあります):

private ModuleBuilder moduleBuilder;
private List<string> isCompiled;

private void Compile(XmlNode compileingClasses) {
        foreach (XmlNode compileingClass in compileingClasses) {
            string fullPath = compileingClass.Attributes["path"].InnerText;
            string type = compileingClass.Attributes["name"].InnerText; ;
            if (!isCompiled.Contains(type)) {
                isCompiled.Add(type);
                var syntaxTree = SyntaxTree.ParseFile("../../File1/File2/" + fullPath);

                var comp = Compilation.Create("Test.dll"
                    , syntaxTrees: new[] { syntaxTree }
                    , references: metadataRef
                    , options: comilationOption
                    );

                // Runtime compilation and check errors
                var result = comp.Emit(moduleBuilder);
                if (!result.Success) {
                    foreach (var d in result.Diagnostics) {
                        Console.WriteLine(d);
                    }
                    throw new XmlLoadException("Class not found " + fullPath);
                }
            }
        }
    }

Class1 から Class2 への参照を取得することは可能ですか?

編集:より良い質問

コンパイル済みで MetadataReference を作成することは可能Class1ですか?

何かのようなもの:

string fullName = bullet.Attributes["fullName"].InnerText;
var o = moduleBuilder.GetType(fullName);
metadataRef.Add(new MetadataFileReference(o.Assembly.Location));

この投げNotSupportedException

4

1 に答える 1