Thursday 8 November 2012

Custom extensions that I use regularly


This is just a list of extensions that I use.

public static Guid ToGuid(this string stringValue)
        {
            try
            {
                return new Guid(stringValue);
            }
            catch (FormatException ex)
            {
                throw new FormatException("string could not convert to Guid");
            }
            catch (ArgumentNullException ex)
            {

                return Guid.Empty;
            }
        }

        public static int ToInt32(this string stringValue)
        {
            if (string.IsNullOrEmpty(stringValue))
            {
                return -0;
            }
            else
            {
                return Convert.ToInt32(stringValue);
            }
        }

        public static bool ToBoolean(this string stringValue)
        {
            if (string.IsNullOrEmpty(stringValue))
            {
                return false;
            }
            else
            {
                return Convert.ToBoolean(stringValue.ToInt32());
            }
        }

// dont really use this one, cannot guarentee that the lastname wasnt supposed to start with a lowercase
        public static string ToProperCase(this string stringValue)
        {
            if (string.IsNullOrEmpty(stringValue))
            {
                return string.Empty;
            }
            else
            {
                var ti = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo;
                return ti.ToTitleCase(stringValue.ToLower());
            }
        }

        public static bool IsNull(this object o)
        {
            return o == null;
        }

        public static bool IsNotNull(this object o)
        {
            return !o.IsNull();
        }

        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }

        public static bool IsNotNullOrEmpty(this string s)
        {
            return !string.IsNullOrEmpty(s);
        }

        public static string ToValidFileName(this string value)
        {
            return Regex.Replace(Regex.Replace(value, @"\W", "_"), "_{2,}", "_");
        }

// this is a fun one, passing in a list of type T will generate a typed dataset
        public static DataSet ToDataSet<T>(this IList<T> value)
        {
            if (value != null && value.Count > 0)
            {
                var type = typeof(T);
                var properties = type.GetProperties();
                ConstructorInfo ci = type.GetConstructor(new Type[] { });
                T item = (T)ci.Invoke(new object[] { });
                var ds = new DataSet();
                var dt = new DataTable();
                foreach (PropertyInfo property in properties)
                {
                    var row = value.FirstOrDefault();
                    var dc = new DataColumn()
                    {
                        Caption = property.Name,
                        ColumnName = property.Name
                    };
                    if (row.IsNotNull())
                    {
                        var itemValue = property.GetValue(row, null);
                        if (itemValue.IsNotNull())
                        {
                            dc.DataType = itemValue.GetType();
                        }
                    }
                    dt.Columns.Add(dc);
                }
                foreach (T listItem in value)
                {
                    var dr = dt.NewRow();
                    foreach (var property in properties)
                    {
                        var listItemValue = property.GetValue(listItem, null);
                        if(listItemValue != null)
                        {
                            dr[property.Name] = property.GetValue(listItem, null);
                        }
                    }
                    dt.Rows.Add(dr);
                }
                ds.Tables.Add(dt);
                return ds;
            }
            else
            {
                return new DataSet();
            }
        }

No comments:

Post a Comment