123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- using System;
- using System.Collections;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Threading.Tasks;
- namespace WCS.Core
- {
- public static class Extentions
- {
- static ConcurrentDictionary<object, object> ExObjs = new ConcurrentDictionary<object, object>();
- public static T GetEx<T>(object entity)
- {
- lock (entity)
- {
- if (!ExObjs.ContainsKey(entity))
- {
- var obj = Activator.CreateInstance(typeof(T), entity);
- ExObjs[entity] = obj;
- }
- return (T)ExObjs[entity];
- }
- }
- public static PLC Ex(this PLCInfo source)
- {
- return GetEx<PLC>(source);
- }
- static ConcurrentDictionary<World, ConcurrentDictionary<object, object>> ExObjsOfWorld = new ConcurrentDictionary<World, ConcurrentDictionary<object, object>>();
- static T GetExOfWorld<T>(this object source, World world)
- {
- if (!ExObjsOfWorld.TryGetValue(world, out var objs))
- {
- objs = new ConcurrentDictionary<object, object>();
- ExObjsOfWorld[world] = objs;
- }
- if (!objs.TryGetValue(source, out var obj))
- {
- obj = Activator.CreateInstance(typeof(T), source);
- objs[source] = obj;
- }
- return (T)obj;
- }
- public static DataBlock Ex(this DBInfo source, World world)
- {
- return GetExOfWorld<DataBlock>(source, world);
- }
- public static DataBlock[] GetDataBlocks(this World source)
- {
- ExObjsOfWorld.TryGetValue(source, out var dic);
- var res= dic.Values.OfType<DataBlock>().ToArray();
- return res;
- }
- public static WorldEx Ex(this World source)
- {
- return GetEx<WorldEx>(source);
- }
- public static void AddSafe<T>(this List<T> source,T item)
- {
- lock (source)
- {
- source.Add(item);
- }
- }
- public static void AddSafe<T>(this List<T> source,IEnumerable<T> item)
- {
- lock (source)
- {
- source.AddRange(item);
- }
- }
- }
- public static class BitExtension
- {
- #region ushort
- /// <summary>
- /// 设置指定位置的位值的值
- /// </summary>
- /// <param name="value">ushort对象</param>
- /// <param name="position">指定位置</param>
- /// <param name="flag">值</param>
- /// <returns></returns>
- public static ushort SetBit(this ushort value, int position, bool flag)
- {
- return SetBits(value, position, 1, flag ? (byte)1 : (byte)0);
- }
- /// <summary>
- /// 批量设置指定位置的位值的值
- /// </summary>
- /// <param name="value">ushort对象</param>
- /// <param name="position">开始位置</param>
- /// <param name="length">长度</param>
- /// <param name="bits">值</param>
- /// <returns></returns>
- public static ushort SetBits(this ushort value, int position, int length, ushort bits)
- {
- if (length <= 0 || position >= 16) return value;
- var mask = (2 << (length - 1)) - 1;
- value &= (ushort)~(mask << position);
- value |= (ushort)((bits & mask) << position);
- return value;
- }
- /// <summary>
- /// 获取指定位置的值
- /// </summary>
- /// <param name="value">ushort对象</param>
- /// <param name="position">指定位置</param>
- /// <returns></returns>
- public static bool GetBit(this ushort value, int position)
- {
- return GetBits(value, position, 1) == 1;
- }
- /// <summary>
- /// 批量获取指定位置的值
- /// </summary>
- /// <param name="value">ushort对象</param>
- /// <param name="position">开始位值</param>
- /// <param name="length">长度</param>
- /// <returns></returns>
- public static ushort GetBits(this ushort value, int position, int length)
- {
- if (length <= 0 || position >= 16) return 0;
- var mask = (2 << (length - 1)) - 1;
- return (ushort)((value >> position) & mask);
- }
- #endregion ushort
- #region byte
- /// <summary>
- /// 设置指定位置的值
- /// </summary>
- /// <param name="value">byte对象</param>
- /// <param name="position">指定位置</param>
- /// <param name="flag">设置值</param>
- /// <returns></returns>
- public static byte SetBit(this byte value, int position, bool flag)
- {
- if (position >= 8) return value;
- var mask = (2 << (1 - 1)) - 1;
- value &= (byte)~(mask << position);
- value |= (byte)(((flag ? 1 : 0) & mask) << position);
- return value;
- }
- /// <summary>
- /// 获取指定位置的值
- /// </summary>
- /// <param name="value">byte对象</param>
- /// <param name="position">指定位置</param>
- /// <returns></returns>
- public static bool GetBit(this byte value, int position)
- {
- if (position >= 8) return false;
- var mask = (2 << (1 - 1)) - 1;
- return (byte)((value >> position) & mask) == 1;
- }
- #endregion byte
- #region uint
- /// <summary>
- /// 设置指定位置的位值的值
- /// </summary>
- /// <param name="value">uint对象</param>
- /// <param name="position">指定位置</param>
- /// <param name="flag">值</param>
- /// <returns></returns>
- public static uint SetBit(this uint value, int position, bool flag)
- {
- return SetBits(value, position, 1, flag ? (byte)1 : (byte)0);
- }
- /// <summary>
- /// 批量设置指定位置的位值的值
- /// </summary>
- /// <param name="value">uint对象</param>
- /// <param name="position">开始位置</param>
- /// <param name="length">长度</param>
- /// <param name="bits">值</param>
- /// <returns></returns>
- public static uint SetBits(this uint value, int position, int length, uint bits)
- {
- if (length <= 0 || position >= 32) return value;
- var mask = (2 << (length - 1)) - 1;
- value &= (uint)~(mask << position);
- value |= (uint)((bits & mask) << position);
- return value;
- }
- /// <summary>
- /// 获取指定位置的值
- /// </summary>
- /// <param name="value">uint对象</param>
- /// <param name="position">指定位置</param>
- /// <returns></returns>
- public static bool GetBit(this uint value, int position)
- {
- return GetBits(value, position, 1) == 1;
- }
- /// <summary>
- /// 批量获取指定位置的值
- /// </summary>
- /// <param name="value">uint对象</param>
- /// <param name="position">开始位值</param>
- /// <param name="length">长度</param>
- /// <returns></returns>
- public static uint GetBits(this uint value, int position, int length)
- {
- if (length <= 0 || position >= 32) return 0;
- var mask = (2 << (length - 1)) - 1;
- return (uint)((value >> position) & mask);
- }
- #endregion uint
- #region ulong
- /// <summary>
- /// 设置指定位置的位值的值
- /// </summary>
- /// <param name="value">ulong对象</param>
- /// <param name="position">指定位置</param>
- /// <param name="flag">值</param>
- /// <returns></returns>
- public static ulong SetBit(this ulong value, int position, bool flag)
- {
- return SetBits(value, position, 1, flag ? (byte)1 : (byte)0);
- }
- /// <summary>
- /// 批量设置指定位置的位值的值
- /// </summary>
- /// <param name="value">ulong对象</param>
- /// <param name="position">开始位置</param>
- /// <param name="length">长度</param>
- /// <param name="bits">值</param>
- /// <returns></returns>
- public static ulong SetBits(this ulong value, int position, int length, ulong bits)
- {
- if (length <= 0 || position >= 64) return value;
- var mask = (ulong)(2 << (length - 1)) - 1;
- value &= ~(mask << position);
- value |= (bits & mask) << position;
- return value;
- }
- /// <summary>
- /// 获取指定位置的值
- /// </summary>
- /// <param name="value">ulong对象</param>
- /// <param name="position">指定位置</param>
- /// <returns></returns>
- public static bool GetBit(this ulong value, int position)
- {
- return GetBits(value, position, 1) == 1;
- }
- /// <summary>
- /// 批量获取指定位置的值
- /// </summary>
- /// <param name="value">ulong对象</param>
- /// <param name="position">开始位值</param>
- /// <param name="length">长度</param>
- /// <returns></returns>
- public static ulong GetBits(this ulong value, int position, int length)
- {
- if (length <= 0 || position >= 64) return 0;
- var mask = (ulong)(2 << (length - 1)) - 1;
- return (value >> position) & mask;
- }
- #endregion ulong
- }
- public static class ExpressionExtensions
- {
- public static string ExpToString(this LambdaExpression source)
- {
- var str = "";
- try
- {
- str = LambdaToString(source);
- }
- catch
- {
- str = source.ToString();
- }
- var arr = str.Split("Convert(");
- arr = arr.Select(v =>
- {
- if (!v.Contains(','))
- return v;
- var index = v.IndexOf(',');
- var index2 = v.IndexOf(')', index);
- var sub = v.Substring(index, index2 - index + 1);
- var str = v.Replace(sub, "");
- return str;
- }).ToArray();
- var res = string.Join("", arr);
- return res;
- }
- public static string LambdaToString(LambdaExpression expression)
- {
- var replacements = new Dictionary<string, string>();
- WalkExpression(replacements, expression);
- string body = expression.ToString();
- foreach (var parm in expression.Parameters)
- {
- var parmName = parm.Name;
- var parmTypeName = parm.Type.Name;
- body = body.Replace(parmName + " =>", "(" + parmTypeName + " v) =>");
- }
- foreach (var replacement in replacements)
- {
- body = body.Replace(replacement.Key, replacement.Value);
- }
- return body;
- }
- private static void WalkExpression(Dictionary<string, string> replacements, Expression expression)
- {
- switch (expression.NodeType)
- {
- case ExpressionType.MemberAccess:
- string replacementExpression = expression.ToString();
- if (replacementExpression.Contains("value("))
- {
- string replacementValue = Expression.Lambda(expression).Compile().DynamicInvoke().ToString();
- if (!replacements.ContainsKey(replacementExpression))
- {
- replacements.Add(replacementExpression, replacementValue.ToString());
- }
- }
- break;
- case ExpressionType.GreaterThan:
- case ExpressionType.GreaterThanOrEqual:
- case ExpressionType.LessThan:
- case ExpressionType.LessThanOrEqual:
- case ExpressionType.OrElse:
- case ExpressionType.AndAlso:
- case ExpressionType.Equal:
- case ExpressionType.NotEqual:
- var bexp = expression as BinaryExpression;
- WalkExpression(replacements, bexp.Left);
- WalkExpression(replacements, bexp.Right);
- break;
- case ExpressionType.Call:
- var mcexp = expression as MethodCallExpression;
- foreach (var argument in mcexp.Arguments)
- {
- WalkExpression(replacements, argument);
- }
- break;
- case ExpressionType.Lambda:
- var lexp = expression as LambdaExpression;
- WalkExpression(replacements, lexp.Body);
- break;
- case ExpressionType.Constant:
- //do nothing
- break;
- case ExpressionType.Convert:
- var exp = expression as UnaryExpression;
- WalkExpression(replacements, exp.Operand);
- break;
- default:
- //Trace.WriteLine("Unknown type");
- break;
- }
- }
- static bool When<T>(this T source, Expression<Func<T, bool>> exp) where T : class
- {
- var str = exp.ExpToString();
- try
- {
- var res = exp.Compile().Invoke(source);
- str += res ? " 成立" : " 不成立";
- return res;
- }
- catch (Exception ex)
- {
- str += ex.GetBaseException().Message;
- throw;
- }
- finally
- {
- Console.WriteLine(str);
- Console.WriteLine("------------------------------------");
- }
- }
- }
- }
|