Skip to content

Instantly share code, notes, and snippets.

@papeMK2
Created May 11, 2020 09:13
Show Gist options
  • Save papeMK2/0e715adacd4f05627e884242e6f8c833 to your computer and use it in GitHub Desktop.
Save papeMK2/0e715adacd4f05627e884242e6f8c833 to your computer and use it in GitHub Desktop.
C# return result class
using System;
namespace Sample.Common
{
public class Result
{
public bool Successed { get; set; }
public string Message { get; set; }
public string Description { get; set; }
public Exception Exception { get; set; }
public static Result Success()
{
return Success(null, null);
}
public static Result Success(string message = null, string description = null)
{
return new Result
{
Successed = true,
Message = message,
Description = description
};
}
public static Result Failed(string message, string description = null)
{
return new Result
{
Successed = false,
Message = message,
Description = description
};
}
public static Result Failed(Exception ex)
{
return new Result
{
Successed = false,
Message = ex.Message,
Description = ex.ToString(),
Exception = ex
};
}
}
public class Result<T> : Result
{
public T Value { get; private set; }
public static Result<T> Success(T result = default(T))
{
return Success(null, null, result);
}
public static Result<T> Success(string message = null, string description = null, T result = default(T))
{
return new Result<T>
{
Successed = true,
Message = message,
Description = description,
Value = result
};
}
public static Result<T> Failed(string message = null, string description = null, T result = default(T))
{
return new Result<T>
{
Successed = false,
Message = message,
Description = description,
Value = result
};
}
public static Result<T> Failed(Exception ex, T result = default(T))
{
return new Result<T>
{
Successed = false,
Message = ex.Message,
Description = ex.ToString(),
Exception = ex,
Value = result
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment