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="textProperty">Text property name</param>
        /// <param name="valueProperty">Value property name</param>
        /// <returns></returns>
        public static dynamic GetFormatedAutoCompleteList(dynamic dynamicList, string textProperty, string valueProperty)
        {
            // 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 textPropertyInfo;
            // 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
                    textPropertyInfo = type.GetProperty(textProperty);
                    // 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(Convert.ToString(textPropertyInfo.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;
                textPropertyInfo = 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 {     ...