For the sake of simplicity take following example, which is not valid in C#:
public void SomeGenericFunc<T>(T classObject, T.Property.Type classPorpertyType)
{
string propertyName = nameof(classPropertyType);
// ...
}
Sample simple class:
public class Car
{
public Car(){}
public int DoorsCount { get; set; }
public string Color { get; set; }
}
A function call would look something like this:
SomeGenericFunc<Car>(new Car(), Car.DoorsCount); //should be valid
SomeGenericFunc<Car>(new Car(), SomeType); //should be invalid at compile time: SomeType is not a property of Car
SomeGenericFunc<Car>(new Car(), Car.Color); //should be valid
Obviously there is nothing like T.Property.Type
available in C#. In this example i don't want to pass nameof(Car.DoorsCount)
everytime i call the function. I want the nameof()
call to take place in SomeGenericFunc<T>()
,so i can call SomeGenericFunc<T>()
with any class and any of that class' property.
So is it possible to pass and expect a property type of a class in a generic function?
EDIT:
To make it clear: I DON'T want the value of the property Car.DoorsCount
. I want the name of the property, without passing it directly as parameter. I want to resolve the name of the property in SomeGenericFunc<T>()
.