c# – 使用Roslyn时,Console不包含ReadKey的定义
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 使用Roslyn时,Console不包含ReadKey的定义,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3465字,纯文字阅读大概需要5分钟。
内容图文
![c# – 使用Roslyn时,Console不包含ReadKey的定义](/upload/InfoBanner/zyjiaocheng/767/a8510d8482db45e7bb6b1238468449c1.jpg)
我试图动态编译代码并在运行时执行它.所以我按照http://www.tugberkugurlu.com/archive/compiling-c-sharp-code-into-memory-and-executing-it-with-roslyn作为指导.
示例中给出的代码完美地起作用.但是,如果我使用Console.ReadKey(),它会给我错误CS0117:’Console’不包含’ReadKey’的定义.我在某处读到这是因为dotnet核心不支持ReadKey(‘Console’ does not contain a definition for ‘ReadKey’ in asp.net 5 console App),但我目前正在使用“Microsoft.NETCore.App”,如果我在代码中显式使用它而不是使用Roslyn,则Console.ReadKey()可以正常工作.
>这是罗斯林的问题还是我做错了什么?
>“Microsoft.NETCore.App”和dotnet核心是一回事吗?我怀疑我可能正在使用其他东西作为我的目标(允许我使用ReadKey)和dotnet核心与Roslyn
>是否有可能将罗斯林的目标改为其他目标?
提前致谢.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
namespace DemoCompiler
{
class Program
{
public static void roslynCompile()
{
string code = @"
using System;
using System.Text;
namespace RoslynCompileSample
{
public class Writer
{
public void Write(string message)
{
Console.WriteLine(message);
Console.ReadKey();
}
}
}";
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
string assemblyName = Path.GetRandomFileName();
MetadataReference[] references = new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
MetadataReference.CreateFromFile(typeof(Enumerable).GetTypeInfo().Assembly.Location)
};
CSharpCompilation compilation = CSharpCompilation.Create(
assemblyName,
syntaxTrees: new[] { syntaxTree },
references: references,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (var ms = new MemoryStream())
{
EmitResult result = compilation.Emit(ms);
if (!result.Success)
{
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
foreach (Diagnostic diagnostic in failures)
Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
}
else
{
ms.Seek(0, SeekOrigin.Begin);
Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(ms);
var type= assembly.GetType("RoslynCompileSample.Writer");
var instance = assembly.CreateInstance("RoslynCompileSample.Writer");
var meth = type.GetMember("Write").First() as MethodInfo;
meth.Invoke(instance, new [] {assemblyName});
}
}
}
}
}
编辑:我试图引用System.Console.dll,但我和System.Private.CoreLib之间发生冲突.我该如何解决?
解决方法:
一种方法是将< PreserveCompilationContext> true< / PreserveCompilationContext> *添加到项目文件中,然后使用Microsoft.Extensions.DependencyModel.DependencyContext获取当前项目的所有参考程序集:
var references = DependencyContext.Default.CompileLibraries
.SelectMany(l => l.ResolveReferencePaths())
.Select(l => MetadataReference.CreateFromFile(l));
这似乎可以避免您获得的错误,但会导致以下警告:
warning CS1701: Assuming assembly reference ‘System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ used by ‘System.Console’ matches identity ‘System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ of ‘System.Runtime’, you may need to supply runtime policy
As far as I can tell, this should not be an issue.
*这假设您正在使用csproj / VS2017.如果您仍在使用project.json / VS2015,则可以使用“buildOptions”完成相同的操作:{“preserveCompilationContext”:true}.
内容总结
以上是互联网集市为您收集整理的c# – 使用Roslyn时,Console不包含ReadKey的定义全部内容,希望文章能够帮你解决c# – 使用Roslyn时,Console不包含ReadKey的定义所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。