public static string GenerateClass(JSchema schema, Dictionary <string, MemberDeclarationSyntax> typeContext, string propertyName, INamingRule namingRule) { if (schema.Type == JSchemaType.Object) { var className = namingRule.GetClassName(schema, propertyName); if (typeContext.ContainsKey(className)) { return(className); } if (string.IsNullOrEmpty(className)) { throw new Exception("Title not set"); } var classDeclaration = Syntax.ClassDeclaration( Syntax.Identifier(className)) .WithModifiers(new SyntaxTokenList().Add(Syntax.Token(SyntaxKind.PublicKeyword))) .WithKeyword( Syntax.Token(SyntaxKind.ClassKeyword, Syntax.TriviaList(Syntax.Space))) .WithOpenBraceToken( Syntax.Token(SyntaxKind.OpenBraceToken)) .WithMembers(new SyntaxList <MemberDeclarationSyntax>().AddProperties(schema, typeContext, namingRule)) .WithCloseBraceToken( Syntax.Token(SyntaxKind.CloseBraceToken)); typeContext.Add(className, classDeclaration); return(className); } return(null); }
public Type Compile(JSchema schema) { if (schema.Properties == null || schema.Properties.Count == 0) { return(typeof(object)); } var schemas = new[] { schema }; using (var stream = new MemoryStream()) { var result = CompileToInternal(schemas, stream); if (result.Success) { var typeName = _namingRule.GetClassName(schema, null); var assembly = Assembly.Load(stream.ToReadOnlyArray().ToArray()); return(assembly.GetTypes().First(t => t.Name == typeName)); } } return(null); }
public async Task <Type> GetModelTypeFromTemplateAsync(TemplateInfo templateInfo) { var schema = await _schemaProvider.GetSchemaFromTemplateAsync(templateInfo).ConfigureAwait(false); var typeName = string.Format("{0}.{1}.{2},{3}", _rootNamespace, _namingRule.GetNamespaceName(schema), _namingRule.GetClassName(schema, string.Empty), _assemblyName); try { return(Type.GetType(typeName, true)); } catch (TypeLoadException) { return(null); } }
public static string GenerateClass(JSchema schema, Dictionary<string, MemberDeclarationSyntax> typeContext, string propertyName, INamingRule namingRule) { if (schema.Type == JSchemaType.Object) { var className = namingRule.GetClassName(schema, propertyName); if (typeContext.ContainsKey(className)) return className; if (string.IsNullOrEmpty(className)) throw new Exception("Title not set"); var classDeclaration = Syntax.ClassDeclaration( Syntax.Identifier(className)) .WithModifiers(new SyntaxTokenList().Add(Syntax.Token(SyntaxKind.PublicKeyword))) .WithKeyword( Syntax.Token(SyntaxKind.ClassKeyword, Syntax.TriviaList(Syntax.Space))) .WithOpenBraceToken( Syntax.Token(SyntaxKind.OpenBraceToken)) .WithMembers(new SyntaxList<MemberDeclarationSyntax>().AddProperties(schema, typeContext, namingRule)) .WithCloseBraceToken( Syntax.Token(SyntaxKind.CloseBraceToken)); typeContext.Add(className, classDeclaration); return className; } return null; }