Set of systems tools.

The class is a container of different system tools, which are used across the framework. Some of these tools are platform specific, so their implementation is different on different platform, like .NET and Mono.

コード例 #1
0
        /// <summary>
        ///   Gets the underlying buffer position for a StreamReader.
        /// </summary>
        ///
        /// <param name="reader">A StreamReader whose position will be retrieved.</param>
        ///
        /// <returns>The current offset from the beginning of the
        ///   file that the StreamReader is currently located into.</returns>
        ///
        public static long GetPosition(this StreamReader reader)
        {
            // http://stackoverflow.com/a/17457085/262032

#if NETSTANDARD1_4 || NETSTANDARD2_0
            var    type       = typeof(StreamReader).GetTypeInfo();
            char[] charBuffer = (char[])type.GetDeclaredField("_charBuffer").GetValue(reader);
            int    charPos    = (int)type.GetDeclaredField("_charPos").GetValue(reader);
            int    byteLen    = (int)type.GetDeclaredField("_byteLen").GetValue(reader);
#else
            var type = typeof(StreamReader);

            char[] charBuffer;
            int    charPos;
            int    byteLen;

            if (SystemTools.IsRunningOnMono() && type.GetField("decoded_buffer") != null)
            {
                // Mono's StreamReader source code is at: https://searchcode.com/codesearch/view/26576619/

                // The current buffer of decoded characters
                charBuffer = (char[])GetField(reader, "decoded_buffer");

                // The current position in the buffer of decoded characters
                charPos = (int)GetField(reader, "decoded_count");

                // The number of encoded bytes that are in the current buffer
                byteLen = (int)GetField(reader, "buffer_size");
            }
            else
            {
                // The current buffer of decoded characters
                charBuffer = (char[])GetField(reader, "charBuffer");

                // The current position in the buffer of decoded characters
                charPos = (int)GetField(reader, "charPos");

                // The number of encoded bytes that are in the current buffer
                byteLen = (int)GetField(reader, "byteLen");
            }
#endif

            // The number of bytes that the already-read characters need when encoded.
            int numReadBytes = reader.CurrentEncoding.GetByteCount(charBuffer, 0, charPos);

            return(reader.BaseStream.Position - byteLen + numReadBytes);
        }
コード例 #2
0
        /// <summary>
        ///   Populates the dictionary with available decoders of a particular category by
        ///   inspecting types from all referenced assemblies. Note: calling this method
        ///   will force all referenced assemblies to be loaded into the current AppDomain.
        /// </summary>
        ///
        /// <typeparam name="T">The base type for the decoders. This should be an interface such as IImageDecoder or IAudioDecoder.</typeparam>
        ///
        /// <param name="dictionary">The dictionary where the found decoders will be stored.</param>
        /// <param name="extension">The extension we are interested in.</param>
        ///
        public static void PopulateDictionaryWithDecodersFromAllAssemblies <T>(Dictionary <string, Type> dictionary, string extension)
        {
            lock (dictionary)
            {
                extension = extension.ToUpperInvariant();
                if (dictionary.ContainsKey(extension))
                {
                    return;
                }

                var decoderTypes = new List <Tuple <Type, FormatDecoderAttribute[]> >();

#if NETSTANDARD1_4
                TypeInfo baseType = typeof(T).GetTypeInfo();

                foreach (Type t in baseType.Assembly.ExportedTypes)
                {
                    TypeInfo ti         = t.GetTypeInfo();
                    var      attributes = ti.GetCustomAttributes(typeof(FormatDecoderAttribute), true).ToArray();

                    if (attributes != null && attributes.Length > 0 && baseType.IsAssignableFrom(ti))
                    {
                        FormatDecoderAttribute[] at = attributes.Cast <FormatDecoderAttribute>().ToArray();
                        decoderTypes.Add(Tuple.Create(t, at));
                    }
                }
#else
                Type baseType = typeof(T);

                if (SystemTools.IsRunningOnMono())
                {
                    Console.WriteLine("FormatDecoderAttribute: Running on Mono ({0})", typeof(T));
                    foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
                    {
                        foreach (Type t in a.GetTypes())
                        {
                            var attributes = t.GetCustomAttributes(typeof(FormatDecoderAttribute), true);

                            if (attributes != null && attributes.Length > 0 && baseType.IsAssignableFrom(t))
                            {
                                FormatDecoderAttribute[] at = attributes.Cast <FormatDecoderAttribute>().ToArray();
                                decoderTypes.Add(Tuple.Create(t, at));
                            }
                        }
                    }
                    Console.WriteLine("FormatDecoderAttribute: Found {0} {1} decoders.", decoderTypes.Count, typeof(T));
                }
                else
                {
                    foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
                    {
                        foreach (AssemblyName referencedName in a.GetReferencedAssemblies())
                        {
                            Assembly referencedAssembly = Assembly.Load(referencedName);

                            foreach (Type t in referencedAssembly.GetTypes())
                            {
                                var attributes = t.GetCustomAttributes(typeof(FormatDecoderAttribute), true);

                                if (attributes != null && attributes.Length > 0 && baseType.IsAssignableFrom(t))
                                {
                                    FormatDecoderAttribute[] at = attributes.Cast <FormatDecoderAttribute>().ToArray();
                                    decoderTypes.Add(Tuple.Create(t, at));
                                }
                            }
                        }
                    }
                }
#endif

                foreach (Tuple <Type, FormatDecoderAttribute[]> pair in decoderTypes)
                {
                    foreach (FormatDecoderAttribute attr in pair.Item2)
                    {
                        extension = attr.Extension.ToUpperInvariant();
                        if (!dictionary.ContainsKey(extension))
                        {
                            dictionary.Add(extension, pair.Item1);
                        }
                    }
                }
            }
        }