Esempio n. 1
0
        /// <summary>
        /// Copy the contents of one <see cref="Stream"/> to another.
        /// </summary>
        /// <param name="source">The stream to source data from.</param>
        /// <param name="destination">The stream to write data to.</param>
        /// <param name="buffer">The buffer to use during copying.</param>
        /// <remarks>This is taken from SharpZipLib.Core.StreamUtils and modified for progress callbacks.</remarks>
        protected void Copy(Stream source, Stream destination, byte[] buffer)
        {
            PscxArgumentException.ThrowIfIsNull(source, "source");
            PscxArgumentException.ThrowIfIsNull(destination, "destination");
            PscxArgumentException.ThrowIfIsNull(buffer, "buffer");
            PscxArgumentException.ThrowIf(buffer.Length < 128, "Buffer is too small");

            long sourceLength = source.Length;
            long bytesWritten = 0;

            bool copying = true;

            Debug.WriteLine("StartCopy", "WriterBase.Copy");
            while (copying)
            {
                int bytesRead = source.Read(buffer, 0, buffer.Length);
                if (bytesRead > 0)
                {
                    destination.Write(buffer, 0, bytesRead);
                    bytesWritten += bytesRead;

                    OnWriteProgress(sourceLength, bytesWritten);
                }
                else
                {
                    destination.Flush();
                    copying = false;
                }
            }
            OnWriteComplete();
            Debug.WriteLine("EndCopy", "WriterBase.Copy");
        }
Esempio n. 2
0
        private Object Read(BinaryParser parser, FieldInfo field, Type type)
        {
            PscxArgumentException.ThrowIf((type == field.DeclaringType), "Parsing field {0} would loop forever.", field);

            if (IsRecord(type))
            {
                return(new RecordParser(type).Parse(parser));
            }

            if (type == typeof(String))
            {
                return(ReadString(parser, field));
            }

            if (type == typeof(DateTime))
            {
                return(ReadDateTime(parser, field));
            }

            if (type == typeof(Version))
            {
                return(ReadVersion(parser, field));
            }

            return(ReadIntegral(parser, type));
        }
Esempio n. 3
0
        public static NonSIUnit Parse(String str)
        {
            NonSIUnit result;
            Boolean   success = TryParse(str, out result);

            PscxArgumentException.ThrowIf(!success, Resources.Errors.UnknownNonSIUnit, str);

            return(result);
        }
Esempio n. 4
0
        /// <summary>
        /// Registers a delegate called when specified type of object
        /// is read from the pipeline.
        /// </summary>
        /// <remarks>
        /// You may register an interface type too, but they are processed only when
        /// there is no handler available for the actual type.
        /// </remarks>
        /// <example>
        /// <para>For example, if you register the following types, the IDisposable handler
        /// will be executed only on objects which do not inherit from Stream.</para>
        /// <code>
        ///     RegisterInputType&lt;IDisposable&gt;(/* ... */);
        ///     RegisterInputType&lt;FileStream&gt;(/* ... */);
        ///     RegisterInputType&lt;Stream&gt;(/* ... */);
        /// </code>
        /// </example>
        /// <typeparam name="T">The type which should be processed. </typeparam>
        /// <param name="action">
        /// The delegate that is called when the object is available.
        /// </param>
        protected void RegisterInputType <T>(Action <T> action)
        {
            PscxArgumentException.ThrowIfIsNull(action,
                                                "To ignore an input type, use IgnoreInputType<T>().");
            PscxArgumentException.ThrowIf(typeof(T) == typeof(Object),
                                          "You cannot register an action for Object. Override the ProcessInputObject method instead.");
            PscxArgumentException.ThrowIf(typeof(T) == typeof(PSObject),
                                          "You cannot register an action for PSObject. Override the ProcessInputObject method instead.");

            if (_recurseIEnumerable)
            {
                PscxArgumentException.ThrowIf(typeof(T) == typeof(IEnumerable),
                                              "You cannot register an action for IEnumerable when ProcessIEnumerableRecursively is true.");
            }

            SetTypeAction(typeof(T), action);
        }
Esempio n. 5
0
        private Delegate CreateDelegate(Type type)
        {
            if (type.IsAbstract)
            {
                PscxArgumentOutOfRangeException.Throw("Type {0} is abstract.", type.Name);
            }
            // need a target for delegate
            PscxArgumentException.ThrowIf(this._arguments.Length == 0,
                                          "Please supply a ScriptBlock as an argument.");

            // must be a scriptblock
            ScriptBlock target = this._arguments[0] as ScriptBlock;

            PscxArgumentException.ThrowIfIsNull(target, "The only argument in ArgumentList must be a ScriptBlock.");

            Delegate invoker = GetScriptBlockDelegate(target, type);

            return(invoker);
        }
Esempio n. 6
0
        // this took blood, sweat and tears AND my first born.
        private static Delegate GetScriptBlockDelegate(ScriptBlock block, Type delegateType)
        {
            PscxArgumentException.ThrowIfIsNull(block, "block");
            PscxArgumentException.ThrowIfIsNull(delegateType, "block");

            bool isDelegate = typeof(Delegate).IsAssignableFrom(delegateType);

            PscxArgumentException.ThrowIf((!isDelegate) || (delegateType.IsAbstract),
                                          "Invalid delegateType: {0}", delegateType.Name);

            MethodInfo invoke = delegateType.GetMethod("Invoke");

            Debug.Assert(invoke != null, "delegate invoke != null");

            ParameterInfo[] parameters = invoke.GetParameters();
            Type            returnType = invoke.ReturnParameter.ParameterType;

            List <Type> args = new List <Type>();

            args.Add(typeof(ScriptBlock)); // first argument is instance
            foreach (ParameterInfo parameter in parameters)
            {
                args.Add(parameter.ParameterType);
            }

            DynamicMethod method = new DynamicMethod(String.Empty, returnType, args.ToArray(),
                                                     typeof(ScriptBlock).Module);

            ILGenerator ilgen = method.GetILGenerator();

            LocalBuilder[] locals = new LocalBuilder[2]
            {
                ilgen.DeclareLocal(typeof(object[]), true),
                ilgen.DeclareLocal(typeof(object[]), true)
            };

            ilgen.Emit(OpCodes.Ldc_I4, parameters.Length);
            ilgen.Emit(OpCodes.Newarr, typeof(object));
            ilgen.Emit(OpCodes.Stloc, 1);

            for (int index = 1; index < args.Count; index++)
            {
                ilgen.Emit(OpCodes.Ldloc, 1);
                EmitFastPushInt(ilgen, (index - 1)); // e.g. Ldc_I4_1
                ilgen.Emit(OpCodes.Ldarg, index);
                if (args[index].IsValueType)
                {
                    ilgen.Emit(OpCodes.Box, args[index]);
                }
                ilgen.Emit(OpCodes.Stelem_Ref);
            }
            ilgen.Emit(OpCodes.Ldloc_1);
            ilgen.Emit(OpCodes.Stloc_0);

            ilgen.Emit(OpCodes.Ldarg_0); // this
            ilgen.Emit(OpCodes.Ldloc_0); // object[] args for script block
            ilgen.EmitCall(OpCodes.Callvirt, typeof(ScriptBlock).GetMethod("InvokeReturnAsIs"), null);

            if (invoke.ReturnType == typeof(void))
            {
                ilgen.Emit(OpCodes.Pop);
            }
            else
            {
                // need to convert return type to the target delegate's return type
                // ...
            }

            ilgen.Emit(OpCodes.Ret);

            return(method.CreateDelegate(delegateType, block));
        }