2

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()

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • 3
    What is the actual problem? error compiling? Error running? Wrong results returned? Something else? – Chris Aug 09 '17 at 10:39
  • What is the link between the `ProductErrors`, `ErrorResponse` and `ErrorDetailEx` classes? – Mihai Pantea Aug 09 '17 at 10:40
  • sorry ErrorDetailEx is old name I renamed it to ErrorResponse bascially my this method is for only one enum. i want to make it generic for all enums so that all error codes use that method to convert them into ErrorResponse..@MihaiPantea @Chris – Imran Ahmad Shahid Aug 09 '17 at 10:43
  • 1
    Is it the description part that's giving you trouble? If so maybe checkout [this](https://stackoverflow.com/questions/2650080/how-to-get-c-sharp-enum-description-from-value) – Mihai Pantea Aug 09 '17 at 10:43
  • @ImranAhmadShahid: That's great. You have told us a lot of what you want to do. You still haven't told us where the actual problem is... Post the code that you have tried to use to solve the problem with an indication of how it failed and we can probably tell you why it failed and how to fix it. Until we know exactly where you are running into a problem your question is too broad. For example do you not know how to write generic methods at all? do you not know how to handle constraining to just enums? Or something else I've not thought of? – Chris Aug 09 '17 at 10:46
  • @MihaiPantea when it's only for ProductErrors. it's convertable to Ulong and x.GetDescription() is working fine but when i made it generic it said can't convert T to ulong – Imran Ahmad Shahid Aug 09 '17 at 10:47
  • @Chris i edited my question with full detail what is my problem – Imran Ahmad Shahid Aug 09 '17 at 10:51
  • What do you mean **when it's made generic**? Are you trying to define your method as an extension method over `IEnumerable` so that it works for `ProductErrors` and `CustomerErrors`? – Mihai Pantea Aug 09 '17 at 10:51
  • 1
    @MihaiPantea yeah i have edit my question.. can you look into it – Imran Ahmad Shahid Aug 09 '17 at 10:52
  • Take a look at [this answer to a related question](https://stackoverflow.com/a/36574524/7122). It shows how to achieve what you are trying to do. – David Arno Aug 09 '17 at 11:08

0 Answers0