1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
using System.Collections.Generic;
using System.Reflection.CustomAttributesTests.Data;
// Need to disable warning related to CLS Compliance as using Array as custom attribute is not CLS compliant
#pragma warning disable 3016
namespace System.Reflection.Tests
{
public class FieldInfoTestClass
{
public FieldInfoTestClass()
{
}
[Attr(77, name = "AttrSimple"),
Int32Attr(77, name = "Int32AttrSimple"),
Int64Attr(77, name = "Int64AttrSimple"),
StringAttr("hello", name = "StringAttrSimple"),
EnumAttr(MyColorEnum.RED, name = "EnumAttrSimple"),
TypeAttr(typeof(object), name = "TypeAttrSimple")]
public string MyField = "MyField";
public int Prop
{
get { return 0; }
set { }
}
}
// Test class for Custom Attribute Test
public class FieldInfoCustomAttributeTests
{
[Theory]
[InlineData(typeof(Int32Attr), "[System.Reflection.CustomAttributesTests.Data.Int32Attr((Int32)77, name = \"Int32AttrSimple\")]")]
[InlineData(typeof(Int64Attr), "[System.Reflection.CustomAttributesTests.Data.Int64Attr((Int64)77, name = \"Int64AttrSimple\")]")]
[InlineData(typeof(StringAttr), "[System.Reflection.CustomAttributesTests.Data.StringAttr(\"hello\", name = \"StringAttrSimple\")]")]
[InlineData(typeof(EnumAttr), "[System.Reflection.CustomAttributesTests.Data.EnumAttr((System.Reflection.CustomAttributesTests.Data.MyColorEnum)1, name = \"EnumAttrSimple\")]")]
[InlineData(typeof(TypeAttr), "[System.Reflection.CustomAttributesTests.Data.TypeAttr(typeof(System.Object), name = \"TypeAttrSimple\")]")]
[InlineData(typeof(Attr), "[System.Reflection.CustomAttributesTests.Data.Attr((Int32)77, name = \"AttrSimple\")]")]
public static void TestCustomAttributeDetails(Type type, string attributeStr)
{
FieldInfo fi = GetField("MyField");
Assert.Contains(fi.CustomAttributes, attr => attr.AttributeType.Equals(type) && attr.ToString().Equals(attributeStr));
}
// Helper method to get field from Type type
private static FieldInfo GetField(string fieldName)
{
Type t = typeof(FieldInfoTestClass);
TypeInfo ti = t.GetTypeInfo();
IEnumerator<FieldInfo> alldefinedFields = ti.DeclaredFields.GetEnumerator();
FieldInfo fi = null, found = null;
while (alldefinedFields.MoveNext())
{
fi = alldefinedFields.Current;
if (fi.Name.Equals(fieldName))
{
//found type
found = fi;
break;
}
}
return found;
}
}
}
|