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">Text property name</param>
        /// <param name="pairedProperty">Paired Text property name</param>
        /// <param name="valueProperty">Value property name</param>
        /// <returns></returns>
        public static dynamic GetFormatedAutoCompleteList(dynamic dynamicList, string textProperty, string pairedProperty, string valueProperty)
        {
            // Gets or sets list of objects for auto complete
            List<PairedValue> pairedValueList;
            // 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;
            // Gets or sets PairText property information from dynamic object
            PropertyInfo pairedPropertyInfo;
            try
            {
                // initilize new instance of TextValue class
                pairedValueList = new List<PairedValue>();
                // 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
                    pairedPropertyInfo = type.GetProperty(pairedProperty);
                    pairedValueList.Add(new PairedValue()
                    {
                        // 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))),
                        // Sets PairText property
                        PairText = HttpUtility.HtmlDecode(Convert.ToString(pairedPropertyInfo.GetValue(dynamicObject, null))),
                    });
                }
                // Returns formated list for auto complete
                return pairedValueList;
            }
            catch (Exception ex)
            {
                // throws exception to calling function
                throw ex;
            }
            finally
            {
                // Disposing used objects
                pairedValueList = null;
                type = null;
                textPropertyInfo = null;
                valuePropertyInfo = null;
                pairedPropertyInfo = null;
            }
        }

No comments:

Using Authorization with Swagger in ASP.NET Core

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