I am learning c# code from one of the applications that I run SQL queries from.
I am wondering what the following code does in layman’s terms:
return typeof(ViewModelBase<T>).GetProperty(propertyName) != null;
This is in a function that returns a boolean and a string is passed into it.
ViewModelBase<T> is an abstract class. Can someone also explain what the <T> does in this? I have ideas on these but I’m not sure what exactly is true.
Thanks!
The code returns
trueif the type has the property, andfalseif it doesn’t.This code will be written inside of a generic class, with a type parameter of T. In generics, each time a “hard” type is used with the generic class, the compiler creates a brand new concrete type. So for example, if there was code in your project that was using
ViewModelBase<int>,ViewModelBase<string>, andViewModelBase<MyType>, there would be three concrete types created in the final assembly by the compiler.Each of these three hypothetical types would have properties and methods. The code shown above would (for all intents and purposes) be duplicated three times, with the type parameter “T” substituted with
int,stringandMyTypein each of the three cases.GetProperty()would then check to see if the concrete types had the property given in the “propertyName” variable, and return true or false accordingly.