At compile time, I don’t know what exact type I will pass to a method, but I am sure that this type will contain some property. How can I pass a Type to a function to perform casting in the function? I would like to get something like that:
foo (classType cl)
{
int x = ((cl)SomeClass).Id.Value;
}
The other answer won’t work when using .id because your T type isn’t constrained. The class has no idea that any T could implement a field/property called id
Imagine you used instead
T would be int and int doesn’t have an id field/property
You can constrain though
Though this means that T can only be of that particular type. Is there a base class that has ID that you can use instead? I don’t know if this is the solution you want though…
Edit:
In response to your post:
Should work assuming BaseClass implements ‘Value’ (and assuming SomeClass comes from somewhere as there appears to be no reference to it in the method!)