-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add an option to not transform Activator.CreateInstance<T>() to new T()
#3497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
I think that extra logic you found is used when dealing with |
Why not? If both collection initializer syntax and static T Test<T>()
where T : IList<int>, new()
{
return new T()
{
1, 2, 3
};
}After: static T Test<T>()
where T : IList<int>, new()
{
T t = Activator.CreateInstance<T>();
t.Add(1);
t.Add(2);
t.Add(3);
return t;
} |
|
I am not a fan of the setting name Copilot says:
System.Reflection.Metadata uses the term to refer to construction of generic type instances, see We should probably use something like
It's sufficient to add the check here: ILSpy/ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs Lines 78 to 81 in 51a2618
just like we already do for ILSpy/ICSharpCode.Decompiler/IL/Transforms/TransformCollectionAndObjectInitializers.cs Lines 82 to 86 in 51a2618
Then, if the setting is disabled, the initializer transform won't be applied in this specific case only. |
|
Upon reading the code referenced above again, I think this should be implemented by adding |
|
Thank you for your contribution! |
Closes #3495
This is my first contribution to this project, so to found places in code I needed I used full text search on the solution. Besides the place I changed there is also this piece:
ILSpy/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Lines 3420 to 3426 in 0bfe222
I was never able to hit this code path when I debugged added test case. Does this logic also need to be changed and if so, what test should I add to cover it?