I'm trying to dynamically changing a property type in my code, for example, at Person class bellow how can I change property name type from bool to string?
public class Person
{
public bool name;
}
I'm trying to dynamically changing a property type in my code, for example, at Person class bellow how can I change property name type from bool to string?
public class Person
{
public bool name;
}
If you want to do that at runtime you have different options. Some of them:
1) Make the property object and check types wherever you use it:
public class Person
{
public object Name;
}
2) Create a generic type for Person that allows you to define different instances of the class for different types:
public class Person<T>
{
public T Name;
}
var boolPerson = new Person<bool>();
boolPerson.Name = true;
var stringPerson = new Person<string>();
stringPerson.Name = "aString";
However you should explain why do you want to do this as there could be a better solution.
Have a look at this (you can create a dynamic object and change that object at runtime).