Ejemplo n.º 1
0
        /// <summary>
        /// Converts a String in the form 0011AABB to a Byte[] using the chars in the string as bytes to caulcate the decimal value.
        /// </summary>
        /// <notes>
        /// Reduced string allocations from managed version substring
        /// About 10 milliseconds faster then Managed when doing it 100,000 times. otherwise no change
        /// </notes>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] HexStringToBytes(string str, int start = 0, int length = -1, bool onlyLettersOrDigits = true)
        {
            //Dont check the results for overflow
            unchecked
            {
                if (length.Equals(Common.Binary.Zero))
                {
                    return(null);
                }

                if (length <= -1)
                {
                    length = str.Length;
                }

                int check = length - start;

                if (start > check)
                {
                    throw new System.ArgumentOutOfRangeException("start");
                }

                if (length > check)
                {
                    throw new System.ArgumentOutOfRangeException("length");
                }

                System.Collections.Generic.IEnumerable <byte> result = Media.Common.MemorySegment.EmptyBytes;

                //Iterate the pointer using the managed length ....
                //Todo, optomize with reverse or i - 1
                for (int i = start, e = length; i < e; i += 2)
                {
                    //to reduce string manipulations pre call
                    if (onlyLettersOrDigits && char.IsLetterOrDigit(str[i]).Equals(false))
                    {
                        //Back up 1
                        --i;

                        //Increase by 2
                        continue;
                    }

                    //Todo, Native and Unsafe

                    //Convert 2 Chars to a byte
                    result = System.Linq.Enumerable.Concat(result, LinqExtensions.Yield((byte)(HexCharToByte(str[i]) << 4 | HexCharToByte(str[i + 1]))));
                }

                //Dont use a List..

                //Return the bytes
                return(System.Linq.Enumerable.ToArray(result));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a delegate from a MethodInfo using <see cref="System.Linq.Expressions.Expression.GetDelegateType"/>
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static System.Delegate CreateDelegate(System.Reflection.MethodInfo method)
        {
            if (method == null)
            {
                throw new System.ArgumentNullException("method");
            }

            return(method.CreateDelegate(System.Linq.Expressions.Expression.GetDelegateType(method.GetParameters().Select(p => p.ParameterType).Concat(LinqExtensions.Yield(method.ReturnType)).ToArray())));
        }