. NET Enumeration type to List type,. net enumeration to list type

Source: Internet
Author: User

. NET Enumeration type to List type,. net enumeration to list type

This bidding status was originally written to several dead States through html code on the front-end interface. Now it needs to be changed to dynamic loading. These statuses are defined as enumeration types.

 

1: Define an enumeration type

/// <Summary>
/// Resource Status
/// </Summary>
Public enum ResourceState
{
/// <Summary>
/// Dismounting
/// </Summary>
[Description ("dismounting")]
SoldOut = 0,
/// <Summary>
/// Mounting
/// </Summary>
[Description ("shelving")]
Putaway = 1,

/// <Summary>
/// Transaction successful
/// </Summary>
[Description ("transaction successful")]
Success = 2,

/// <Summary>
/// Waste mark
/// </Summary>
[Description ("Waste mark")]
AbandonedTender = 6,
/// <Summary>
/// Default mark
/// </Summary>
[Description ("default")]
DefaultMark = 7,

/// <Summary>
/// Winning
/// </Summary>
[SetClassification (Type = 5)]
[Description ("")]
WinTheBidding = 3,
/// <Summary>
/// Stream mark
/// </Summary>
[SetClassification (Type = 6)]
[Description ("streaming")]
FlowStandard = 4,
/// <Summary>
/// Fail to win
/// </Summary>
 [SetClassification (Type = 4)]
[Description ("not winning")]
LoseABid = 5,
/// <Summary>
/// Bidding
/// </Summary>
[SetClassification (Type = 2)]
[Description ("Bidding")]
Bidding = 8,

/// <Summary>
/// Bidding
/// </Summary>
[SetClassification (Type = 3)]
[Description ("Bidding")]
Auctioning = 9,

/// <Summary>
/// Processed (for streaming Resource)
/// </Summary>
[Description ("processed")]
Alreadyprocessed = 10,

/// <Summary>
/// Expired
/// </Summary>
[Description ("expired")]
ExpiredTime = 11,

/// <Summary>
/// All quotations
/// </Summary>
[SetClassification (Type = 1)]
[Description ("All quotations")]
All = 12


}

2: Customize a tag type inheritanceAttribute

/// <Summary>
/// Add custom attributes

/// Function: Filter enumeration types
/// </Summary>
Public class SetClassificationAttribute: Attribute
{
/// <Summary>
/// Category
/// </Summary>
Public int Type {get; set ;}

Public SetClassificationAttribute (){}

}

3: Custom return List type

/// <Summary>
/// Custom Return Value Type
/// </Summary>
Public class EnumberCreditType
{
/// <Summary>
/// Enumeration description
/// </Summary>
Public string Desction {set; get ;}

/// <Summary>
/// Enumeration name
/// </Summary>
Public string Key {set; get ;}

/// <Summary>
/// Enumerated object Value
/// </Summary>
Public int Value {set; get ;}

/// <Summary>
/// Description
/// </Summary>
Public string Name {get; set ;}


/// <Summary>
/// Category
/// </Summary>
Public int Classification {set; get ;}
}

4: Convert enumeration to List <EnumberCreditType>

/// <Summary>
/// Obtain the enumerated list
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Returns> </returns>
Public static List <EnumberCreditType> EnumToList <T> ()
{
List <EnumberCreditType> list = new List <EnumberCreditType> ();

Foreach (var e in Enum. GetValues (typeof (T )))
{
EnumberCreditType m = new EnumberCreditType ();
Object [] objArr = e. GetType (). GetField (e. ToString (). GetCustomAttributes (typeof (DescriptionAttribute), true );
If (objArr! = Null & objArr. Length> 0)
{
DescriptionAttribute da = objArr [0] as DescriptionAttribute;
M. Desction = da. Description;
}
// SetClassification
Object [] setClassificationArr = e. GetType (). GetField (e. ToString (). GetCustomAttributes (typeof (SetClassificationAttribute), true );
If (setClassificationArr! = Null & setClassificationArr. Length> 0)
{
SetClassificationAttribute da = setClassificationArr [0] as SetClassificationAttribute;
M. Classification = da. Type;
}
// Display
Object [] disArr = e. GetType (). GetField (e. ToString (). GetCustomAttributes (typeof (DisplayAttributes), true );
If (disArr! = Null & disArr. Length> 0)
{
DisplayAttribute da = disArr [0] as DisplayAttribute;
M. Name = da. Name;
}

M. Value = Convert. ToInt32 (e );
M. Key = e. ToString ();
List. Add (m );
}
Return list;
}

5: Use

Static void Main (string [] args)
{

// Convert the retrieved enumerated type to List and use the Where filter condition of List
Var query = _ enumAppservice. enumToList <ResourceState> (). where (e => e. classification> = 1 & e. classification <= 6 ). orderBy (e => e. classification ). toList ();
Console. ReadKey ();
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.