-
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.
Reflection to support enums. Added tests. Targets #41
- Loading branch information
1 parent
24c6eef
commit f35e782
Showing
24 changed files
with
745 additions
and
60 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/// <summary> | ||
/// IFieldInfoProxy.cs | ||
/// Andrea Tino - 2017 | ||
/// </summary> | ||
|
||
namespace Rosetta.Reflection.Proxies | ||
{ | ||
using System; | ||
|
||
/// <summary> | ||
/// Abstracts field info API. | ||
/// </summary> | ||
public interface IFieldInfoProxy | ||
{ | ||
/// <summary> | ||
/// Gets the name of the current member. | ||
/// </summary> | ||
string Name { get; } | ||
|
||
/// <summary> | ||
/// Gets the Type of this field. | ||
/// </summary> | ||
ITypeProxy FieldType { get; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the field is visible only within its class | ||
/// and derived classes. | ||
/// </summary> | ||
bool IsFamily { get; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether this member is private. | ||
/// </summary> | ||
bool IsPrivate { get; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether this member is public. | ||
/// </summary> | ||
bool IsPublic { get; } | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
src/Reflection/assembly-proxying/mono/MonoFieldInfoProxy.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,61 @@ | ||
/// <summary> | ||
/// MonoFieldInfoProxy.cs | ||
/// Andrea Tino - 2017 | ||
/// </summary> | ||
|
||
namespace Rosetta.Reflection.Proxies | ||
{ | ||
using System; | ||
using System.Diagnostics; | ||
|
||
using Mono.Cecil; | ||
|
||
/// <summary> | ||
/// Proxy for member incapsulating Mono. | ||
/// </summary> | ||
[DebuggerDisplay("Field {Name,nq}", Name = "MonoFieldInfoProxy {Name,nq} (Mono proxy)")] | ||
public class MonoFieldInfoProxy : IFieldInfoProxy | ||
{ | ||
private readonly FieldDefinition fieldDefinition; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MonoFieldInfoProxy"/> class. | ||
/// </summary> | ||
/// <param name="monoFieldDefinition"></param> | ||
public MonoFieldInfoProxy(FieldDefinition monoFieldDefinition) | ||
{ | ||
if (monoFieldDefinition == null) | ||
{ | ||
throw new ArgumentNullException(nameof(monoFieldDefinition)); | ||
} | ||
|
||
this.fieldDefinition = monoFieldDefinition; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the name of the parameter. | ||
/// </summary> | ||
public string Name => this.fieldDefinition.Name; | ||
|
||
/// <summary> | ||
/// Gets the Type of this field. | ||
/// </summary> | ||
public ITypeProxy FieldType => new MonoTypeProxy(this.fieldDefinition.FieldType); | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the field or constructor is visible only within its class | ||
/// and derived classes. | ||
/// </summary> | ||
public bool IsFamily => this.fieldDefinition.IsFamily; | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether this member is private. | ||
/// </summary> | ||
public bool IsPrivate => this.fieldDefinition.IsPrivate; | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether this member is public. | ||
/// </summary> | ||
public bool IsPublic => this.fieldDefinition.IsPublic; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/// <summary> | ||
/// EnumDeclarationSyntaxFactory.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="EnumDeclarationSyntax"/>. | ||
/// </summary> | ||
public class EnumDeclarationSyntaxFactory : ISyntaxFactory | ||
{ | ||
private readonly ITypeInfoProxy enumInfo; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EnumDeclarationSyntaxFactory"/> class. | ||
/// </summary> | ||
/// <param name="classInfo"></param> | ||
public EnumDeclarationSyntaxFactory(ITypeInfoProxy enumInfo) | ||
{ | ||
if (enumInfo == null) | ||
{ | ||
throw new ArgumentNullException(nameof(enumInfo)); | ||
} | ||
|
||
this.enumInfo = enumInfo; | ||
} | ||
|
||
/// <summary> | ||
/// Creates the <see cref="InterfaceDeclarationSyntax"/>. | ||
/// </summary> | ||
/// <returns></returns> | ||
public SyntaxNode Create() | ||
{ | ||
var enumNode = SyntaxFactory.EnumDeclaration(this.enumInfo.Name); | ||
|
||
// Defining accessibility | ||
var visibility = new Visibility(this.enumInfo).Token; | ||
if (visibility != SyntaxKind.None) | ||
{ | ||
enumNode = enumNode.AddModifiers(SyntaxFactory.Token(visibility)); | ||
} | ||
|
||
// Values | ||
var values = this.enumInfo.DeclaredFields; | ||
|
||
if (values != null) | ||
{ | ||
foreach (var value in values) | ||
{ | ||
enumNode = enumNode.AddMembers(SyntaxFactory.EnumMemberDeclaration(value.Name)); | ||
} | ||
} | ||
|
||
return enumNode; | ||
} | ||
} | ||
} |
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,56 @@ | ||
/// <summary> | ||
/// FieldDeclarationSyntaxFactory.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="FieldDeclarationSyntax"/>. | ||
/// </summary> | ||
public class FieldDeclarationSyntaxFactory : ISyntaxFactory | ||
{ | ||
private readonly IFieldInfoProxy fieldInfo; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FieldDeclarationSyntaxFactory"/> class. | ||
/// </summary> | ||
/// <param name="propertyInfo"></param> | ||
/// <param name="withBody"></param> | ||
public FieldDeclarationSyntaxFactory(IFieldInfoProxy fieldInfo) | ||
{ | ||
if (fieldInfo == null) | ||
{ | ||
throw new ArgumentNullException(nameof(fieldInfo)); | ||
} | ||
|
||
this.fieldInfo = fieldInfo; | ||
} | ||
|
||
/// <summary> | ||
/// Creates the <see cref="PropertyDeclarationSyntax"/>. | ||
/// </summary> | ||
/// <returns></returns> | ||
public SyntaxNode Create() | ||
{ | ||
var varDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.ParseTypeName(this.fieldInfo.FieldType.FullName), | ||
new SeparatedSyntaxList<VariableDeclaratorSyntax>() | ||
.Add(SyntaxFactory.VariableDeclarator(this.fieldInfo.Name))); | ||
var fieldDeclaration = SyntaxFactory.FieldDeclaration(varDeclaration); | ||
|
||
// Defining accessibility | ||
fieldDeclaration = fieldDeclaration.AddModifiers(SyntaxFactory.Token(new Visibility(this.fieldInfo).Token)); | ||
|
||
return fieldDeclaration; | ||
} | ||
} | ||
} |
Oops, something went wrong.