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