I have a Model-Class that extends a BaseModel-class.
public abstract class BaseModel {
public abstract string Name;
public abstract string NamePlural;
//....
}
public class Production : BaseModel
{
public string Name = "production";
public string NamePlural = "productions";
//....
}
Somewhere else I call another class that uses the BaseModel as base for generics:
public class Store {
public BaseModel SomeMethod<T>()
where T : BaseModel
{
// here I need to get the Name property and the NamePlural property
T.NamePlural; // gives compile error "T Not valid in the given context
// next try:
var model = typeof(T);
model.NamePlural; // simply doesn't exist (model.Name gives the name of the class, but not the property)
// another strange try:
BaseModel model = new T(); // haha....nope
//....
return something...;
}
}
//usage:
Store store = new Store();
Production p = SomeMethod<Production>();
So the question is:
Is there a simple way to access these properties?
I even tried via Assembly.GetTypes()..., ...
Or do I need another genreric class as described in those answers: How to access Property of generic member in generic class ?
Yes, I've read related questions Access to properties of generic object and Get property of generic class