diff --git a/src/Compilers/CSharp/Portable/Symbols/Retargeting/RetargetingParameterSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Retargeting/RetargetingParameterSymbol.cs index dd32f3e7212af..36b791724d1b6 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Retargeting/RetargetingParameterSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Retargeting/RetargetingParameterSymbol.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; +using System.Threading; using Microsoft.CodeAnalysis.CSharp.Emit; namespace Microsoft.CodeAnalysis.CSharp.Symbols.Retargeting @@ -23,6 +24,8 @@ internal abstract class RetargetingParameterSymbol : WrappedParameterSymbol /// private ImmutableArray _lazyCustomAttributes; + private TypeWithAnnotations.Boxed? _lazyTypeWithAnnotations; + protected RetargetingParameterSymbol(ParameterSymbol underlyingParameter) : base(underlyingParameter) { @@ -38,7 +41,14 @@ public sealed override TypeWithAnnotations TypeWithAnnotations { get { - return this.RetargetingModule.RetargetingTranslator.Retarget(_underlyingParameter.TypeWithAnnotations, RetargetOptions.RetargetPrimitiveTypesByTypeCode); + if (_lazyTypeWithAnnotations is null) + { + Interlocked.CompareExchange(ref _lazyTypeWithAnnotations, + new TypeWithAnnotations.Boxed(this.RetargetingModule.RetargetingTranslator.Retarget(_underlyingParameter.TypeWithAnnotations, RetargetOptions.RetargetPrimitiveTypesByTypeCode)), + null); + } + + return _lazyTypeWithAnnotations.Value; } }