IEnumerable & lt; string & gt;

Source: Internet
Author: User

Similar to the following string

"ID", "NameValue", "CodeValue", "ExchangeTypeValue", 6, "invalid"

"ID2", "NameValue2", "CodeValue2", "ExchangeTypeValue2", 6, "invalid"

.......

It may exist in a file or call the structured data returned by other programs. How can this problem be resolved? In other scenarios, the returned Order (attribute order) is changed, and the class structure is the same. How can this problem be solved? In many similar scenarios, should we abstract a generic method to deal with this scenario? Of course, not only in the above situations, the returned structure may be definite, but in different forms. This process is omitted temporarily because the regular expression can be completely parsed. To use the following method, you must convert it to the IEnumerable <string> structure. In the IEnumerable <string> structure, IEnumerable <string> is the value of all objects, it is a set of values of multiple objects. This article uses reflection (provided in subsequent articles about the IL operation). The related class diagram is as follows:

ResultTransfer is mainly used to parse the structure of IEnumerable <string>. You can also specify the params string [] propertyNames attribute parameter list to determine the resolution sequence (that is, the attribute sequence ), the main method is as follows:

The first parameter entityRows is a set of Object List values.

The second parameter, propertyNames, is an optional parameter. After this parameter is entered, if the propertyNames contains relevant attributes, the system will parse the attribute order corresponding to propertyNames. Otherwise, the attribute order is determined based on the DataMemberAttribute of the provided T class.

The implementation code is very simple and simple. The method is as follows:

          IList<T> Parse<T>(IEnumerable<IEnumerable<>> entityRows,  [] propertyNames)  T :  (entityRows ==  || entityRows.Count() ==   List<T><T> entities =  List<T> members =  DataMemberAttributeCollection( (members.Count <=   List<T>=  ( propertyValues  (propertyValues ==  || propertyValues.Count() == <T>  T Generate<T>(IEnumerable<> T : = Activator.CreateInstance<T> memberCount = propertyCount = (memberCount ==  || propertyCount ==  convertCount = propertyValueIndex =  ( propertyValue  (propertyValueIndex >=++= members[propertyValueIndex - = (propertyValue == ,  (propertyValue.GetType() == result = funcProvider.DynamicInvoke(currAttribute.PropertyType, (propertyValue ?? 

The DataMemberAttributeCollection collection class is mainly used to set the order of resolution attributes. Likewise, this class provides two parameter constructors to generate the corresponding configuration information public DataMemberAttributeCollection (Type type, params string [] propertyNames ).

The main code is as follows:

          GetConfiguration(Type type,   (type ==  || type.GetProperties().Length <=  (propertyNames ==  || propertyNames.Length == ._memberAttributes = ._memberAttributes.OrderBy(p =>  AddDataMemberAttributes(Type type, <PropertyInfo> validPropertyInfos =  List<PropertyInfo> ( propertyName  (= (tempPropertyInfo ==   ArgumentException(.Format( (validPropertyInfos.Count() >  ( property  =  (PropertyInfo propertyInfo = AttributeUtility.GetCustomAttribute<DataMemberAttribute> (attr ==  (! (._memberAttributes.Count(p => p.Order == attr.Order) >   ArgumentException(.Format(  (====

This class ensures that the DataMemberAttribute of the specified Type is set correctly (whether there is the same Order) and whether an incorrect propertyName is entered.

For specific applications, unit testing is convenient and straightforward.

(1) The following is an application for inputting only one parameter:

 <IList<>> entityRows =  List<IList<>> List<>() { , , , , ,  contracts = ResultTransfer.Parse<ContinousContract>== ].Code, ].Name, ].ExchangeType, ].OrgidID, ].ExchangeTypeValue, 

(2) The following is an application for inputting only invalid parameters:

 <IList<>> entityRows =  List<IList<>> List<>() { , , , , ,  contracts = ResultTransfer.Parse<ContinousContract>== ].Code, ].Name, ].ExchangeType, ].OrgidID, ].ExchangeTypeValue, 

If you enter an invalid IEnumerable <string> parameter, the method is implicitly converted, for example, "sss" is converted to 0.

(3) When two parameters are used:

 <IList<>> entityRows =  List<IList<>> List<>() { , , , ,  propertyNames =  List<>() { , , ,  contracts = ResultTransfer.Parse<ContinousContract>== ].Code, ].Name, ].ExchangeType, ].OrgidID, ].ExchangeTypeValue, 

Once two parameters are entered and the number of propertyNames parameters is greater than 0, the attribute order corresponding to propertyNames is parsed. For attribute names with incorrect input, an exception is thrown inside the method. You can also add a parameter to control whether to throw an exception or to write a moderate log file.

Regular Expression Parsing is simple for parsing strings in a fixed format into IEnumerable <string>. This article will not explain it, skip it...

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.