简体   繁体   English

编辑源代码并在C#中的运行时使用它

[英]Edit Source code and use it at runtime in c#

Basically I want to extract JsonSchema from Json string at runtime(Can't use Tools), found no library to do this. 基本上我想在运行时从Json字符串中提取JsonSchema(不能使用工具),没有库可以做到这一点。 Other way is to first Convert JSON to Class(Found one Open Source Code) then create JsonSchema using JSON.NET as below. 另一种方法是先将JSON转换为Class(找到一个开源代码),然后使用JSON.NET创建JsonSchema,如下所示。

var generator = new JSchemaGenerator();
JSchema schema = generator.Generate(typeof(Rootobject));

But Rootobject class is not known at compile time, so I'm getting compile time error. 但是Rootobject类在编译时未知,所以我遇到了编译时错误。 Is there any other way to do this? 还有其他方法吗? Please help 请帮忙

The best way I can think of is to use your code to generate a class's source code, compile that using the CodeDOM, load the generated assembly and generate the Schema for that object then. 我能想到的最好的方法是使用您的代码生成类的源代码,使用CodeDOM对其进行编译,然后加载生成的程序集并为该对象生成Schema。

Information on using the CodeDOM to build an Assembly at runtime can be found on MSDN here: Generating and Compiling Source Code from a CodeDOM graph (it's the second headline you'd be interested in). 可以在MSDN上找到有关在运行时使用CodeDOM 生成程序集的信息: 从CodeDOM图生成和编译源代码 (这是您感兴趣的第二个标题)。

Information on how to load the Assembly can be found on MSDN here: How to: Load Assemblies into an Application Domain 可以在MSDN上找到有关如何加载程序集的信息如何:将程序集加载到应用程序域中

Hope that helps in solving your problem. 希望能帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM