Get IEnumValue Based Values

March 19, 2021

Select use dynamic “enumerations” to hold values that are user defined and maintained in the database. A partial list of these classes is IBillCode, IState, and ITitleStatus. Below is a static helper to get a member value.

/// <summary>
/// Get member of IEnumValue based class.
/// </summary>
/// <typeparam name="T">type Interface</typeparam>
/// <param name="sp">Caller can pass either SelectServer or IOrder instance.</param>
/// <param name="code">Alias for member Name.</param>
/// <returns>Member with specified code or throws 
/// InvalidOperationException (Sequence contains no elements).</returns>
public static T GetIEnumValueFromName<T>(IServiceProvider sp, string code)
	where T : IEnumValue
{
	IEnumManager enumMgr = (IEnumManager)sp.GetService(typeof(IEnumManager));
	T value = enumMgr.GetEnum<T>()
		.Values.Where(t => t.Name == code).First();

	return value;
}

Here is an example of how to use the helper. Note that you can pass either an order instance to the helper or the SelectServer instance.

IOrder order = {get order instance}
string code = "DOC";
var bCode = GetIEnumValueFromName<IBillCode>(order, billCode);