Skip to content

Commit

Permalink
Merge pull request #84 from JakeGinnivan/readonly-struct
Browse files Browse the repository at this point in the history
Rudimentary readonly struct support
  • Loading branch information
danielmarbach committed Aug 22, 2019
2 parents 230291b + ecb5c95 commit 36a3876
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/PublicApiGenerator/ApiGenerator.cs
Expand Up @@ -209,18 +209,32 @@ static CodeTypeDeclaration CreateTypeDeclaration(TypeDefinition publicType, stri
// correct C#, but it's good enough for our API outline
var name = publicType.Name;

var isStruct = publicType.IsValueType && !publicType.IsPrimitive && !publicType.IsEnum;

var @readonly = isStruct && publicType.CustomAttributes.Any(a =>
a.AttributeType.FullName == "System.Runtime.CompilerServices.IsReadOnlyAttribute");

var index = name.IndexOf('`');
if (index != -1)
name = name.Substring(0, index);
var declaration = new CodeTypeDeclaration(@static ? "static " + name : name)

var declarationName = string.Empty;
if (@readonly)
declarationName += "readonly ";
if (@static)
declarationName += "static ";

declarationName += name;

var declaration = new CodeTypeDeclaration(declarationName)
{
CustomAttributes = CreateCustomAttributes(publicType, excludeAttributes),
// TypeAttributes must be specified before the IsXXX as they manipulate TypeAttributes!
TypeAttributes = attributes,
IsClass = publicType.IsClass,
IsEnum = publicType.IsEnum,
IsInterface = publicType.IsInterface,
IsStruct = publicType.IsValueType && !publicType.IsPrimitive && !publicType.IsEnum,
IsStruct = isStruct,
};

if (declaration.IsInterface && publicType.BaseType != null)
Expand Down Expand Up @@ -417,6 +431,7 @@ static string ConvertAttributeToCode(Func<CodeTypeReference, CodeTypeReference>
"System.Runtime.CompilerServices.ExtensionAttribute",
"System.Runtime.CompilerServices.RuntimeCompatibilityAttribute",
"System.Runtime.CompilerServices.IteratorStateMachineAttribute",
"System.Runtime.CompilerServices.IsReadOnlyAttribute",
"System.Reflection.DefaultMemberAttribute",
"System.Diagnostics.DebuggableAttribute",
"System.Diagnostics.DebuggerNonUserCodeAttribute",
Expand Down
1 change: 1 addition & 0 deletions src/PublicApiGeneratorTests/PublicApiGeneratorTests.csproj
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net452</TargetFrameworks>
<NoWarn>$(NoWarn);CS0067</NoWarn>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
28 changes: 28 additions & 0 deletions src/PublicApiGeneratorTests/Struct_readonly.cs
@@ -0,0 +1,28 @@
using System;
using PublicApiGeneratorTests.Examples;
using Xunit;

namespace PublicApiGeneratorTests
{
public class Struct_readonly : ApiGeneratorTestsBase
{
[Fact]
public void Should_output()
{
AssertPublicApi<ReadonlyStruct>(
@"namespace PublicApiGeneratorTests.Examples
{
public struct readonly ReadonlyStruct { }
}");
}
}

// ReSharper disable ClassNeverInstantiated.Global
namespace Examples
{
public readonly struct ReadonlyStruct
{
}
}
// ReSharper restore ClassNeverInstantiated.Global
}

0 comments on commit 36a3876

Please sign in to comment.