Given the following code:
var property = typeof(TEntity).GetProperty("Id");
var temp = (property.PropertyType)id;
VS underlines ‘property’ in the second line and tells me the type or namespace cannot be found. Why? I’ve tried with and without using System.Reflection at the top and get the same result.
I was able to work around it by using Convert.ChangeType(id, property.PropertyType), but I’m curious what about the C# spec makes the previous code invalid.
It expects a type (not variable of type
System.Typebut actual type) between the parentheses, but such type (property.PropertyType) doesn’t exist.To prove this add this to your code:
Now, the compiler will be satisfied but this is probably not what you want to do.