.net – 如何使用Roslyn添加对内存流中编译的类型的引用?
发布时间:2020-12-16 03:29:42 所属栏目:asp.Net 来源:网络整理
导读:作为编译的输入,我有一个字符串,其中包含以下代码: public class Person{ public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public string EmailAddress { get; set; }} 我有以下代码,请注意[01]
作为编译的输入,我有一个字符串,其中包含以下代码:
public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public string EmailAddress { get; set; } } 我有以下代码,请注意[01]的评论.这里的目的是获取一个包含类的字符串(来自this.Source并将程序集的代码发送到MemoryStream中). var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location); var assemblyName = Guid.NewGuid().ToString(); var syntaxTrees = CSharpSyntaxTree.ParseText(this.Source); // build references up var references = new List<MetadataReference>(); //[01] references.Add("System.dll")); // set up compilation var compilation = CSharpCompilation.Create(assemblyName) .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)) .AddReferences(references) .AddSyntaxTrees(syntaxTrees); // build the assembly Assembly assembly; using (var stream = new MemoryStream()) { // this is broked... EmitResult compileResult = compilation.Emit(stream); // [02] we get here,with diagnostic errors (check compileResult.Diagnostics) assembly = Assembly.Load(stream.GetBuffer()); } 在[02]我得到一个BadImageFormat异常,在compileResult.Diagnostics中有以下内容: [0]: warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options. [1]: (2,18): error CS0518: Predefined type 'System.Object' is not defined or imported [2]: (4,16): error CS0518: Predefined type 'System.String' is not defined or imported [3]: (4,40): error CS0518: Predefined type 'System.Void' is not defined or imported [4]: (5,16): error CS0518: Predefined type 'System.String' is not defined or imported [5]: (5,39): error CS0518: Predefined type 'System.Void' is not defined or imported [6]: (6,16): error CS0518: Predefined type 'System.Int32' is not defined or imported [7]: (6,31): error CS0518: Predefined type 'System.Void' is not defined or imported [8]: (7,16): error CS0518: Predefined type 'System.String' is not defined or imported [9]: (7,43): error CS0518: Predefined type 'System.Void' is not defined or imported [10]: (2,18): error CS1729: 'object' does not contain a constructor that takes 0 arguments 如果我添加一个使用系统;到代码的开头我得到以下错误: error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) 这让我相信我无法从我所在的位置访问System,这就是为什么我在那个伪代码references.Add(“System.dll”));中存根.这显然不正确,但这是我的意图. 有关更多信息,此处的目的是动态创建生成类型的实例并为属性赋值. 将引用提供给编译器的正确方法是什么? 或者,是否有另一种方法来编译这样的类(来自字符串输入)? 解决方法
调用
AssemblyMetadata.CreateFromFile(path) method,并传递typeof(object).Assembly.Location(或其他程序集).
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 我从添加引用对话框中找不到System.Web.MVC [我
- asp.net-mvc – 如何将特定于HTTP-Request的对象注入到我提
- asp.net-mvc – 如何在将model属性传递给局部视图时保持正确
- asp.net-mvc – 使用Html.BeginForm()与自定义路由
- asp.net-mvc – Telerik mvc网格标签顺序问题
- asp.net-mvc – 企业库日志记录 – 从格式中删除连字符
- asp.net – 免费DotNetNuke皮肤资源
- ASP.NET分页控件
- asp.net-mvc – 从ASP.NET MVC操作返回什么来允许jQuery aj
- asp.net – 实体框架6 EntityDataSource不在DbContext中调用
推荐文章
站长推荐
- asp.net-mvc-3 – 应该如何看待“分离”?
- asp.net-mvc – Request.GetOwinContext在单元测
- asp.net-mvc-4 – 使用Bootstrap日期时间选择器,
- asp.net – 未捕获错误:在初始化之前无法在弹出
- asp.net-mvc – Nuget如何指定包位置?
- asp.net – 实体框架CTP5,代码优先.可选的导航属
- 会话的最大长度是什么ASP.net 4.0会话ID – 存储
- ASP.NET Web API合同版本控制
- asp.net – Linq更新查询生成哪里0 = 1?
- asp.net-mvc – 我们应该使用Entity Framework C
热点阅读