Skip to content

Commit

Permalink
Fix incorrect optimization of Activator.CreateInstance (#50228)
Browse files Browse the repository at this point in the history
We should not return "default" if there's a private parameterless constructor. GetDefaultConstructor only calls the default constructor in the C# sense (public parameterless ctor on a non-abstract class).
  • Loading branch information
MichalStrehovsky authored Mar 25, 2021
1 parent 3571f36 commit 85e06bf
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private MethodIL TryGetIntrinsicMethodILForActivator(MethodDesc method)
&& method.Name == "CreateInstance")
{
TypeDesc type = method.Instantiation[0];
if (type.IsValueType && type.GetDefaultConstructor() == null)
if (type.IsValueType && type.GetParameterlessConstructor() == null)
{
// Replace the body with implementation that just returns "default"
MethodDesc createDefaultInstance = method.OwningType.GetKnownMethod("CreateDefaultInstance", method.GetTypicalMethodDefinition().Signature);
Expand Down

0 comments on commit 85e06bf

Please sign in to comment.