Sunday, November 16, 2014

Method to Get formated list of objects for auto complete

/// <summary>
        /// Get formated list of objects for auto complete
        /// </summary>
        /// <param name="dynamicList">List for formatting</param>
        /// <param name="textProperty1">First Text property name</param>
        /// <param name="textProperty2">Second Text property name</param>
        /// <param name="valueProperty">Value property name</param>
        /// <param name="isCombined">whether the two Text Properties are concantinated</param>
        /// <returns></returns>
        public static dynamic GetFormatedAutoCompleteList(dynamic dynamicList, string textProperty1, string textProperty2, string valueProperty, bool isCombined)
        {
            // Gets or sets list of objects for auto complete
            List<TextValue> textValueList;
            // Gets the type of dynamic object in the list
            Type type;
            // Gets or sets text property information from dynamic object
            PropertyInfo textPropertyInfo1;
            // Gets or sets text property information from dynamic object
            PropertyInfo textPropertyInfo2;
            // Gets or sets value property information from dynamic object
            PropertyInfo valuePropertyInfo;
            try
            {
                // initilize new instance of TextValue class
                textValueList = new List<TextValue>();
                // Iterate through each object in the dynamic list
                foreach (dynamic dynamicObject in dynamicList)
                {
                    // Sets type of dynamic object
                    type = dynamicObject.GetType();
                    // Sets text property information
                    textPropertyInfo1 = type.GetProperty(textProperty1);
                    // Sets text property information
                    textPropertyInfo2 = type.GetProperty(textProperty2);
                    // Sets value property information
                    valuePropertyInfo = type.GetProperty(valueProperty);
                    // Add object to text value list
                    textValueList.Add(new TextValue()
                    {
                        // Sets text propert after html decoding
                        Text = HttpUtility.HtmlDecode(string.Format(ERPUtilityResources.NameCodeFormat, Convert.ToString(textPropertyInfo1.GetValue(dynamicObject, null)),
                        Convert.ToString(textPropertyInfo2.GetValue(dynamicObject, null)))),
                        // Sets value property
                        Value = HttpUtility.HtmlDecode(Convert.ToString(valuePropertyInfo.GetValue(dynamicObject, null))),
                    });
                }
                // Returns formated list for auto complete
                return textValueList;
            }
            catch (Exception ex)
            {
                // throws exception to calling function
                throw ex;
            }
            finally
            {
                // Disposing used objects
                textValueList = null;
                type = null;
                textPropertyInfo1 = null;
                textPropertyInfo2 = null;
                valuePropertyInfo = null;
            }
        }

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...