You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
.NET includes System.Runtime.CompilerServices.RuntimeFeature, a static class containing a number of string constants for various runtime feature, and an IsSupported(string) method for testing such support at run time.
The string constants identify the features that runtime has (and IsSupported will return true for those on that same runtime), so it is usable both at run time (by calling IsSupported) and at reflection time (by looking at the string constants in the class).
One of these features is NumericIntPtr, added in .NET 7 - it indicates that the runtime supports full numeric processing involving native integers. This means that nint/nuint are entirely synonymous with IntPrt/UIntPtr; in .NET 6, a NativeIntegerTypeAttribute was added to any type reference to IntPrt/UIntPtr that should be considered to be nint/nuint.
My use case is correct visualising a method signature; up to and including .NET 6, all was fine.
But now for a .NET 8 assembly (and also in a .NET 7one, but I didn't notice it then), a method involving nint is being shown as IntPtr because the attribute is no longer added to it.
So, I am faced with having an assembly I am processing and then needing to identify the presence of System.Runtime.CompilerServices.RuntimeFeature.NumericIntPtr in its corresponding core library.
if(ad.MainModule.TypeSystem.CoreLibrary is AssemblyNameReference anr){varcoreAsm= md.AssemblyResolver.Resolve(anr);varmd= coreAsm.MainModule;vartr=new TypeReference("System.Runtime.CompilerServices","RuntimeFeature", md, md.TypeSystem.CoreLibrary);varruntimeFeature= tr.Resolve();}
But that also failed.
In both cases, the Resolve() resolves the type, but in the runtime of the running process, not the referenced core library: anr is System.Runtime 8.0.0, but I get 4.0.0 when running on netfx and 6.0.0 when running on net6.0.
So: how the heck do I resolve a field in a type in the appropriate core library of an assembly?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
.NET includes
System.Runtime.CompilerServices.RuntimeFeature
, a static class containing a number of string constants for various runtime feature, and anIsSupported(string)
method for testing such support at run time.The string constants identify the features that runtime has (and
IsSupported
will returntrue
for those on that same runtime), so it is usable both at run time (by callingIsSupported
) and at reflection time (by looking at the string constants in the class).One of these features is
NumericIntPtr
, added in .NET 7 - it indicates that the runtime supports full numeric processing involving native integers. This means thatnint
/nuint
are entirely synonymous withIntPrt
/UIntPtr
; in .NET 6, aNativeIntegerTypeAttribute
was added to any type reference toIntPrt
/UIntPtr
that should be considered to benint
/nuint
.My use case is correct visualising a method signature; up to and including .NET 6, all was fine.
But now for a .NET 8 assembly (and also in a .NET 7one, but I didn't notice it then), a method involving
nint
is being shown asIntPtr
because the attribute is no longer added to it.So, I am faced with having an assembly I am processing and then needing to identify the presence of
System.Runtime.CompilerServices.RuntimeFeature.NumericIntPtr
in its corresponding core library.A naive approach did not work:
I tried resolving the core library reference:
But that also failed.
In both cases, the
Resolve()
resolves the type, but in the runtime of the running process, not the referenced core library:anr
is System.Runtime 8.0.0, but I get 4.0.0 when running on netfx and 6.0.0 when running onnet6.0
.So: how the heck do I resolve a field in a type in the appropriate core library of an assembly?
Beta Was this translation helpful? Give feedback.
All reactions