Skip to content

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

Closed
@DoctorKrolic

Description

@DoctorKrolic
Contributor

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

Activity

changed the title [-]Add an option to _not_ transform `Activator.CreateInstance<T>()` to `new T()`[/-] [+]Add an option to __not__ transform `Activator.CreateInstance<T>()` to `new T()`[/+] on Jun 17, 2025
changed the title [-]Add an option to __not__ transform `Activator.CreateInstance<T>()` to `new T()`[/-] [+]Add an option to not transform `Activator.CreateInstance<T>()` to `new T()`[/+] on Jun 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @siegfriedpammer@DoctorKrolic

      Issue actions

        Add an option to not transform `Activator.CreateInstance<T>()` to `new T()` · Issue #3495 · icsharpcode/ILSpy