From b4198715e1a2dcf90b540c447b684483e6acc700 Mon Sep 17 00:00:00 2001 From: vwxyzh Date: Mon, 2 May 2016 22:45:03 -0700 Subject: [PATCH] fix attribute argument serialize for type array #280 --- .../Visitors/DefaultFilterVisitor.cs | 26 ++++++++-- .../Visitors/SymbolVisitorAdapter.cs | 6 +-- .../GenerateMetadataFromCSUnitTest.cs | 50 +++++++++++++++++++ 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.DocAsCode.Metadata.ManagedReference/Visitors/DefaultFilterVisitor.cs b/src/Microsoft.DocAsCode.Metadata.ManagedReference/Visitors/DefaultFilterVisitor.cs index 8180af28c84..83a3a791c30 100644 --- a/src/Microsoft.DocAsCode.Metadata.ManagedReference/Visitors/DefaultFilterVisitor.cs +++ b/src/Microsoft.DocAsCode.Metadata.ManagedReference/Visitors/DefaultFilterVisitor.cs @@ -63,10 +63,30 @@ private static bool CanVisitCore(ISymbol symbol, bool wantProtectedMember, IFilt return CanVisitCore(fieldSymbol, wantProtectedMember, outer); } - var typeSymbol = symbol as INamedTypeSymbol; - if (typeSymbol != null) + var namedTypeSymbol = symbol as INamedTypeSymbol; + if (namedTypeSymbol != null) { - return CanVisitCore(typeSymbol, wantProtectedMember, outer); + return CanVisitCore(namedTypeSymbol, wantProtectedMember, outer); + } + + var ts = symbol as ITypeSymbol; + if (ts != null) + { + switch (ts.TypeKind) + { + case TypeKind.Dynamic: + case TypeKind.TypeParameter: + return true; + case TypeKind.Unknown: + case TypeKind.Error: + return false; + case TypeKind.Array: + return outer.CanVisitApi(((IArrayTypeSymbol)ts).ElementType, wantProtectedMember, outer); + case TypeKind.Pointer: + return outer.CanVisitApi(((IPointerTypeSymbol)ts).PointedAtType, wantProtectedMember, outer); + default: + break; + } } if (symbol.DeclaredAccessibility != Accessibility.Public) diff --git a/src/Microsoft.DocAsCode.Metadata.ManagedReference/Visitors/SymbolVisitorAdapter.cs b/src/Microsoft.DocAsCode.Metadata.ManagedReference/Visitors/SymbolVisitorAdapter.cs index e0dad045490..1afb3ed945c 100644 --- a/src/Microsoft.DocAsCode.Metadata.ManagedReference/Visitors/SymbolVisitorAdapter.cs +++ b/src/Microsoft.DocAsCode.Metadata.ManagedReference/Visitors/SymbolVisitorAdapter.cs @@ -698,7 +698,7 @@ private ArgumentInfo GetArgumentInfo(TypedConstant arg) } if (arg.Value != null) { - var type = arg.Value as INamedTypeSymbol; + var type = arg.Value as ITypeSymbol; if (type != null) { if (!FilterVisitor.CanVisitApi(type)) @@ -714,7 +714,7 @@ private ArgumentInfo GetArgumentInfo(TypedConstant arg) private object GetConstantValueForArgumentInfo(TypedConstant arg) { - var type = arg.Value as INamedTypeSymbol; + var type = arg.Value as ITypeSymbol; if (type != null) { return AddSpecReference(type); @@ -760,7 +760,7 @@ private NamedArgumentInfo GetNamedArgumentInfo(KeyValuePair), typeof (Func<, >), typeof (Func)})] public class TestAttribute : Attribute, _Attribute", @class.Syntax.Content[SyntaxLanguage.CSharp]); + Assert.NotNull(@class.Attributes); + Assert.Equal(5, @class.Attributes.Count); + + Assert.Equal("System.SerializableAttribute", @class.Attributes[0].Type); + Assert.Equal("System.SerializableAttribute.#ctor", @class.Attributes[0].Constructor); + Assert.NotNull(@class.Attributes[0].Arguments); + Assert.Equal(0, @class.Attributes[0].Arguments.Count); + Assert.Null(@class.Attributes[0].NamedArguments); + + Assert.Equal("System.AttributeUsageAttribute", @class.Attributes[1].Type); + Assert.Equal("System.AttributeUsageAttribute.#ctor(System.AttributeTargets)", @class.Attributes[1].Constructor); + Assert.NotNull(@class.Attributes[1].Arguments); + Assert.Equal(1, @class.Attributes[1].Arguments.Count); + Assert.Equal("System.AttributeTargets", @class.Attributes[1].Arguments[0].Type); + Assert.Equal(32767, @class.Attributes[1].Arguments[0].Value); + Assert.NotNull(@class.Attributes[1].NamedArguments); + Assert.Equal(2, @class.Attributes[1].NamedArguments.Count); + Assert.Equal("Inherited", @class.Attributes[1].NamedArguments[0].Name); + Assert.Equal("System.Boolean", @class.Attributes[1].NamedArguments[0].Type); + Assert.Equal(true, @class.Attributes[1].NamedArguments[0].Value); + Assert.Equal("AllowMultiple", @class.Attributes[1].NamedArguments[1].Name); + Assert.Equal("System.Boolean", @class.Attributes[1].NamedArguments[1].Type); + Assert.Equal(true, @class.Attributes[1].NamedArguments[1].Value); + + Assert.Equal("System.ComponentModel.TypeConverterAttribute", @class.Attributes[2].Type); + Assert.Equal("System.ComponentModel.TypeConverterAttribute.#ctor(System.Type)", @class.Attributes[2].Constructor); + Assert.NotNull(@class.Attributes[2].Arguments); + Assert.Equal(1, @class.Attributes[2].Arguments.Count); + Assert.Equal("System.Type", @class.Attributes[2].Arguments[0].Type); + Assert.Equal("Test1.TestAttribute", @class.Attributes[2].Arguments[0].Value); + Assert.Null(@class.Attributes[2].NamedArguments); + + Assert.Equal("System.ComponentModel.TypeConverterAttribute", @class.Attributes[3].Type); + Assert.Equal("System.ComponentModel.TypeConverterAttribute.#ctor(System.Type)", @class.Attributes[3].Constructor); + Assert.NotNull(@class.Attributes[3].Arguments); + Assert.Equal(1, @class.Attributes[3].Arguments.Count); + Assert.Equal("System.Type", @class.Attributes[3].Arguments[0].Type); + Assert.Equal("Test1.TestAttribute[]", @class.Attributes[3].Arguments[0].Value); + Assert.Null(@class.Attributes[3].NamedArguments); + + Assert.Equal("Test1.TestAttribute", @class.Attributes[4].Type); + Assert.Equal("Test1.TestAttribute.#ctor(System.Object)", @class.Attributes[4].Constructor); + Assert.NotNull(@class.Attributes[4].Arguments); + Assert.Equal(1, @class.Attributes[4].Arguments.Count); + Assert.Equal("System.String", @class.Attributes[4].Arguments[0].Type); + Assert.Equal("test", @class.Attributes[4].Arguments[0].Value); + Assert.Null(@class.Attributes[4].NamedArguments); + var ctor = @class.Items[0]; Assert.NotNull(ctor); Assert.Equal(@"[Test(1)]