Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 49c64c8

Browse files
committed
Fix AttributesTests on .NET Core and PCL
1 parent 9afe7e6 commit 49c64c8

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/ServiceStack.Text/ReflectionExtensions.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public static bool IsInstanceOf(this Type type, Type thisOrBaseType)
118118

119119
type = type.BaseType();
120120
}
121+
121122
return false;
122123
}
123124

@@ -1185,7 +1186,6 @@ public static Type AddAttributes(this Type type, params Attribute[] attrs)
11851186
}
11861187

11871188
typeAttrs.AddRange(attrs);
1188-
11891189
return type;
11901190
}
11911191

@@ -1342,17 +1342,24 @@ public static object[] AllAttributes(this FieldInfo fieldInfo, Type attrType)
13421342

13431343
public static object[] AllAttributes(this Type type)
13441344
{
1345-
#if (NETFX_CORE || PCL || NETSTANDARD)
1345+
#if (NETFX_CORE || PCL)
13461346
return type.GetTypeInfo().GetCustomAttributes(true).ToArray();
1347+
#elif NETSTANDARD
1348+
return type.GetTypeInfo().GetCustomAttributes(true).Union(type.GetRuntimeAttributes()).ToArray();
13471349
#else
13481350
return type.GetCustomAttributes(true).Union(type.GetRuntimeAttributes()).ToArray();
13491351
#endif
13501352
}
13511353

13521354
public static object[] AllAttributes(this Type type, Type attrType)
13531355
{
1354-
#if (NETFX_CORE || PCL || NETSTANDARD)
1355-
return type.GetTypeInfo().GetCustomAttributes(true).Where(x => attrType.IsInstanceOf(x.GetType())).ToArray();
1356+
#if (NETFX_CORE || PCL)
1357+
return type.GetTypeInfo().GetCustomAttributes(true).Where(x => x.GetType().IsInstanceOf(attrType)).ToArray();
1358+
#elif NETSTANDARD
1359+
return type.GetTypeInfo().GetCustomAttributes(true)
1360+
.Where(x => x.GetType().IsInstanceOf(attrType))
1361+
.Union(type.GetRuntimeAttributes())
1362+
.ToArray();
13561363
#else
13571364
return type.GetCustomAttributes(true).Union(type.GetRuntimeAttributes()).ToArray();
13581365
#endif
@@ -1408,8 +1415,12 @@ public static TAttr[] AllAttributes<TAttr>(this Type type)
14081415
where TAttr : Attribute
14091416
#endif
14101417
{
1411-
#if (NETFX_CORE || PCL || NETSTANDARD)
1418+
#if (NETFX_CORE || PCL)
14121419
return type.GetTypeInfo().GetCustomAttributes<TAttr>(true).ToArray();
1420+
#elif NETSTANDARD
1421+
return type.GetTypeInfo().GetCustomAttributes<TAttr>(true)
1422+
.Union(type.GetRuntimeAttributes<TAttr>())
1423+
.ToArray();
14131424
#else
14141425
return type.GetCustomAttributes(typeof(TAttr), true)
14151426
.OfType<TAttr>()

tests/ServiceStack.Text.Tests/AttributeTests.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ public void Does_get_Multiple_RouteDefault_Attributes()
3737
{
3838
// AllAttributes<T>() makes this call to get attrs
3939
#if NETCORE
40-
var referenceGeneric = typeof(DefaultWithMultipleAttributes).GetTypeInfo().GetCustomAttributes(true)
41-
.Where(x => typeof(RouteDefaultAttribute).IsInstanceOf(x.GetType())).ToArray()
42-
.OfType<RouteDefaultAttribute>();
40+
var referenceGeneric =
41+
typeof(DefaultWithMultipleAttributes).GetTypeInfo().GetCustomAttributes(true)
42+
.Where(x => x.GetType().IsInstanceOf(typeof(RouteDefaultAttribute)))
43+
.OfType<RouteDefaultAttribute>();
4344
#else
4445
var referenceGeneric =
4546
typeof(DefaultWithMultipleAttributes).GetCustomAttributes(typeof(RouteDefaultAttribute), true)
@@ -50,8 +51,9 @@ public void Does_get_Multiple_RouteDefault_Attributes()
5051

5152
// AllAttributes() makes this call to get attrs
5253
#if NETCORE
53-
var reference = typeof(DefaultWithMultipleAttributes).GetTypeInfo().GetCustomAttributes(true)
54-
.Where(x => typeof(RouteDefaultAttribute).IsInstanceOf(x.GetType())).ToArray();
54+
var reference =
55+
typeof(DefaultWithMultipleAttributes).GetTypeInfo().GetCustomAttributes(true)
56+
.Where(x => x.GetType().IsInstanceOf(typeof(RouteDefaultAttribute))).ToArray();
5557
#else
5658
var reference =
5759
typeof(DefaultWithMultipleAttributes).GetCustomAttributes(typeof(RouteDefaultAttribute), true);

0 commit comments

Comments
 (0)