コード例 #1
0
        public static Type Compile(string json)
        {
            JsonClassGenerator.JsonClassGenerator gen = new JsonClassGenerator.JsonClassGenerator();
            gen.Example       = json;
            gen.UseProperties = true;
            gen.Namespace     = "__JSON__";
            gen.MainClass     = "__JSON__";
            gen.UsePascalCase = true;

            string source = "";

            using (StringWriter sw = new StringWriter())
            {
                gen.OutputStream = sw;
                gen.GenerateClasses();
                sw.Flush();
                source = sw.ToString();
            }

            string nLocation = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
            string nPath     = Path.Combine(nLocation, "Newtonsoft.Json.dll");

            Type type = Utils.CompileHelper.GenerateAssemblyInMemory(source, nPath).GetType("__JSON__.__JSON__");

            return(type);
        }
コード例 #2
0
 public static bool GenClassStringByJSON(string JSONString, string NameSpace, string SecondaryNameSpace, string TargetFolder, string MainClass)
 {
     try
     {
         var gen = new JsonClassGenerator.JsonClassGenerator()
         {
             Example                    = JSONString,
             InternalVisibility         = false,
             CodeWriter                 = new CSharpCodeWriter(),
             ExplicitDeserialization    = false,
             Namespace                  = NameSpace,
             NoHelperClass              = true,
             SecondaryNamespace         = SecondaryNameSpace,
             TargetFolder               = TargetFolder,
             UseProperties              = true,
             MainClass                  = MainClass,
             SortMemberFields           = true,
             UsePascalCase              = true,
             UseNestedClasses           = false,
             ApplyObfuscationAttributes = false,
             SingleFile                 = true,
             ExamplesInDocumentation    = true
         };
         if (gen == null)
         {
             return(false);
         }
         try
         {
             //需要升为.net framework 4.6才能打开这段注释
             //gen.GenerateClasses();
         }
         catch (Exception ex)
         {
             MessageBox.Show("无法生成代码: " + ex.Message, "错误", MessageBoxButtons.OK);
             throw ex;
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     return(true);
 }
コード例 #3
0
        public static Type Compile(string json)
        {
            JsonClassGenerator.JsonClassGenerator gen = new JsonClassGenerator.JsonClassGenerator();
            gen.Example       = json;
            gen.UseProperties = true;
            gen.Namespace     = "__JSON__";
            gen.MainClass     = "__JSON__";
            gen.UsePascalCase = true;

            string source = "";

            using (StringWriter sw = new StringWriter())
            {
                gen.OutputStream = sw;
                gen.GenerateClasses();
                sw.Flush();
                source = sw.ToString();
            }

            using (CSharpCodeProvider compiler = new CSharpCodeProvider())
            {
                CompilerParameters parameters = new CompilerParameters()
                {
                    GenerateInMemory = true
                };
                string nLocation = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
                string nPath     = Path.Combine(nLocation, "Newtonsoft.Json.dll");
                parameters.ReferencedAssemblies.Add(nPath);
#if FRCORE
                var mscorPath = compiler.GetReference("System.Private.CoreLib.dll").Display;
                parameters.ReferencedAssemblies.Add(mscorPath);
#endif

                CompilerResults results = compiler.CompileAssemblyFromSource(parameters, source);
                Type            type    = results.CompiledAssembly.GetType("__JSON__.__JSON__");
                return(type);
            }
        }