Sylloge
A C# helper library
code/Extensions/Extensions.cs
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 using System;
00017 using System.Collections;
00018 using System.Collections.Generic;
00019 using System.Text;
00020 using System.Windows.Forms;
00021 using System.Reflection;
00022 
00023 namespace Sylloge.Extensions
00024 {
00025     #region Type Extensions
00026     
00030     public static class TypeExtensions
00031     {
00035         public class TypeExtensionException : System.Exception
00036         {
00037             public TypeExtensionException(Type t) : base("Invalid input value when converting to " + t.ToString()) { }
00038         }
00039 
00045         public static bool IsDesignMode(this Control c)
00046         {
00047             return System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime;
00048         }
00049 
00056         public static ushort ToUShort(this string s)
00057         {
00058             ushort ret = 0;
00059             if (!ushort.TryParse(s, out ret)) {
00060                 throw new Sylloge.Extensions.TypeExtensions.TypeExtensionException(typeof(Int32));
00061             }
00062             return ret;
00063         }
00064 
00071         public static Int16 ToInt16(this string s)
00072         {
00073             Int16 ret = 0;
00074             if (!Int16.TryParse(s, out ret)) {
00075                 throw new Sylloge.Extensions.TypeExtensions.TypeExtensionException(typeof(Int32));
00076             }
00077             return ret;
00078         }
00079 
00086         public static Int32 ToInt32(this string s)
00087         {
00088             Int32 ret = 0;
00089             if (!Int32.TryParse(s, out ret)) {
00090                 throw new Sylloge.Extensions.TypeExtensions.TypeExtensionException(typeof(Int32));
00091             }
00092             return ret;
00093         }
00094 
00101         public static Int64 ToInt64(this string s)
00102         {
00103             Int64 ret = 0;
00104             if (!Int64.TryParse(s, out ret)) {
00105                 throw new Sylloge.Extensions.TypeExtensions.TypeExtensionException(typeof(Int64));
00106             }
00107             return ret;
00108         }
00109 
00115         public static bool IsNumeric(this string s)
00116         {
00117             if (string.IsNullOrEmpty(s)) { return false; }
00118             char[] arr = s.ToCharArray();
00119             if ((!char.IsDigit(arr[0])) && (arr[0] != '-')) { return false; }
00120             if (arr[0] == '-') {
00121                 string t = new string(arr, 1, arr.Length - 1);
00122                 arr = t.ToCharArray();
00123             }
00124             int pcount = 0;
00125             foreach (char c in arr) {
00126                 if (!char.IsDigit(c)) {
00127                     if (c == '.') {
00128                         if (++pcount > 1) { return false; }
00129                     } else {
00130                         return false;
00131                     }
00132                 }
00133             }
00134             return true;
00135         }
00136 
00143         public static bool HasFlag(this byte b, byte flag)
00144         {
00145             return ((b & flag) == flag);
00146         }
00147 
00154         public static bool HasFlag(this byte b, int flag)
00155         {
00156             return ((b & flag) == flag);
00157         }
00158 
00165         public static byte[] ToByteArray(this System.Drawing.Image img, string mimeType)
00166         {
00167             return Sylloge.Data.Imaging.ToByteArray(img, mimeType);
00168         }
00169 
00176         public static byte[] ToByteArray(this System.Drawing.Image img, System.Drawing.Imaging.ImageFormat format)
00177         {
00178             return Sylloge.Data.Imaging.ToByteArray(img, format);
00179         }
00180 
00187         public static byte[] ToByteArray(this System.Drawing.Image img)
00188         {
00189             return Sylloge.Data.Imaging.ToByteArray(img, new System.Drawing.Imaging.ImageFormat(img.RawFormat.Guid));
00190         }
00191     }
00192 
00193     #endregion
00194 }
 All Classes Namespaces Files Functions Variables Enumerations Properties Events