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 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
int 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.
Aucun commentaire:
Enregistrer un commentaire