2

I am trying to initialize a class using Activator.CreateInstance where respective class has three constructors as given bellow, here when I try to create an instance by passing bool value in Activator.CreateInstance method it throws error No parameterless constructor defined for this object but it successfully create class instance for string or int value.

public class MyType
{
    //public MyType()
    //{

    //}

    public MyType(string value)
    {
        FlagString = value;
    }

    public MyType(int value)
    {
        FlagInt = value;
    }

    public MyType(bool value)
    {
        FlagBool = value;
    }

    public string FlagString { get; set; }
    public int FlagInt { get; set; }
    public bool FlagBool { get; set; }
}


class Program
    {
        static void Main(string[] args)
        {
          
            MyType stringVal = (MyType)Activator.CreateInstance(typeof(MyType), "Yes");
            string s = stringVal.FlagString;

            MyType intVal = (MyType)Activator.CreateInstance(typeof(MyType), 1);
            int s1 = stringVal.FlagInt;

            MyType boolValue = (MyType)Activator.CreateInstance(typeof(MyType), true); //Throws error
            bool s2 = stringVal.FlagBool;
        }
    }

I have no idea why constructor public MyType(bool value) does not get called for bool value, please help me to fix this issue.

Neeraj Kumar Gupta
  • 2,157
  • 7
  • 30
  • 58
  • 'Throws error' - What error? – Jasper Kent Aug 26 '20 at 16:02
  • You should select the constructor yourself and that it by reflection. [here](https://stackoverflow.com/a/60513076/2748412) is a small example of how to call it with contructor parameters – Franck Aug 26 '20 at 16:02

3 Answers3

4

This is because there is an overload of Activator.CreateInstance that takes a bool as a second parameter, see here. The quick fix is to cast the parameter to object to force it to use the correct overload:

MyType boolValue = (MyType)Activator.CreateInstance(typeof(MyType), (object)true);

Or pass it in as an array:

MyType boolValue = (MyType)Activator.CreateInstance(typeof(MyType), new object[] {true});
DavidG
  • 113,891
  • 12
  • 217
  • 223
1

You are calling a completely different overload. If you want to pass in a bool argument, either box it or don’t call params implicitly and pass an object array containing the required arguments.

Your call right now resolves to an overload where the bool argument means wether the constructor can be non public.

Checking the documentation makes the issue pretty clear to identify and understand.

InBetween
  • 32,319
  • 3
  • 50
  • 90
0

Your code does not compile, I asssume this:

MyType intVal = (MyType)Activator.CreateInstance(typeof(MyType), 1);
int s1 = stringVal.FlagInt;

MyType boolValue = (MyType)Activator.CreateInstance(typeof(MyType), true); //Throws error
int s2 = stringVal.FlagBool;

should be this:

MyType intVal = (MyType)Activator.CreateInstance(typeof(MyType), 1);
int s1 = intVal.FlagInt;

MyType boolVal = (MyType)Activator.CreateInstance(typeof(MyType), true); //Throws error
bool s2 = boolVal.FlagBool;

Now regarding your question, you make a call to Activator.CreateInstance(Type type, bool nonPublic); overload which tries to call the default constructor. But there is no default constructor in your class.

ndogac
  • 1,185
  • 6
  • 15
  • That's just a typo in the question, the error OP is talking about is on the line that says "Throws error" – DavidG Aug 26 '20 at 16:07
  • @DavidG I was going to edit my answer regarding his question. Just a little bit patience :) – ndogac Aug 26 '20 at 16:09