Skip to content

Instantly share code, notes, and snippets.

@danielgreen
Created May 30, 2013 08:33
Show Gist options
  • Save danielgreen/5676514 to your computer and use it in GitHub Desktop.
Save danielgreen/5676514 to your computer and use it in GitHub Desktop.
General extension methods for Exceptions, Enums, etc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Utility
{
public class AvailableAttribute : Attribute
{
public AvailableAttribute(bool available) { this.Available = available; }
public virtual bool Available { get; protected set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Utility
{
public class CodeAttribute : Attribute
{
public CodeAttribute(string code) { this.Code = code; }
public virtual string Code { get; protected set; }
}
}
using System;
using System.Linq;
using System.ComponentModel;
using System.Reflection;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Text;
namespace Utility
{
public static class ExtensionsMethods
{
// Builds a string containing the messages from an exception and all of its inner exceptions
public static string GetFullMessage(this Exception e)
{
var sb = new StringBuilder(e.Message);
var inner = e.InnerException;
while (inner != null)
{
sb.AppendLine(";").Append(inner.Message);
inner = inner.InnerException;
}
return sb.ToString();
}
// A fluent Add method for the Dictionary collection; allows you to add multiple values in one line of code
public static Dictionary<TKey, TValue> AddFluent<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
{
dict.Add(key, value);
return dict;
}
// Retrieves a string attribute value from an Enum
public static string GetAttribute<TAttr>(this Enum value, Func<TAttr, string> expr) where TAttr : Attribute
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name != null)
{
FieldInfo field = type.GetField(name);
if (field != null)
{
TAttr attr =
Attribute.GetCustomAttribute(field,
typeof(TAttr)) as TAttr;
if (attr != null)
{
return expr(attr);
}
else
{
return name;
}
}
}
return null;
}
// Retrieves a value attribute (int, bool, etc) from an Enum
public static TRet GetAttribute<TAttr, TRet>(this Enum value, Func<TAttr, TRet> expr, TRet defaultValue = default(TRet))
where TAttr : Attribute
where TRet : struct
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name != null)
{
FieldInfo field = type.GetField(name);
if (field != null)
{
TAttr attr =
Attribute.GetCustomAttribute(field,
typeof(TAttr)) as TAttr;
if (attr != null)
{
return expr(attr);
}
}
}
return defaultValue;
}
// Retrieves the value of an enum's Description attribute
public static string GetDescription(this Enum value)
{
return value.GetAttribute<DescriptionAttribute>(da => da.Description);
}
// Retrieves the value of an enum's Code attribute
public static string GetCode(this Enum value)
{
return value.GetAttribute<CodeAttribute>(ca => ca.Code);
}
// Retrieves the value of an enum's Available boolean attribute
public static bool GetAvailable(this Enum value)
{
return value.GetAttribute<AvailableAttribute, bool>(aa => aa.Available);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment