-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for constructors in class generation for reflection
Class AST node factory was ignoring constructors. Added support and added tests too. Targets #41
- Loading branch information
1 parent
4c0efd1
commit a41f6f9
Showing
12 changed files
with
300 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/Reflection/factories/ConstructorDeclarationSyntaxFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/// <summary> | ||
/// ConstructorDeclarationSyntaxFactory.cs | ||
/// Andrea Tino - 2017 | ||
/// </summary> | ||
|
||
namespace Rosetta.Reflection.Factories | ||
{ | ||
using System; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
using Rosetta.Reflection.Helpers; | ||
using Rosetta.Reflection.Proxies; | ||
|
||
/// <summary> | ||
/// Factory for generating a <see cref="ConstructorDeclarationSyntax"/>. | ||
/// </summary> | ||
public class ConstructorDeclarationSyntaxFactory | ||
{ | ||
private ITypeInfoProxy classInfo; | ||
private IConstructorInfoProxy ctorInfo; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConstructorDeclarationSyntaxFactory"/> class. | ||
/// </summary> | ||
/// <param name="ctorInfo"></param> | ||
/// <param name="classInfo"></param> | ||
public ConstructorDeclarationSyntaxFactory(IConstructorInfoProxy ctorInfo, ITypeInfoProxy classInfo) | ||
{ | ||
if (ctorInfo == null) | ||
{ | ||
throw new ArgumentNullException(nameof(ctorInfo)); | ||
} | ||
|
||
if (classInfo == null) | ||
{ | ||
throw new ArgumentNullException(nameof(classInfo)); | ||
} | ||
|
||
this.ctorInfo = ctorInfo; | ||
this.classInfo = classInfo; | ||
} | ||
|
||
/// <summary> | ||
/// Creates the <see cref="ConstructorDeclarationSyntax"/>. | ||
/// </summary> | ||
/// <returns></returns> | ||
public ConstructorDeclarationSyntax Create() | ||
{ | ||
var ctorDeclaration = SyntaxFactory.ConstructorDeclaration(this.classInfo.Name); | ||
|
||
// Defining accessibility | ||
ctorDeclaration = ctorDeclaration.AddModifiers(SyntaxFactory.Token(new Visibility(this.ctorInfo).Token)); | ||
|
||
// Defining parameters | ||
var parameters = this.ctorInfo.Parameters; | ||
|
||
if (parameters != null) | ||
{ | ||
foreach (var parameter in parameters) | ||
{ | ||
ctorDeclaration = ctorDeclaration.AddParameterListParameters(SyntaxFactory.Parameter(SyntaxFactory.Identifier(parameter.Name)) | ||
.WithType(SyntaxFactory.ParseTypeName(parameter.ParameterType.FullName))); | ||
} | ||
} | ||
|
||
// Dummy body | ||
ctorDeclaration = ctorDeclaration.WithBody(DummyBody.GenerateForMerhod()); | ||
|
||
return ctorDeclaration; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.