示例#1
0
        public static TExtension GetRequiredExtension <TExtension>(this IExtensionManager extensionManager) where TExtension : IExtension
        {
            var found = extensionManager
                        .GetRegisteredExtensions()
                        .Where(extension => typeof(TExtension).IsAssignableFrom(extension.GetType()))
                        .FirstOrDefault()
                        .SelectOrDefault(x => (TExtension)(object)x);

            if (found == null)
            {
                throw new InvalidOperationException("Extension of type {0} has not been registered".InvariantCultureFormat(typeof(TExtension).FullName));
            }
            return(found);
        }
示例#2
0
        public static TExtension FindOrCreateExtension <TExtension>(this IExtensionManager extensionManager, Func <TExtension> factory) where TExtension : IExtension
        {
            var found = extensionManager
                        .GetRegisteredExtensions()
                        .Where(extension => typeof(TExtension).IsAssignableFrom(extension.GetType()))
                        .FirstOrDefault()
                        .SelectOrDefault(x => (TExtension)(object)x);

            if (found == null)
            {
                found = factory();
                extensionManager.RegisterExtension(found);
            }
            return(found);
        }