Skip to content

Commit

Permalink
Merge pull request #69 from wwwDayDream/feat_allow-public-fields
Browse files Browse the repository at this point in the history
Added public field option that defaults to false
  • Loading branch information
wwwDayDream authored May 20, 2024
2 parents ef3a9d8 + 3204156 commit d06ca4a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CessilCellsCeaChells/CeaChore/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace CessilCellsCeaChells.CeaChore;

public class RequiresFieldAttribute : RequiresAttribute {
[UsedImplicitly] public RequiresFieldAttribute(Type targetType, string fieldName, Type fieldType) {}
[UsedImplicitly] public RequiresFieldAttribute(Type targetType, string fieldName, Type fieldType, bool isPublic = false) {}
}

public class RequiresPropertyAttribute : RequiresAttribute {
Expand Down
7 changes: 5 additions & 2 deletions CessilCellsCeaChells/Merges/FieldMerge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace CessilCellsCeaChells.Merges;
internal class FieldMerge(CustomAttribute attribute) : CessilMerge((TypeReference)attribute.ConstructorArguments[0].Value) {
private readonly string FieldName = (string)attribute.ConstructorArguments[1].Value;
private readonly TypeReference FieldType = (TypeReference)attribute.ConstructorArguments[2].Value;
private readonly bool IsPublic = attribute.ConstructorArguments.Count > 3 && (bool)attribute.ConstructorArguments[3].Value;

internal override CustomAttribute ConvertToAttribute(AssemblyDefinition into)
{
Expand All @@ -19,15 +20,17 @@ internal override CustomAttribute ConvertToAttribute(AssemblyDefinition into)
ConstructorArguments = {
new CustomAttributeArgument(typeTypeRef, targetTypeRef),
new CustomAttributeArgument(into.MainModule.TypeSystem.String, FieldName),
new CustomAttributeArgument(typeTypeRef, fieldTypeTypeRef)
new CustomAttributeArgument(typeTypeRef, fieldTypeTypeRef),
new CustomAttributeArgument(into.MainModule.TypeSystem.Boolean, IsPublic)
}
};
}

internal override bool TryMergeInto(TypeDefinition typeDefinition, out IMemberDefinition? memberDefinition)
{
memberDefinition = default;
var didCreate = CessilHelper.TryCreateField(typeDefinition, FieldName, GetOrImportTypeReference(typeDefinition.Module, FieldType), out var fieldDefinition);
var didCreate = CessilHelper.TryCreateField(typeDefinition, FieldName, GetOrImportTypeReference(typeDefinition.Module, FieldType),
out var fieldDefinition, IsPublic ? FieldAttributes.Public : FieldAttributes.Private);
if (didCreate)
memberDefinition = fieldDefinition;
return didCreate;
Expand Down

0 comments on commit d06ca4a

Please sign in to comment.