System.Runtime.CompilerServices in .NET Standard library

Currently I am in the process of converting a .NET 4.5 class library to a .NET Core class library, referencing .NETStandard v1.6.

In part of my code (relying heavily on reflection) I need to determine whether an object is of type Closure, which is located in the System.Runtime.CompilerServices namespace. This namespace and type is not available in .NETStandard v1.6.

  • Did this type move, or is it no longer accessible?
  • Since my code relied on it, what is an alternative in case it is not available in .NETStandard?

The specific code relying on Closure determines the parameter types of a delegate, skipping the compiler generated Closure ones.

Delegate toWrap;
MethodInfo toWrapInfo = toWrap.GetMethodInfo();
var toWrapArguments = toWrapInfo.GetParameters() // Closure argument isn't an actual argument, but added by the compiler. .SkipWhile( p => p.ParameterType == typeof( Closure ) ) .Select( p => p.ParameterType );
4

1 Answer

In .Net Core 1.0, Closure exists in the System.Linq.Expressions assembly in the System.Linq.Expressions package, but it's not exposed in a reference assembly. This means it's just an implementation detail in Core (and could for example vanish or move in a future version). It also means you can't reference it at compile time (like you did in .Net Framework), but you can retrieve it using reflection at runtime (don't forget using System.Reflection; for GetTypeInfo()):

Type closureType = typeof(Expression).GetTypeInfo().Assembly .GetType("System.Runtime.CompilerServices.Closure");

In .Net Framework, Closure is in a different assembly (namely, System.Core), but so is Expression, so this code should work on both.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like