Sunday, November 16, 2014

Method to initialize Entity Object

  /// <summary>
        /// Method to initialize Entity Object
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T Initilize<T>()
        {
            T src = Activator.CreateInstance<T>();
            Type targetType = src.GetType();

            //    Loop through the source properties
            foreach (PropertyInfo p in targetType.GetProperties())
            {
                if (p.CanWrite)
                {
                    Type ptype = p.PropertyType;

                    if (ptype == typeof(byte))
                        p.SetValue(src, byte.MinValue, null);
                    if (ptype == typeof(string))
                        p.SetValue(src, string.Empty, null);
                    else if (ptype == typeof(int))
                        p.SetValue(src, (int)-1, null);
                    //else if (ptype == typeof(int?))
                    //    p.SetValue(src, (int)-1, null);
                    else if (ptype == typeof(Int64))
                        p.SetValue(src, (Int64)(-1), null);
                    //else if (ptype == typeof(Int64?))
                    //    p.SetValue(src, (Int64)(-1), null);
                    else if (ptype == typeof(Int32))
                        p.SetValue(src, (Int32)(-1), null);
                    //else if (ptype == typeof(Int32?))
                    //    p.SetValue(src, (Int32)(-1), null);
                    else if (ptype == typeof(short))
                        //p.SetValue(src, (short)255, null);
                        p.SetValue(src, (short)-1, null);
                    //else if (ptype == typeof(short?))
                    //    p.SetValue(src, (short)255, null);
                    else if (ptype == typeof(float))
                        p.SetValue(src, (float)(-1), null);
                    //else if (ptype == typeof(float?))
                    //    p.SetValue(src, (float)(-1), null);
                    else if (ptype == typeof(decimal))
                        p.SetValue(src, Convert.ToDecimal(-1), null);
                    //else if (ptype == typeof(decimal?))
                    //    p.SetValue(src, Convert.ToDecimal(-1), null);
                    else if (ptype == typeof(bool))
                        p.SetValue(src, false, null);
                    //else if (ptype == typeof(bool?))
                    //    p.SetValue(src, false, null);
                    else if (ptype == typeof(DateTime))
                        p.SetValue(src, DateTime.MinValue, null);
                    //else if (ptype == typeof(DateTime?))
                    //    p.SetValue(src, DateTime.MinValue, null);
                }
            }

            return src;
        }

No comments:

Using Authorization with Swagger in ASP.NET Core

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