)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Extention ()

Public general use code classes and xml files that we've compiled and used over the years:

Extention methods for different class objects.

    1: using System;
    2: using System.Collections.Generic;
    3: using System.IO;
    4: using System.Linq;
    5: using System.Xml.Serialization;
    6:  
    7: ////////////////////////////////////////////////////////////////////////////
    8:  
    9: /// <summary publish="true">
   10: /// Extention methods for different class objects.
   11: /// </summary>
   12: /// 
   13: /// <remarks> 
   14: /// Copyright © 2001-2021 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   15: ///
   16: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   17: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   18: ///
   19: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   20: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   21: /// 
   22: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   23: /// 
   24: /// Copyright notice: This notice may not be removed or altered from any source distribution.
   25: /// </remarks>
   26:  
   27: public static class Extention
   28: {
   29:     private static Dictionary<int, int> listHashCodeToIndexDictionary = new Dictionary<int, int>();
   30:  
   31:     ////////////////////////////////////////////////////////////////////////////
   32:  
   33:     /// <summary>
   34:     ///
   35:     /// </summary>
   36:     public static T XmlDeserializeFromString<T>(this string objectData)
   37:     {
   38:         return (T)XmlDeserializeFromString(objectData, typeof(T));
   39:     }
   40:  
   41:     ////////////////////////////////////////////////////////////////////////////
   42:  
   43:     /// <summary>
   44:     ///
   45:     /// </summary>
   46:     public static object XmlDeserializeFromString(this string objectData, Type type)
   47:     {
   48:         var serializer = new XmlSerializer(type);
   49:         object result;
   50:  
   51:         using (TextReader reader = new StringReader(objectData))
   52:         {
   53:             result = serializer.Deserialize(reader);
   54:         }
   55:  
   56:         return result;
   57:     }
   58:  
   59:     ////////////////////////////////////////////////////////////////////////////
   60:  
   61:     /// <summary>
   62:     /// Extends strings to return bool values
   63:     /// <see href="http://stackoverflow.com/questions/16205436/convert-toboolean-fails-with-0-value"/>
   64:     /// </summary>
   65:     public static bool ToBool(this string str)
   66:     {
   67:         bool b;
   68:         string cleanValue;
   69:  
   70:         cleanValue = (str ?? "").Trim();
   71:  
   72:         if (string.Equals(cleanValue, "False", StringComparison.OrdinalIgnoreCase)) b = false;
   73:         else b = (string.Equals(cleanValue, "True", StringComparison.OrdinalIgnoreCase)) || (cleanValue != "0");
   74:  
   75:         return b;
   76:     }
   77:  
   78:     /*
   79:     ////////////////////////////////////////////////////////////////////////////
   80: 
   81:     /// <summary>
   82:     ///
   83:     /// </summary>
   84:     public static bool IsInMenuPath(this SiteMapNode thisnode, SiteMapNode node)
   85:     {
   86:         var temp = node;
   87: 
   88:         while (temp != null && temp != thisnode) temp = temp.ParentNode;
   89: 
   90:         return temp != null;
   91:     }
   92:     */
   93:  
   94:     ////////////////////////////////////////////////////////////////////////////
   95:     ////////////////////////////////////////////////////////////////////////////
   96:  
   97:     /// <summary>
   98:     /// Pick a random value from List
   99:     ///<see href="http://stackoverflow.com/questions/2019417/access-random-item-in-list"/>
  100:     /// </summary>
  101:     public static T PickRandom<T>(this IEnumerable<T> source)
  102:     {
  103:         return source.PickRandom(1).Single();
  104:     }
  105:  
  106:     ////////////////////////////////////////////////////////////////////////////
  107:  
  108:     /// <summary>
  109:     ///
  110:     /// </summary>
  111:     public static IEnumerable<T> PickRandom<T>(this IEnumerable<T> source, int count)
  112:     {
  113:         return source.Shuffle().Take(count);
  114:     }
  115:  
  116:     ////////////////////////////////////////////////////////////////////////////
  117:  
  118:     /// <summary>
  119:     ///
  120:     /// </summary>
  121:     public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
  122:     {
  123:         return source.OrderBy(x => Guid.NewGuid());
  124:     }
  125:  
  126:     ////////////////////////////////////////////////////////////////////////////
  127:     ////////////////////////////////////////////////////////////////////////////
  128:  
  129:     /// <summary>
  130:     ///
  131:     /// </summary>
  132:     public static bool JustStartedOrRolledOver<T>(this IList<T> list)
  133:     {
  134:         return JustStarted(list) || RolledOver(list);
  135:     }
  136:  
  137:     ////////////////////////////////////////////////////////////////////////////
  138:  
  139:     /// <summary>
  140:     ///
  141:     /// </summary>
  142:     private static bool JustStarted<T>(this IList<T> list)
  143:     {
  144:         var listHashCode = list.GetHashCode();
  145:  
  146:         if (!listHashCodeToIndexDictionary.ContainsKey(listHashCode))
  147:         {
  148:             listHashCodeToIndexDictionary[listHashCode] = 0;
  149:         }
  150:  
  151:         return listHashCodeToIndexDictionary[listHashCode] == 0;
  152:     }
  153:  
  154:     ////////////////////////////////////////////////////////////////////////////
  155:  
  156:     /// <summary>
  157:     ///
  158:     /// </summary>
  159:     private static bool RolledOver<T>(this IList<T> list)
  160:     {
  161:         var listHashCode = list.GetHashCode();
  162:  
  163:         if (!listHashCodeToIndexDictionary.ContainsKey(listHashCode))
  164:         {
  165:             listHashCodeToIndexDictionary[listHashCode] = 0;
  166:         }
  167:  
  168:         return listHashCodeToIndexDictionary[listHashCode] % list.Count == 0;
  169:     }
  170:  
  171:     ////////////////////////////////////////////////////////////////////////////
  172:  
  173:     /// <summary>
  174:     ///
  175:     /// </summary>
  176:     public static T Next<T>(this IList<T> list)
  177:     {
  178:         var listHashCode = list.GetHashCode();
  179:  
  180:         if (!listHashCodeToIndexDictionary.ContainsKey(listHashCode))
  181:         {
  182:             listHashCodeToIndexDictionary[listHashCode] = 0;
  183:         }
  184:  
  185:         return list[listHashCodeToIndexDictionary[listHashCode]++ % list.Count];
  186:     }
  187:  
  188:     ////////////////////////////////////////////////////////////////////////////
  189:  
  190:     /// <summary>
  191:     ///
  192:     /// </summary>
  193:     public static T RandomThenNext<T>(this IList<T> list)
  194:     {
  195:         var listHashCode = list.GetHashCode();
  196:  
  197:         if (!listHashCodeToIndexDictionary.ContainsKey(listHashCode))
  198:         {
  199:             listHashCodeToIndexDictionary[listHashCode] = Ia.Cl.Models.Default.Random(list.Count);
  200:         }
  201:  
  202:         return list[listHashCodeToIndexDictionary[listHashCode]++ % list.Count];
  203:     }
  204:  
  205:     ////////////////////////////////////////////////////////////////////////////
  206:  
  207:     /// <summary>
  208:     ///
  209:     /// </summary>
  210:     public static T Next<T>(this IList<T> list, out int index, out int count)
  211:     {
  212:         var listHashCode = list.GetHashCode();
  213:  
  214:         if (!listHashCodeToIndexDictionary.ContainsKey(listHashCode))
  215:         {
  216:             listHashCodeToIndexDictionary[listHashCode] = 0;
  217:         }
  218:  
  219:         count = list.Count;
  220:         index = listHashCodeToIndexDictionary[listHashCode] % list.Count;
  221:  
  222:         var t = Next(list);
  223:  
  224:         return t;
  225:     }
  226:  
  227:     ////////////////////////////////////////////////////////////////////////////
  228:  
  229:     /// <summary>
  230:     ///
  231:     /// </summary>
  232:     public static T Next<T>(this IList<T> list, int index)
  233:     {
  234:         var i = index;
  235:  
  236:         return Next(list, ref i);
  237:     }
  238:  
  239:     ////////////////////////////////////////////////////////////////////////////
  240:  
  241:     /// <summary>
  242:     ///
  243:     /// </summary>
  244:     public static T Next<T>(this IList<T> list, ref int index, out int index2, out int count)
  245:     {
  246:         var listHashCode = list.GetHashCode();
  247:  
  248:         if (!listHashCodeToIndexDictionary.ContainsKey(listHashCode))
  249:         {
  250:             listHashCodeToIndexDictionary[listHashCode] = 0;
  251:         }
  252:  
  253:         count = list.Count;
  254:         index2 = index;
  255:  
  256:         var t = Next(list, ref index);
  257:  
  258:         return t;
  259:     }
  260:  
  261:     ////////////////////////////////////////////////////////////////////////////
  262:  
  263:     /// <summary>
  264:     ///
  265:     /// </summary>
  266:     public static T Next<T>(this IList<T> list, ref int index)
  267:     {
  268:         var listHashCode = list.GetHashCode();
  269:  
  270:         if (!listHashCodeToIndexDictionary.ContainsKey(listHashCode))
  271:         {
  272:             listHashCodeToIndexDictionary[listHashCode] = 0;
  273:         }
  274:  
  275:         if (index < 0 || index >= list.Count) throw new ArgumentOutOfRangeException("Index value: " + index + " is not within list range: 0," + (list.Count - 1));
  276:  
  277:         listHashCodeToIndexDictionary[listHashCode] = index;
  278:  
  279:         index = ++index % list.Count;
  280:  
  281:         return list[listHashCodeToIndexDictionary[listHashCode]++ % list.Count];
  282:     }
  283:  
  284:     ////////////////////////////////////////////////////////////////////////////
  285:  
  286:     /// <summary>
  287:     ///
  288:     /// </summary>
  289:     public static T NextOf<T>(this IList<T> list, T item)
  290:     {
  291:         return list[(list.IndexOf(item) + 1) == list.Count ? 0 : (list.IndexOf(item) + 1)];
  292:     }
  293:  
  294:     ////////////////////////////////////////////////////////////////////////////
  295:     ////////////////////////////////////////////////////////////////////////////
  296:  
  297:     /// <summary>
  298:     /// List get next element or get the first
  299:     ///<see href="https://stackoverflow.com/questions/22595655/how-to-do-a-dictionary-reverse-lookup"/>
  300:     /// </summary>
  301:     public static Dictionary<TValue, TKey> Reverse<TKey, TValue>(this IDictionary<TKey, TValue> source)
  302:     {
  303:         var dictionary = new Dictionary<TValue, TKey>();
  304:  
  305:         foreach (var entry in source)
  306:         {
  307:             if (entry.Value != null)
  308:             {
  309:                 if (!dictionary.ContainsKey(entry.Value))
  310:                 {
  311:                     dictionary.Add(entry.Value, entry.Key);
  312:                 }
  313:             }
  314:         }
  315:  
  316:         return dictionary;
  317:     }
  318:  
  319:     ////////////////////////////////////////////////////////////////////////////
  320:     ////////////////////////////////////////////////////////////////////////////
  321:  
  322:     public static IEnumerable<T> Flatten<T>(this T source, Func<T, IEnumerable<T>> selector)
  323:     {
  324:         // How to search Hierarchical Data with Linq
  325:         // https://stackoverflow.com/questions/18165460/how-to-search-hierarchical-data-with-linq
  326:  
  327:         return selector(source).SelectMany(c => Flatten(c, selector))
  328:                                .Concat(new[] { source });
  329:     }
  330:  
  331:     ////////////////////////////////////////////////////////////////////////////
  332:     ////////////////////////////////////////////////////////////////////////////
  333: }