예제 #1
0
        /// <summary>
        /// Loads the logger.
        /// </summary>
        private void LoadLogger()
        {
            Type type = typeof(ILogger);

            if (this.Logger == null)
            {
                List <Type> availableTypes =
                    TypeFinder.GetAssembliesWithKnownExclusions()
                    .SelectMany(a => a.GetLoadableTypes())
                    .Where(t => type.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract)
                    .ToList();

                // There's more than one so load the first that is not our default.
                if (availableTypes.Count > 1)
                {
                    this.Logger = availableTypes.Where(l => l != typeof(DefaultLogger))
                                  .Select(f => (Activator.CreateInstance(f) as ILogger))
                                  .First();
                }
                else
                {
                    this.Logger = new DefaultLogger();
                }
            }
        }
        /// <summary>
        /// Creates a list, using reflection, of supported image formats that ImageProcessor can run.
        /// </summary>
        private void LoadSupportedImageFormats()
        {
            var formats = new List <ISupportedImageFormat>
            {
                new BitmapFormat(),
                new GifFormat(),
                new JpegFormat(),
                new PngFormat(),
                new TiffFormat()
            };

            var type = typeof(ISupportedImageFormat);

            if (SupportedImageFormats == null)
            {
                var availableTypes =
                    TypeFinder.GetAssembliesWithKnownExclusions()
                    .SelectMany(a => AssemblyExtensions.GetLoadableTypes(a))
                    .Where(t => type.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract)
                    .ToList();

                formats.AddRange(availableTypes.Select(f => Activator.CreateInstance(f) as ISupportedImageFormat).ToList());

                SupportedImageFormats = formats;
            }
        }
예제 #3
0
 static ExtensionMethodFinder()
 {
     AllExtensionMethods = TypeFinder.GetAssembliesWithKnownExclusions()
                           // assemblies that contain extension methods
                           .Where(a => a.IsDefined(typeof(ExtensionAttribute), false))
                           // types that contain extension methods
                           .SelectMany(a => a.GetTypes()
                                       .Where(t => t.IsDefined(typeof(ExtensionAttribute), false) && t.IsSealed && t.IsGenericType == false && t.IsNested == false))
                           // actual extension methods
                           .SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public)
                                       .Where(m => m.IsDefined(typeof(ExtensionAttribute), false)))
                           // and also IEnumerable<T> extension methods - because the assembly is excluded
                           .Concat(typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public))
                           .ToArray();
 }
예제 #4
0
        /// <summary>
        /// Creates a list, using reflection, of supported image formats that ImageProcessor can run.
        /// </summary>
        private void LoadSupportedImageFormats()
        {
            Type type = typeof(ISupportedImageFormat);

            if (this.SupportedImageFormats == null)
            {
                List <Type> availableTypes =
                    TypeFinder.GetAssembliesWithKnownExclusions()
                    .SelectMany(a => a.GetLoadableTypes())
                    .Where(t => type.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract)
                    .ToList();

                this.SupportedImageFormats =
                    availableTypes.Select(f => (Activator.CreateInstance(f) as ISupportedImageFormat))
                    .ToList();
            }
        }
예제 #5
0
        /// <summary>
        /// Returns all extension methods found matching the definition
        /// </summary>
        /// <param name="thisType"></param>
        /// <param name="name"></param>
        /// <param name="argumentCount"></param>
        /// <param name="argsContainsThis"></param>
        /// <returns></returns>
        /// <remarks>
        /// TODO: NOTE: This will be an intensive method to call!! Results should be cached!
        /// </remarks>
        private static IEnumerable <MethodInfo> GetAllExtensionMethods(Type thisType, string name, int argumentCount, bool argsContainsThis)
        {
            //only scan assemblies we know to contain extension methods (user assemblies)
            var assembliesToScan = TypeFinder.GetAssembliesWithKnownExclusions();

            //get extension methods from runtime
            var candidates = (
                from assembly in assembliesToScan
                where assembly.IsDefined(typeof(ExtensionAttribute), false)
                from type in assembly.GetTypes()
                where (type.IsDefined(typeof(ExtensionAttribute), false) &&
                       type.IsSealed && !type.IsGenericType && !type.IsNested)
                from method in type.GetMethods(BindingFlags.Static | BindingFlags.Public)
                // this filters extension methods
                where method.IsDefined(typeof(ExtensionAttribute), false)
                select method
                );

            //add the extension methods defined in IEnumerable
            candidates = candidates.Concat(typeof(IEnumerable).GetMethods(BindingFlags.Static | BindingFlags.Public));

            //filter by name
            var methodsByName = candidates.Where(m => m.Name == name);

            var isGenericAndRightParamCount = methodsByName.Where(m => m.GetParameters().Length == argumentCount + (argsContainsThis ? 0 : 1));

            //find the right overload that can take genericParameterType
            //which will be either DynamicNodeList or List<DynamicNode> which is IEnumerable`

            var withGenericParameterType = isGenericAndRightParamCount.Select(m => new { m, t = FirstParameterType(m) });

            var methodsWhereArgZeroIsTargetType = (from method in withGenericParameterType
                                                   where
                                                   method.t != null && MethodArgZeroHasCorrectTargetType(method.m, method.t, thisType)
                                                   select method);

            return(methodsWhereArgZeroIsTargetType.Select(mt => mt.m));
        }
        /// <summary>
        /// Returns the enumerable of all extension method info's in the app domain = USE SPARINGLY!!!
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// We cache this as a sliding 5 minute exiration, in unit tests there's over 1100 methods found, surely that will eat up a bit of memory so we want
        /// to make sure we give it back.
        /// </remarks>
        private static IEnumerable <MethodInfo> GetAllExtensionMethodsInAppDomain(IRuntimeCacheProvider runtimeCacheProvider)
        {
            if (runtimeCacheProvider == null)
            {
                throw new ArgumentNullException("runtimeCacheProvider");
            }

            return(runtimeCacheProvider.GetCacheItem <MethodInfo[]>(typeof(ExtensionMethodFinder).Name, () => TypeFinder.GetAssembliesWithKnownExclusions()
                                                                    // assemblies that contain extension methods
                                                                    .Where(a => a.IsDefined(typeof(ExtensionAttribute), false))
                                                                    // types that contain extension methods
                                                                    .SelectMany(a => a.GetTypes()
                                                                                .Where(t => t.IsDefined(typeof(ExtensionAttribute), false) && t.IsSealed && t.IsGenericType == false && t.IsNested == false))
                                                                    // actual extension methods
                                                                    .SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public)
                                                                                .Where(m => m.IsDefined(typeof(ExtensionAttribute), false)))
                                                                    // and also IEnumerable<T> extension methods - because the assembly is excluded
                                                                    .Concat(typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public))
                                                                    //If we don't do this then we'll be scanning all assemblies each time!
                                                                    .ToArray(),

                                                                    //only cache for 5 minutes
                                                                    timeout: TimeSpan.FromMinutes(5),

                                                                    //each time this is accessed it will be for 5 minutes longer
                                                                    isSliding: true));
        }