diff --git a/src/System.Windows.Forms/tests/IntegrationTests/DesignSurface/DesignSurfaceExt/DesignSurfaceExt.cs b/src/System.Windows.Forms/tests/IntegrationTests/DesignSurface/DesignSurfaceExt/DesignSurfaceExt.cs index 5aa09878e2f..80a0232ad53 100644 --- a/src/System.Windows.Forms/tests/IntegrationTests/DesignSurface/DesignSurfaceExt/DesignSurfaceExt.cs +++ b/src/System.Windows.Forms/tests/IntegrationTests/DesignSurface/DesignSurfaceExt/DesignSurfaceExt.cs @@ -309,15 +309,11 @@ private void InitServices() //- We can leave the default services in their present state, //- or we can remove them and replace them with our own. //- Now add our own services using IServiceContainer - //- - //- //- Note //- before loading the root control in the design surface //- we must add an instance of naming service to the service container. //- otherwise the root component did not have a name and this caused //- troubles when we try to use the UndoEngine - //- - //- //- 1. NameCreationService _nameCreationService = new NameCreationServiceImp(); if (_nameCreationService is not null) @@ -326,8 +322,6 @@ private void InitServices() ServiceContainer.AddService(typeof(INameCreationService), _nameCreationService); } - //- - //- //- 2. CodeDomComponentSerializationService _codeDomComponentSerializationService = new CodeDomComponentSerializationService(ServiceContainer); if (_codeDomComponentSerializationService is not null) @@ -337,8 +331,6 @@ private void InitServices() ServiceContainer.AddService(typeof(ComponentSerializationService), _codeDomComponentSerializationService); } - //- - //- //- 3. IDesignerSerializationService _designerSerializationService = new DesignerSerializationServiceImpl(ServiceContainer); if (_designerSerializationService is not null) @@ -348,8 +340,6 @@ private void InitServices() ServiceContainer.AddService(typeof(IDesignerSerializationService), _designerSerializationService); } - //- - //- //- 4. UndoEngine _undoEngine = new UndoEngineExt(ServiceContainer); //- disable the UndoEngine @@ -361,10 +351,11 @@ private void InitServices() ServiceContainer.AddService(typeof(UndoEngine), _undoEngine); } - //- - //- //- 5. IMenuCommandService ServiceContainer.AddService(typeof(IMenuCommandService), new MenuCommandService(this)); + + //- 6. ITypeDiscoveryService + ServiceContainer.AddService(typeof(ITypeDiscoveryService), new TypeDiscoveryService()); } //- do some Edit menu command using the MenuCommandService diff --git a/src/System.Windows.Forms/tests/IntegrationTests/DesignSurface/DesignSurfaceExt/TypeDiscoveryService.cs b/src/System.Windows.Forms/tests/IntegrationTests/DesignSurface/DesignSurfaceExt/TypeDiscoveryService.cs new file mode 100644 index 00000000000..7550989a417 --- /dev/null +++ b/src/System.Windows.Forms/tests/IntegrationTests/DesignSurface/DesignSurfaceExt/TypeDiscoveryService.cs @@ -0,0 +1,55 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections; +using System.Collections.Concurrent; +using System.Collections.Immutable; +using System.Reflection; + +namespace System.ComponentModel.Design; + +/// +/// This service is requested by when asking for type information for a component. +/// This is a sample implementation suitable for this sample application. +/// +internal class TypeDiscoveryService : ITypeDiscoveryService +{ + public TypeDiscoveryService() { } + + private readonly ConcurrentDictionary> _discoveredTypesCache = new(); + + public ICollection GetTypes(Type baseType, bool excludeGlobalTypes) + { + return baseType is null + ? throw new ArgumentNullException(nameof(baseType)) + : (ICollection)_discoveredTypesCache.GetOrAdd(baseType, type => FindTypes(type, AppDomain.CurrentDomain.GetAssemblies())); + + static ImmutableArray FindTypes(Type baseType, Assembly[] assemblies) + { + var builder = ImmutableArray.CreateBuilder(); + + foreach (var assembly in assemblies) + { + Type[] types; + try + { + types = assembly.GetTypes(); + } + catch (ReflectionTypeLoadException exception) + { + types = exception.Types!; + } + + foreach (var type in types) + { + if (baseType.IsAssignableFrom(type)) + { + builder.Add(type); + } + } + } + + return builder.ToImmutable(); + } + } +}