diff --git a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj
index 20073739b2bbd0..377157b7091670 100644
--- a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj
+++ b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj
@@ -162,7 +162,6 @@
-
@@ -170,6 +169,7 @@
+
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs
index 81372c76ce2ec8..0c920d1ea12bdc 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs
@@ -36,14 +36,14 @@ public override LocalBuilder DeclareLocal(Type localType, bool pinned)
{
ArgumentNullException.ThrowIfNull(localType);
- LocalBuilder localBuilder;
+ RuntimeLocalBuilder localBuilder;
RuntimeType? rtType = localType as RuntimeType;
if (rtType == null)
throw new ArgumentException(SR.Argument_MustBeRuntimeType);
- localBuilder = new LocalBuilder(m_localCount, localType, m_methodBuilder, pinned);
+ localBuilder = new RuntimeLocalBuilder(m_localCount, localType, m_methodBuilder, pinned);
// add the localType to local signature
m_localSignature.AddArgument(localType, pinned);
m_localCount++;
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeILGenerator.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeILGenerator.cs
index e176ac2cf0e894..b8992dbe5d29b3 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeILGenerator.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeILGenerator.cs
@@ -819,8 +819,8 @@ public override void Emit(OpCode opcode, LocalBuilder local)
ArgumentNullException.ThrowIfNull(local);
// Puts the opcode onto the IL stream followed by the information for local variable local.
- int tempVal = local.GetLocalIndex();
- if (local.GetMethodBuilder() != m_methodBuilder)
+ int tempVal = local.LocalIndex;
+ if (local is not RuntimeLocalBuilder localBuilder || localBuilder.GetMethodBuilder() != m_methodBuilder)
{
throw new ArgumentException(SR.Argument_UnmatchedMethodForLocal, nameof(local));
}
@@ -1185,7 +1185,7 @@ public override LocalBuilder DeclareLocal(Type localType, bool pinned)
// add the localType to local signature
m_localSignature.AddArgument(localType, pinned);
- return new LocalBuilder(m_localCount++, localType, methodBuilder, pinned);
+ return new RuntimeLocalBuilder(m_localCount++, localType, methodBuilder, pinned);
}
public override void UsingNamespace(string usingNamespace)
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeLocalBuilder.cs
similarity index 69%
rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs
rename to src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeLocalBuilder.cs
index 9dd4d6d5c2cfd9..e85c08daabefbe 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeLocalBuilder.cs
@@ -3,7 +3,7 @@
namespace System.Reflection.Emit
{
- public sealed class LocalBuilder : LocalVariableInfo
+ internal sealed class RuntimeLocalBuilder : LocalBuilder
{
#region Private Data Members
private readonly int m_localIndex;
@@ -13,9 +13,9 @@ public sealed class LocalBuilder : LocalVariableInfo
#endregion
#region Constructor
- internal LocalBuilder(int localIndex, Type localType, MethodInfo methodBuilder)
+ internal RuntimeLocalBuilder(int localIndex, Type localType, MethodInfo methodBuilder)
: this(localIndex, localType, methodBuilder, false) { }
- internal LocalBuilder(int localIndex, Type localType, MethodInfo methodBuilder, bool isPinned)
+ internal RuntimeLocalBuilder(int localIndex, Type localType, MethodInfo methodBuilder, bool isPinned)
{
m_isPinned = isPinned;
m_localIndex = localIndex;
@@ -25,14 +25,7 @@ internal LocalBuilder(int localIndex, Type localType, MethodInfo methodBuilder,
#endregion
#region Internal Members
- internal int GetLocalIndex()
- {
- return m_localIndex;
- }
- internal MethodInfo GetMethodBuilder()
- {
- return m_methodBuilder;
- }
+ internal MethodInfo GetMethodBuilder() => m_methodBuilder;
#endregion
#region LocalVariableInfo Override
diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj
index a007a3ed7fbba5..ec3a3363a511a7 100644
--- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj
+++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj
@@ -144,7 +144,6 @@
-
diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs
deleted file mode 100644
index 2d1afc04d5b730..00000000000000
--- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs
+++ /dev/null
@@ -1,37 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Reflection.Emit
-{
- public sealed class LocalBuilder : LocalVariableInfo
- {
- internal LocalBuilder()
- {
- // Prevent generating a default constructor
- }
-
- public override bool IsPinned
- {
- get
- {
- return default;
- }
- }
-
- public override int LocalIndex
- {
- get
- {
- return default;
- }
- }
-
- public override Type LocalType
- {
- get
- {
- return default;
- }
- }
- }
-}
diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems
index ed3325afbec528..b27a43992e2ce7 100644
--- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems
+++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems
@@ -418,7 +418,6 @@
-
@@ -674,7 +673,9 @@
+
+
diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs
new file mode 100644
index 00000000000000..7c38c287bf22b5
--- /dev/null
+++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs
@@ -0,0 +1,16 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace System.Reflection.Emit
+{
+ public abstract class LocalBuilder : LocalVariableInfo
+ {
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ ///
+ /// This constructor is invoked by derived classes.
+ ///
+ protected LocalBuilder() { }
+ }
+}
diff --git a/src/libraries/System.Reflection.Emit.ILGeneration/ref/System.Reflection.Emit.ILGeneration.cs b/src/libraries/System.Reflection.Emit.ILGeneration/ref/System.Reflection.Emit.ILGeneration.cs
index e549331571dba1..f79ee4288df6ce 100644
--- a/src/libraries/System.Reflection.Emit.ILGeneration/ref/System.Reflection.Emit.ILGeneration.cs
+++ b/src/libraries/System.Reflection.Emit.ILGeneration/ref/System.Reflection.Emit.ILGeneration.cs
@@ -67,9 +67,9 @@ public virtual void ThrowException([System.Diagnostics.CodeAnalysis.DynamicallyA
public static bool operator ==(System.Reflection.Emit.Label a, System.Reflection.Emit.Label b) { throw null; }
public static bool operator !=(System.Reflection.Emit.Label a, System.Reflection.Emit.Label b) { throw null; }
}
- public sealed partial class LocalBuilder : System.Reflection.LocalVariableInfo
+ public abstract class LocalBuilder : System.Reflection.LocalVariableInfo
{
- internal LocalBuilder() { }
+ protected LocalBuilder() { }
public override bool IsPinned { get { throw null; } }
public override int LocalIndex { get { throw null; } }
public override System.Type LocalType { get { throw null; } }
diff --git a/src/libraries/System.Reflection.Emit/src/Resources/Strings.resx b/src/libraries/System.Reflection.Emit/src/Resources/Strings.resx
index cc67be68a05b95..e425a272ac097f 100644
--- a/src/libraries/System.Reflection.Emit/src/Resources/Strings.resx
+++ b/src/libraries/System.Reflection.Emit/src/Resources/Strings.resx
@@ -186,6 +186,9 @@
Method body should not exist.
+
+ Local passed in does not belong to this ILGenerator.
+
Invalid Label.
diff --git a/src/libraries/System.Reflection.Emit/src/System.Reflection.Emit.csproj b/src/libraries/System.Reflection.Emit/src/System.Reflection.Emit.csproj
index 3c6273439966ac..03b448b163bc71 100644
--- a/src/libraries/System.Reflection.Emit/src/System.Reflection.Emit.csproj
+++ b/src/libraries/System.Reflection.Emit/src/System.Reflection.Emit.csproj
@@ -12,6 +12,7 @@
+
diff --git a/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/ILGeneratorImpl.cs b/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/ILGeneratorImpl.cs
index eb9d5f70a81dc7..e7331233adb3dc 100644
--- a/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/ILGeneratorImpl.cs
+++ b/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/ILGeneratorImpl.cs
@@ -17,6 +17,7 @@ internal sealed class ILGeneratorImpl : ILGenerator
private bool _hasDynamicStackAllocation;
private int _maxStackSize;
private int _currentStack;
+ private List _locals = new();
private Dictionary