I have a problem. I want to make a generic method to convert my errors enums to specific response. for instance my response object
public class ErrorResponse
{
public ulong Code { get; set; }
public string Description { get; set; }
public string OrigionationSystem { get; set; }
}
and I want to convert my all errors to this response for eg
public enum ProductErrors:ulong
{
None,
[Description("Prouct Not Found")]
ProductNotFound,
[Description("Invalid Product Isbn")]
InvalidIsbn
}
public enum CustomerErrors:ulong
{
None,
AlreadyExists,
InvalidEmailAddress
}
and now i want to make generic method to convert my enums to ErrorResponse. previously i was using this method to convert my error codes to error response.
public static IList<ErrorResponse> ParseErrorCodeToErrorResponse(this IEnumerable<ProductErrors> errorCodes)
{
return errorCodes.Select(x => new ErrorResponse
{
Code = (ulong)x,
Description = x.GetDescription(),
OriginatingSystem = Extensions.GetCurrentSystemName()
} ).ToList();
}
problem raise here :
public static IList<ErrorResponse> ParseErrorCodeToErrorResponse<T>(this IEnumerable<T> errorCodes) where T : struct, IConvertible
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException("T must be an enumerated type");
}
return errorCodes.Select(x => new ErrorResponse
{
Code = (ulong)x,
Description = x.GetDescription(),
OriginatingSystem = Extensions.GetSystemName()
}).ToList();
}
when it's for only one enum it's working fine but when i made it generic it said
Type T can't convert to ulong and on x.GetDescription() it is saying can't resolve GetDescription()