Skip to content

Commit

Permalink
fix attribute argument serialize for type array #280
Browse files Browse the repository at this point in the history
  • Loading branch information
vwxyzh committed May 3, 2016
1 parent debefb2 commit b419871
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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);
Expand Down Expand Up @@ -760,7 +760,7 @@ private NamedArgumentInfo GetNamedArgumentInfo(KeyValuePair<string, TypedConstan
}
else if (arg.Value != null)
{
var type = arg.Value as INamedTypeSymbol;
var type = arg.Value as ITypeSymbol;
if (type != null)
{
if (!FilterVisitor.CanVisitApi(type))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2176,6 +2176,7 @@ namespace Test1
[Serializable]
[AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
[TypeConverter(typeof(TestAttribute))]
[TypeConverter(typeof(TestAttribute[]))]
[Test(""test"")]
[Test(new int[]{1,2,3})]
[Test(new object[]{null, ""abc"", 'd', 1.1f, 1.2, (sbyte)2, (byte)3, (short)4, (ushort)5, 6, 7u, 8l, 9ul, new int[]{ 10, 11, 12 }})]
Expand All @@ -2198,12 +2199,61 @@ public TestAttribute([Test(3), Test(4)] object obj){}
Assert.Equal(@"[Serializable]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter | AttributeTargets.All, Inherited = true, AllowMultiple = true)]
[TypeConverter(typeof (TestAttribute))]
[TypeConverter(typeof (TestAttribute[]))]
[Test(""test"")]
[Test(new int[]{1, 2, 3})]
[Test(new object[]{null, ""abc"", 'd', 1.1F, 1.2, (sbyte)2, (byte)3, (short)4, (ushort)5, 6, 7U, 8L, 9UL, new int[]{10, 11, 12}})]
[Test(new Type[]{typeof (Func<>), typeof (Func<, >), typeof (Func<string, string>)})]
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)]
Expand Down

0 comments on commit b419871

Please sign in to comment.