Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rudimentary readonly struct support #84

Merged
merged 1 commit into from Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}