I want to cast an object to a type that a System.Type object defines. I only know the object
and the Type
as inputs, I can't add type parameters
.
void Test()
{
object obj = 3;
Type type = typeof(int);
int number = Cast(obj, type);
Console.WriteLine($"{number}, {number.GetType()}"); // Should output "3, System.Int32"
}
I can't change the inputs
??? Cast(object obj, Type type)
{
// return the obj casted to type, but how?
}
I guess there's a way to solve it using reflection but I couldn't find anything like this.
Aucun commentaire:
Enregistrer un commentaire