I am currently running into a type issue with the .net framework I want to dynamically call a function based on its type. Which is easy using method overloading, the problem is using the PropertyInfo.GetValue(objectOfInterest)
returns a c# object which is not the type of the actual value its returning. As I loop over all the properties in the objectOfInterest some properties are bool, string string[], etc. I have created methods to deal with all the types Im expecting. In the Visual Studio debugger they show up as object{bool} or object{string} I am assuming those are subtypes but I'm not sure on that exactly. Is there a way to dynamically cast the object to its own subtype? I do not want to create the function doSomething(object passedValue);
as this will accept all the values and will not accomplish the polymorphism I'm trying to implement. I also do not want to use a switch case as they are not considered "agile". Im using .net Framework 4.7
var properties = typeof(objectOfInterest).GetProperties();
foreach(var property in properties){
var value = property.GetValue(objectOfInterest); //value is of type object
//Some sort of cast needs to go here like value = (subtypeof(value))value
doSomething(value); //Compiler error "Cannot convert object to bool,int,string"
}
Do something definitions
doSomething(int passedValue);
doSomething(string passedValue);
doSomething(bool passedValue);