Example #1
0
        /// <summary>
        /// Writes a <see cref="char" /> value to the underlying <see cref="Stream" />.
        /// </summary>
        /// <param name="value">The non-surrogate <see cref="char" /> value to write.</param>
        public void Write(char value)
        {
            AssertCanWrite();

            Writer.Write(value);
            BytesWritten += Encoding.GetByteCount(SingletonCollection.Array(value));
        }
Example #2
0
        /// <summary>
        /// Executes a .NET executable from a <see cref="byte" />[] by invoking the main entry point. The Main method must either have no parameters or one <see cref="string" />[] parameter. If it has a parameter, <paramref name="args" /> is passed, otherwise <paramref name="args" /> is ignored.
        /// </summary>
        /// <param name="executable">A <see cref="byte" />[] that represents a .NET executable file.</param>
        /// <param name="args">A <see cref="string" />[] representing the arguments that is passed to the main entry point, if the Main method has a <see cref="string" />[] parameter.</param>
        /// <param name="thread"><see langword="true" /> to invoke the main entry point in a new thread.</param>
        public static void ExecuteDotNetAssembly(byte[] executable, string[] args, bool thread)
        {
            Check.ArgumentNull(executable, nameof(executable));

            MethodInfo method = Assembly.Load(executable).EntryPoint;

            ParameterInfo[] parameters = method.GetParameters();

            Action invoke;

            if (parameters.Length == 0)
            {
                invoke = () => method.Invoke();
            }
            else if (parameters.Length == 1 && parameters.First().ParameterType == typeof(string[]))
            {
                invoke = () => method.Invoke(null, SingletonCollection.Array(args ?? new string[0]));
            }
            else
            {
                throw Throw.InvalidOperation("Executable does not contain a static 'main' method suitable for an entry point.");
            }

            if (thread)
            {
                ThreadFactory.StartThread(invoke);
            }
            else
            {
                invoke();
            }
        }
Example #3
0
        /// <summary>
        /// Reads a <see cref="char" /> value from underlying <see cref="Stream" />.
        /// </summary>
        /// <returns>
        /// The next <see cref="char" /> read from the underlying <see cref="Stream" />.
        /// </returns>
        public char ReadChar()
        {
            AssertCanRead();

            char value = Reader.ReadChar();

            BytesRead += Encoding.GetByteCount(SingletonCollection.Array(value));
            return(value);
        }
Example #4
0
        /// <summary>
        /// Gets the commandline <see cref="string" /> of this <see cref="Process" /> that was passed during process creation.
        /// </summary>
        /// <param name="process">The <see cref="Process" /> to be checked.</param>
        /// <returns>
        /// The commandline <see cref="string" /> of this <see cref="Process" /> that was passed during process creation.
        /// </returns>
        public static string GetCommandLine(this Process process)
        {
            Check.ArgumentNull(process, nameof(process));

            return(new WmiNamespace("CIMV2", false, false)
                   .GetClass("Win32_Process", false)
                   .GetObjects(SingletonCollection.Array("CommandLine"), "ProcessId = " + process.Id)
                   .First()
                   .Properties["CommandLine"]
                   .GetValue <string>());
        }
        public async Task add_a_document()
        {
            // arrange
            var collection = new SingletonCollection <TestDocument>(_mongoDb);

            // act
            var id = await collection.CreateAsync(new TestDocument());

            // assert
            Assert.IsTrue(!string.IsNullOrWhiteSpace(id));
            Assert.IsNotNull(await collection.ReadOrDefaultAsync(id));
        }
        public async Task throw_exception_if_add_two_documents()
        {
            // arrange
            var collection = new SingletonCollection <TestDocument>(_mongoDb);

            if (!collection.GetAll().Any())
            {
                await collection.CreateAsync(new TestDocument());
            }

            // act
            Assert.ThrowsAsync <InvalidOperationException>(async() => await collection.CreateAsync(new TestDocument()));
        }
        public async Task return_single_document_if_exists()
        {
            // arrange
            var collection = new SingletonCollection <TestDocument>(_mongoDb);

            if (!collection.GetAll().Any())
            {
                await collection.CreateAsync(new TestDocument());
            }
            var docs = collection.GetAll();

            // act
            Assert.AreEqual(1, docs.Count());
        }
Example #8
0
        private MethodInfo CreateFunctionMethod(string name, CallingConvention callingConvention, CharSet charSet, Type returnType, Type[] parameterTypes)
        {
            string assemblyName = Path.GetFileNameWithoutExtension(DllName).ChangeCasing(StringCasing.CamelCase);

            TypeBuilder typeBuilder = AppDomain.CurrentDomain
                                      .DefineDynamicAssembly(new AssemblyName(assemblyName + "DynamicLibrary"), AssemblyBuilderAccess.Run)
                                      .DefineDynamicModule(assemblyName + "Module", false)
                                      .DefineType(assemblyName + "Imports", TypeAttributes.Class | TypeAttributes.Public);

            typeBuilder
            .DefinePInvokeMethod(name, DllName, name, MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, returnType, parameterTypes, callingConvention, charSet)
            .SetCustomAttribute(new CustomAttributeBuilder(typeof(DllImportAttribute).GetConstructor(SingletonCollection.Array(typeof(string))), SingletonCollection.Array(DllName)));

            return(typeBuilder.CreateType().GetMethod(name, BindingFlags.Static | BindingFlags.Public));
        }
Example #9
0
 /// <summary>
 /// Converts the specified <see cref="int" /> value to a <see cref="BitArray" />.
 /// </summary>
 /// <param name="value">The <see cref="int" /> value to retrieve the bits from.</param>
 /// <returns>
 /// An equivalent <see cref="BitArray" /> value containing all bits from <paramref name="value" />.
 /// </returns>
 public static BitArray GetBitArray(int value)
 {
     return(new BitArray(SingletonCollection.Array(value)));
 }
Example #10
0
        /// <summary>
        /// Produces the set difference of a sequence and one element by using the specified <see cref="IEqualityComparer{T}" />.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements of the input sequence and the second element.</typeparam>
        /// <param name="first">An <see cref="IEnumerable{T}" /> whose elements that are not equal to <paramref name="second" /> will be returned.</param>
        /// <param name="second">The second element, which will be removed from the returned sequence, if it also occurs in the first sequence.</param>
        /// <param name="comparer">An <see cref="IComparer{T}" /> to compare the elements.</param>
        /// <returns>
        /// An <see cref="IEnumerable{T}" /> that contains the set difference of the elements from the input sequence and the second element.
        /// </returns>
        public static IEnumerable <TSource> Except <TSource>(this IEnumerable <TSource> first, TSource second, IEqualityComparer <TSource> comparer)
        {
            Check.ArgumentNull(first, nameof(first));

            return(first.Except(SingletonCollection.List(second), comparer));
        }