Support Microsoft.Bcl.AsyncInterfaces (#151)

* Support Microsoft.Bcl.AsyncInterfaces

* Update Microsoft.Bcl.AsyncInterfaces version

* Use Condition
This commit is contained in:
彭伟 2021-11-03 10:38:28 +08:00 committed by GitHub
parent 4bdf8e3a2b
commit b9ceb93fe0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 11 deletions

View File

@ -3,9 +3,6 @@
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' != 'netstandard2.0' and !$(TargetFramework.StartsWith('netcoreapp2')) and !$(TargetFramework.StartsWith('net4'))">
<DefineConstants>$(DefineConstants);HAS_ASYNC_ENUMERATOR</DefineConstants>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="All" /> <PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="All" />
</ItemGroup> </ItemGroup>

View File

@ -20,6 +20,24 @@ namespace System.Diagnostics
public partial class EnhancedStackTrace public partial class EnhancedStackTrace
{ {
private static readonly Type? StackTraceHiddenAttributeType = Type.GetType("System.Diagnostics.StackTraceHiddenAttribute", false); private static readonly Type? StackTraceHiddenAttributeType = Type.GetType("System.Diagnostics.StackTraceHiddenAttribute", false);
private static readonly Type? AsyncIteratorStateMachineAttributeType = Type.GetType("System.Runtime.CompilerServices.AsyncIteratorStateMachineAttribute", false);
static EnhancedStackTrace()
{
if (AsyncIteratorStateMachineAttributeType != null) return;
Assembly mba;
try
{
mba = Assembly.Load("Microsoft.Bcl.AsyncInterfaces");
}
catch
{
return;
}
AsyncIteratorStateMachineAttributeType = mba.GetType("System.Runtime.CompilerServices.AsyncIteratorStateMachineAttribute", false);
}
private static List<EnhancedStackFrame> GetFrames(Exception exception) private static List<EnhancedStackFrame> GetFrames(Exception exception)
{ {
@ -706,6 +724,10 @@ namespace System.Diagnostics
{ {
return false; return false;
} }
if (method.Name == "GetResult" && method.DeclaringType?.FullName == "System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore`1")
{
return false;
}
if (type == typeof(Task) || type.DeclaringType == typeof(Task)) if (type == typeof(Task) || type.DeclaringType == typeof(Task))
{ {
if (method.Name.Contains(".cctor")) if (method.Name.Contains(".cctor"))
@ -866,10 +888,8 @@ namespace System.Diagnostics
{ {
foundAttribute = true; foundAttribute = true;
foundIteratorAttribute |= asma is IteratorStateMachineAttribute foundIteratorAttribute |= asma is IteratorStateMachineAttribute
#if HAS_ASYNC_ENUMERATOR || AsyncIteratorStateMachineAttributeType != null
|| asma is AsyncIteratorStateMachineAttribute && AsyncIteratorStateMachineAttributeType.IsInstanceOfType(asma);
#endif
;
} }
} }

View File

@ -1,4 +1,3 @@
#if HAS_ASYNC_ENUMERATOR
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@ -63,4 +62,3 @@ namespace Ben.Demystifier.Test
} }
} }
} }
#endif

View File

@ -2,13 +2,14 @@
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks> <TargetFrameworks>netcoreapp2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net46</TargetFrameworks> <TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net461</TargetFrameworks>
<SignAssembly>true</SignAssembly> <SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\src\Ben.Demystifier\key.snk</AssemblyOriginatorKeyFile> <AssemblyOriginatorKeyFile>..\..\src\Ben.Demystifier\key.snk</AssemblyOriginatorKeyFile>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" Condition="'$(TargetFramework)' == 'net461' or '$(TargetFramework)' == 'netcoreapp2.1'" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" /> <PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="xunit" Version="2.4.1" />