/// <summary>
        /// Initializes <see cref="EndiannessSwapper{T}"/> by generating the appropriate swapping method.
        /// </summary>
        unsafe static EndiannessSwapper()
        {
            var valueParam  = Expression.Parameter(typeof(WrappedPointer <T>), "value");
            var resultParam = Expression.Parameter(typeof(WrappedPointer <T>), "result");

            var indexer = typeof(WrappedPointer <T>).GetProperty("Item");

            IEnumerable <Expression> GetBody(int byteCount)
            {
                for (var offset = 0; offset < byteCount; offset++)
                {
                    var valueOffset = Expression.Constant(offset);
                    var accessValue = Expression.MakeIndex(instance: valueParam,
                                                           indexer: indexer,
                                                           arguments: System.Linq.Enumerable.Repeat(valueOffset, 1));

                    var resultOffset = Expression.Constant(byteCount - offset - 1);
                    var accessResult = Expression.MakeIndex(instance: resultParam,
                                                            indexer: indexer,
                                                            arguments: System.Linq.Enumerable.Repeat(resultOffset, 1));

                    yield return(Expression.Assign(accessResult, accessValue));
                }
            }

            var body = Expression.Block(GetBody(sizeof(T)));

            Swapper = Expression.Lambda <SwapMethod>(body, valueParam, resultParam)
                      .Compile();
        }
Exemple #2
0
 /// <summary> Swap object A and B using the chosen SwapMethod. </summary>
 /// <param name="swapWhich"></param>
 public void Swap(SwapMethod swapWhich)
 {
     if (this.CanSwap())
     {
         if (swapWhich == SwapMethod.Objects)
         {
             this.Log("Swapping " + this.objectA.name + " and " + this.objectB.name + ".");
             this.SwapObjects();
         }
         else if (swapWhich == SwapMethod.Children)
         {
             this.Log("Swapping children of " + this.objectA.name + " and " + this.objectB.name + ".");
             this.SwapChildren();
         }
     }
 }