Skip to content
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

Warn on Activator.CreateInsatnce when ctor is not public. #224

Open
JohanLarsson opened this issue Feb 8, 2020 · 2 comments
Open

Warn on Activator.CreateInsatnce when ctor is not public. #224

JohanLarsson opened this issue Feb 8, 2020 · 2 comments

Comments

@JohanLarsson
Copy link
Collaborator

class C
{
    internal C(int i) { }

    C M() => (C)Activator.CreateInstance(typeof(C), 1);
}
@JohanLarsson
Copy link
Collaborator Author

Not sure what the best code is if we write a fix, maybe:

class C
{
    internal C(int i) { }

    C M() => (C)Activator.CreateInstance(typeof(C), BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, new object[] { 1 }, null, null);
}

@jnm2
Copy link
Collaborator

jnm2 commented Feb 13, 2020

BindingFlags.CreateInstance is only relevant when calling the Type.InvokeMember method.
https://docs.microsoft.com/en-us/dotnet/api/system.reflection.bindingflags?view=netcore-3.1#System_Reflection_BindingFlags_CreateInstance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants