Skip to content

Commit

Permalink
Merge pull request #191 from stakx/serializableattribute
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmarbach committed Jun 17, 2020
2 parents a2a2490 + c802f4e commit 1e377fa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/PublicApiGenerator/ApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,23 @@ bool IsSpecialConstraint(GenericParameterConstraint constraint)
var attribute = GenerateCodeAttributeDeclaration(codeTypeModifier, customAttribute);
attributes.Add(attribute);
}

// so this is not very cool but at least all the attribute creation is in the same place
PopulateAttributesThatDontAppearInCustomAttributes(type, attributes);
}

static void PopulateAttributesThatDontAppearInCustomAttributes(ICustomAttributeProvider type,
CodeAttributeDeclarationCollection attributes)
{
if (type is TypeDefinition typeDefinition)
{
if (typeDefinition.Attributes.HasFlag(Mono.Cecil.TypeAttributes.Serializable))
{
var attribute = new CodeAttributeDeclaration("System.SerializableAttribute");
attribute.Name = AttributeNameBuilder.Get(attribute.Name);
attributes.Add(attribute);
}
}
}

static CodeAttributeDeclaration GenerateCodeAttributeDeclaration(Func<CodeTypeReference, CodeTypeReference> codeTypeModifier, CustomAttribute customAttribute)
Expand Down Expand Up @@ -667,7 +684,7 @@ static object FormatParameterConstant(ParameterDefinition parameter)
// this seems right for default
return "default";
}

return parameter.ParameterType.IsValueType ? "default" : "null";
}

Expand Down
21 changes: 20 additions & 1 deletion src/PublicApiGeneratorTests/Class_attributes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using PublicApiGenerator;
using PublicApiGeneratorTests.Examples;
using System;
using Xunit;

namespace PublicApiGeneratorTests
Expand Down Expand Up @@ -294,6 +294,20 @@ public class ClassWithMultipleAttributes
}
}", options);
}

[Fact]
public void Should_include_Serializable_attribute()
{
AssertPublicApi<ClassWithSerializableAttribute>(
@"namespace PublicApiGeneratorTests.Examples
{
[System.Serializable]
public class ClassWithSerializableAttribute
{
public ClassWithSerializableAttribute() { }
}
}");
}
}

// ReSharper disable UnusedMember.Global
Expand Down Expand Up @@ -403,6 +417,11 @@ public class ClassWithAttributeWithMultipleUsagesSupport
public class ClassWithInternalAttribute
{
}

[Serializable]
public class ClassWithSerializableAttribute
{
}
}
// ReSharper restore ClassNeverInstantiated.Global
// ReSharper restore UnusedMember.Global
Expand Down

0 comments on commit 1e377fa

Please sign in to comment.