Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the ITypeDiscoveryService service #10247

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -326,8 +322,6 @@ private void InitServices()
ServiceContainer.AddService(typeof(INameCreationService), _nameCreationService);
}

//-
//-
//- 2. CodeDomComponentSerializationService
_codeDomComponentSerializationService = new CodeDomComponentSerializationService(ServiceContainer);
if (_codeDomComponentSerializationService is not null)
Expand All @@ -337,8 +331,6 @@ private void InitServices()
ServiceContainer.AddService(typeof(ComponentSerializationService), _codeDomComponentSerializationService);
}

//-
//-
//- 3. IDesignerSerializationService
_designerSerializationService = new DesignerSerializationServiceImpl(ServiceContainer);
if (_designerSerializationService is not null)
Expand All @@ -348,8 +340,6 @@ private void InitServices()
ServiceContainer.AddService(typeof(IDesignerSerializationService), _designerSerializationService);
}

//-
//-
//- 4. UndoEngine
_undoEngine = new UndoEngineExt(ServiceContainer);
//- disable the UndoEngine
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// This service is requested by <see cref="TypeDescriptor" /> when asking for type information for a component.
/// This is a sample implementation suitable for this sample application.
/// </summary>
internal class TypeDiscoveryService : ITypeDiscoveryService
{
public TypeDiscoveryService() { }

private readonly ConcurrentDictionary<Type, ImmutableArray<Type>> _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<Type> FindTypes(Type baseType, Assembly[] assemblies)
{
var builder = ImmutableArray.CreateBuilder<Type>();

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();
}
}
}
Loading