Reputation: 492
Im just starting with generics and was wondering how I can access T of the class in a class method? Lets take some code to explain better what I want to do:
public class RegisterResult<T> where T : IRegisterable
{
public bool Success { get; set; }
public string Entity { get; set; }
public string ErrorMessage { get; set; }
//Want to avoid this, by using generics:
/*public static RegisterResult UserSuccess = new RegisterResult(true, "User", "");
public static RegisterResult DeviceSuccess = new RegisterResult(true, "Device", "");
public static RegisterResult DeviceDataSucces = new RegisterResult(true, "DeviceData", "");*/
public RegisterResult(bool success, string errmsg)
{
this.Success = success;
//The following line does not work, so how can I reach that?
this.Entity = T.GetType().Name;
this.ErrorMessage = errmsg;
}
}
Thank you very much for all helpful and well meant answers!
UPDATE: Errormessage from Visual Studio
"T" is "type Parameter" and not valid in given context
Upvotes: 0
Views: 956