Skip to content

Add an option to not transform Activator.CreateInstance<T>() to new T() #3495

Open
@DoctorKrolic

Description

@DoctorKrolic

Is your feature request related to a problem? Please describe.
new() generic constraint was introduced in the same C# version as generics themselves (C# 2). Its implementation under the hood requres to call Activator.CreateInstance<T>() for what user writes as new T() in the source code. ILSpy always decompiles Activator.CreateInstance<T>() into new T() and there is no setting to disable that. Low-level C# viewers like sharplab, .NET Lab or Compiler developer SDK would benifit is there was an option to disable such transformation to be able to present as low-level C# decompilation view as possible

Describe the solution you'd like
New API like:

namespace ICSharpCode.Decompiler;

class DecompilerSettings
{
+    public bool GenericTypeInstantiation { get; set; }
}

The name is obviously not final here, maybe you can suggest something better. By default the setting is true for C# 2 and above, which represents current behavior, but can be set to false, in which case Activator.CreateInstance<T>() are not transformed to new T() even for a generic T with new() constraint

Additional context
The use case here seems very specific, but I believe implementation and maintaining this option is very straight-forward, this is probably a matter of 1 if somewhere in the codebase

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions