It is easy to use enumeration extension classes

Due to the project takes the time to write enumeration extension classes, details are as follows:

 Enum declaration:

public enum the Status
{
[the Description ( "Invite")]
Invite =. 1,
[the Description ( "declined the invitation")]
decline the invitation = 2,
[the Description ( "Join")]
Join =. 3,
[the Description ( " refused to join ")]
refused to join. 4 =,
[the Description (" agreed to join ")]
agreed to join. 5 =,
[the Description (" away ")]
to leave = 6
}

 

Call the method:

var temp = Convert.ToInt32 (. CmsRelationship.Status agreed to join); // common use
var temp1 = EnumExtension.GetValue <CmsRelationship.Status> ( " refuse an invitation"); // call the extended class
var temp2 = EnumExtension.GetName <CmsRelationship .Status> (2); // call extended by

 

Extended class method:


public static class EnumExtension
{

/// <Summary>
/// The name of the enumerated type, acquires digital values of the enumerated type
/// </ Summary> 
public static int the GetValue <T> (String the inputString)
{
the try
{
the Type of enumType = typeof (T );
var = enumModel that Enum.Parse (of enumType, the inputString);
var = Result Convert.ToInt32 (enumModel);
return Result;
}
the catch
{
return 0;
}
}

 

/// <Summary>
/// enumerated type of digital values, acquires the name of enumeration type
/// </ Summary> 
public static String GetName <T> (int INPUT) 
{
the try
{
var name = Enum.GetName (typeof (T), INPUT);
return name;
}
the catch
{
return "";
}
}

}

Reproduced in: https: //www.cnblogs.com/ushou/p/5666425.html

Guess you like

Origin blog.csdn.net/weixin_34127717/article/details/93765235