Skip to content

Commit 4342f09

Browse files
Chris Martinezcommonsensesoftware
Chris Martinez
authored andcommitted
Use typeof( ActionResult<> ) instead of Reflection
1 parent f7bff84 commit 4342f09

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/Common.OData.ApiExplorer/AspNet.OData/TypeExtensions.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
using System.Net.Http;
1616
using System.Reflection;
1717
using System.Reflection.Emit;
18-
using static System.StringComparison;
1918
#if WEBAPI
2019
using IActionResult = System.Web.Http.IHttpActionResult;
2120
#endif
@@ -26,11 +25,14 @@
2625
public static partial class TypeExtensions
2726
{
2827
static readonly Type VoidType = typeof( void );
29-
static readonly Type ActionResultType = typeof( IActionResult );
3028
static readonly Type HttpResponseType = typeof( HttpResponseMessage );
3129
static readonly Type IEnumerableOfT = typeof( IEnumerable<> );
3230
static readonly Type ODataValueOfT = typeof( ODataValue<> );
3331
static readonly Type SingleResultOfT = typeof( SingleResult<> );
32+
static readonly Type ActionResultType = typeof( IActionResult );
33+
#if !WEBAPI
34+
static readonly Type ActionResultOfT = typeof( ActionResult<> );
35+
#endif
3436

3537
/// <summary>
3638
/// Substitutes the specified type, if required.
@@ -157,7 +159,11 @@ internal static Type ExtractInnerType( this Type type )
157159

158160
var typeArg = typeArgs[0];
159161

160-
if ( typeDef.IsDelta() || typeDef.Equals( ODataValueOfT ) || typeDef.IsActionResult() || typeDef.Equals( SingleResultOfT ) )
162+
#if WEBAPI
163+
if ( typeDef.IsDelta() || typeDef.IsODataValue() || typeDef.IsSingleResult() )
164+
#else
165+
if ( typeDef.IsDelta() || typeDef.IsODataValue() || typeDef.IsSingleResult() || typeDef.IsActionResult() )
166+
#endif
161167
{
162168
return typeArg;
163169
}
@@ -189,7 +195,11 @@ static bool IsSubstitutableGeneric( Type type, Stack<Type> openTypes, out Type i
189195

190196
var typeArg = typeArgs[0];
191197

192-
if ( typeDef.Equals( IEnumerableOfT ) || typeDef.IsDelta() || typeDef.Equals( ODataValueOfT ) || typeDef.IsActionResult() || typeDef.Equals( SingleResultOfT ) )
198+
#if WEBAPI
199+
if ( typeDef.Equals( IEnumerableOfT ) || typeDef.IsDelta() || typeDef.IsODataValue() || typeDef.IsSingleResult() )
200+
#else
201+
if ( typeDef.Equals( IEnumerableOfT ) || typeDef.IsDelta() || typeDef.IsODataValue() || typeDef.IsSingleResult() || typeDef.IsActionResult() )
202+
#endif
193203
{
194204
innerType = typeArg;
195205
}
@@ -284,12 +294,18 @@ internal static bool IsEnumerable( this Type type, out Type itemType )
284294
return false;
285295
}
286296

287-
static bool IsActionResult( this Type type ) =>
288-
type.IsGenericType &&
289-
type.GetGenericTypeDefinition().FullName.Equals( "Microsoft.AspNetCore.Mvc.ActionResult`1", Ordinal );
297+
static bool IsSingleResult( this Type type ) => type.Is( SingleResultOfT );
298+
299+
static bool IsODataValue( this Type type ) => type.Is( ODataValueOfT );
290300

291-
static bool IsSingleResult( this Type type ) => SingleResultOfT.IsAssignableFrom( type );
301+
static bool Is( this Type type, Type typeDefinition ) => type.IsGenericType && type.GetGenericTypeDefinition().Equals( typeDefinition );
292302

303+
#if WEBAPI
304+
static bool ShouldExtractInnerType( this Type type ) => type.IsDelta() || type.IsSingleResult();
305+
#else
293306
static bool ShouldExtractInnerType( this Type type ) => type.IsDelta() || type.IsSingleResult() || type.IsActionResult();
307+
308+
static bool IsActionResult( this Type type ) => type.Is( ActionResultOfT );
309+
#endif
294310
}
295311
}

0 commit comments

Comments
 (0)