/// <summary> /// Renders the class declaration. /// </summary> public override IEnumerable <MemberDeclarationSyntax> Render() { // get property declarations var declarations = Transform.Members.SelectMany(m => m.PropertyMemberRenderer.GetDeclarationCode()).ToArray(); // generate class yield return(SyntaxHelper.GenerateNamespace(Transform.TargetType.Namespace, new MemberDeclarationSyntax[] { SyntaxHelper.GenerateClass(Transform.TargetType.Name, new [] { SyntaxKind.PublicKeyword, SyntaxKind.PartialKeyword }, declarations) }, GetUsingsForNamespace())); }
/// <summary> /// Renders the code. /// </summary> public override IEnumerable <MemberDeclarationSyntax> Render() { // TransformToSource method var method = SyntaxHelper.GenerateExtensionMethod("TransformToSource", Transform.SourceType.FullName, new [] { SyntaxHelper.GenerateMethodParameter("target", Transform.TargetType.FullName, true) }, new[] { SyntaxHelper.GenerateAttribute( typeof(DtoConvertFunctionAttribute), SyntaxFactory.TypeOfExpression(SyntaxHelper.GenerateTypeSyntax(Transform.TargetType)), SyntaxFactory.TypeOfExpression(SyntaxHelper.GenerateTypeSyntax(Transform.SourceType)), SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression) ) }) .WithBody(SyntaxFactory.Block( new StatementSyntax[] { SyntaxHelper.GenerateStaticMethodCall("EnsureInitialized", typeof(PropertyConverter).FullName), SyntaxHelper.GenerateVariableDeclarationAndObjectCreationStatement("source", Transform.SourceType.FullName) }.Concat( Transform.Members.SelectMany(m => m.PropertyMemberRenderer.GetTransformBackCode()).ToArray() ).Concat(new[] { SyntaxFactory.ReturnStatement(SyntaxFactory.ParseName("source")) }) )); // PopulateSource method var method2 = SyntaxHelper.GenerateExtensionMethod("PopulateSource", null, new[] { SyntaxHelper.GenerateMethodParameter("target", Transform.TargetType.FullName, true), SyntaxHelper.GenerateMethodParameter("source", Transform.SourceType.FullName, false) }, new[] { SyntaxHelper.GenerateAttribute( typeof(DtoConvertFunctionAttribute), SyntaxFactory.TypeOfExpression(SyntaxHelper.GenerateTypeSyntax(Transform.TargetType)), SyntaxFactory.TypeOfExpression(SyntaxHelper.GenerateTypeSyntax(Transform.SourceType)), SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression) ) }) .WithBody(SyntaxFactory.Block( new StatementSyntax[] { SyntaxHelper.GenerateStaticMethodCall("EnsureInitialized", typeof(PropertyConverter).FullName), }.Concat( Transform.Members.SelectMany(m => m.PropertyMemberRenderer.GetTransformBackCode()).ToArray() ) )); // generate the static class var className = Transform.TargetType.Name + "Extensions"; yield return(SyntaxHelper.GenerateNamespace(Transform.TargetType.Namespace, new MemberDeclarationSyntax[] { SyntaxHelper.GenerateClass( className, new[] { SyntaxKind.PublicKeyword, SyntaxKind.StaticKeyword }, new MemberDeclarationSyntax[] { method, method2 }, new[] { SyntaxHelper.GenerateAttribute(typeof(DtoGeneratedAttribute)) } ) }, GetUsingsForNamespace())); }