Я пытаюсь создать экземпляр класса с помощью Roslyn Compiler.Но он выдает ошибку выше: код:
namespace CSharptoJSON.Controllers
{
public class InstanceCreator
{
/// Compiles C# code and creates instances of object types
/// <returns>Collection of object instances</returns>
public static IEnumerable<object> CompileClasses(string csharp)
{
if (string.IsNullOrEmpty(csharp))
{
throw new ArgumentNullException(nameof(csharp));
}
SyntaxTree tree = CSharpSyntaxTree.ParseText(csharp);
// CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
CompilationUnitSyntax root = (CompilationUnitSyntax) tree.GetRoot();
// add Using statements to syntax tree
var system = SyntaxFactory.IdentifierName("System");
var systemCollections = SyntaxFactory.QualifiedName(system, SyntaxFactory.IdentifierName("Collections"));
var systemCollectionsGeneric = SyntaxFactory.QualifiedName(systemCollections, SyntaxFactory.IdentifierName("Generic"));
var systemLinq = SyntaxFactory.QualifiedName(system, SyntaxFactory.IdentifierName("Linq"));
var systemText = SyntaxFactory.QualifiedName(system, SyntaxFactory.IdentifierName("Text"));
var systemXml = SyntaxFactory.QualifiedName(system, SyntaxFactory.IdentifierName("Xml"));
var declaredUsings = root.Usings.Select(x => x.Name.ToString()).ToList();
if (!declaredUsings.Contains("System"))
{
root = root.AddUsings(SyntaxFactory.UsingDirective(system).NormalizeWhitespace());
}
if (!declaredUsings.Contains("System.Collections"))
{
root = root.AddUsings(SyntaxFactory.UsingDirective(systemCollections).NormalizeWhitespace());
}
if (!declaredUsings.Contains("System.Collections.Generic"))
{
root = root.AddUsings(SyntaxFactory.UsingDirective(systemCollectionsGeneric).NormalizeWhitespace());
}
if (!declaredUsings.Contains("System.Linq"))
{
root = root.AddUsings(SyntaxFactory.UsingDirective(systemText).NormalizeWhitespace());
}
if (!declaredUsings.Contains("System.Text"))
{
root = root.AddUsings(SyntaxFactory.UsingDirective(systemLinq).NormalizeWhitespace());
}
if (!declaredUsings.Contains("System.Xml"))
{
root = root.AddUsings(SyntaxFactory.UsingDirective(systemXml).NormalizeWhitespace());
}
tree = CSharpSyntaxTree.Create(root);
root = tree.GetCompilationUnitRoot();
Console.WriteLine(tree);
// generate compiled object with references to commonly used .NET Framework assemblies
var compilation = CSharpCompilation.Create("CSharp2Json",
new SyntaxTree[] { tree },
references: new[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location), // mscorelib.dll
MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location), // System.Core.dll
MetadataReference.CreateFromFile(typeof(Uri).Assembly.Location), // System.dll
MetadataReference.CreateFromFile(typeof(DataSet).Assembly.Location), // System.Data.dll
// MetadataReference.CreateFromFile(typeof(EntityKey).Assembly.Location), // System.Data.Entity.dll
MetadataReference.CreateFromFile(typeof(XmlDocument).Assembly.Location), // System.Xml.dll
},
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
System.Console.WriteLine(compilation);
// load compiled bits into assembly
Assembly assembly;
using (var memoryStream = new MemoryStream())
{
var result = compilation.Emit(memoryStream);
/* if (!result.Success)
{
throw new System.ArgumentException("Parameter cannot be null", "original");
}
*/
assembly = AppDomain.CurrentDomain.Load(memoryStream.ToArray());
}
// instantiate object instances from assembly types
foreach (var definedType in assembly.DefinedTypes)
{
Type objType = assembly.GetType(definedType.FullName);
if (objType.BaseType?.FullName != "System.Enum")
{
object instance = null;
try
{
instance = assembly.CreateInstance(definedType.FullName);
}
catch (MissingMethodException)
{
// no default constructor - eat the exception
}
if (instance != null)
{
yield return instance;
}
}
}
}
}
}
// журналы ошибок:
Необработанное исключение: System.BadImageFormatException: не удалось загрузить файл или сборку 'загружено 0 байтиз RoslynCompileSample, Version = 1.0.0.0, Culture = нейтральный, PublicKeyToken = null 'или одной из его зависимостей.Была предпринята попытка загрузить программу с неверным форматом.---> System.BadImageFormatException: неверный формат IL.--- Конец внутренней трассировки стека исключений --- в System.Reflection.RuntimeAssembly.nLoadImage (Byte [] rawAssembly, Byte [] rawSymbolStore, свидетельства доказательств, StackCrawlMark & stackMark, логический метод проверки, логический fSkipInteg rityCheck, SecurityConteSecurityStext).AppDomain.Load (Byte [] rawAssembly) в RoslynCompileSample.Program.d__0.MoveNext () в C: \ Users \ RoboMQ-sagarrana \ Desktop \ graphql \ RoslynCompileSample \ RoslynCompileSample \ Program.cs: строка 100 в RoslynCompileSample.Строка [] args) в C: \ Users \ RoboMQ-sagarrana \ Desktop \ graphql \ RoslynCompileSample \ RoslynCompileSample \ Program.cs: строка 141