-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Hisham Bin Ateya <[email protected]>
- Loading branch information
Showing
9 changed files
with
153 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 49 additions & 7 deletions
56
...ardCore/OrchardCore.Abstractions/Shell/Builders/Extensions/ServiceDescriptorExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,76 @@ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
#nullable enable | ||
|
||
namespace OrchardCore.Environment.Shell.Builders | ||
{ | ||
public static class ServiceDescriptorExtensions | ||
{ | ||
public static Type GetImplementationType(this ServiceDescriptor descriptor) | ||
public static Type? GetImplementationType(this ServiceDescriptor descriptor) | ||
{ | ||
if (descriptor is ClonedSingletonDescriptor cloned) | ||
{ | ||
// Use the parent descriptor as it was before being cloned. | ||
return cloned.Parent.GetImplementationType(); | ||
} | ||
|
||
if (descriptor.ImplementationType != null) | ||
if (descriptor.TryGetImplementationTypeInternal(out var implementationType)) | ||
{ | ||
return descriptor.ImplementationType; | ||
return implementationType; | ||
} | ||
|
||
if (descriptor.ImplementationInstance != null) | ||
if (descriptor.TryGetImplementationInstance(out var implementationInstance)) | ||
{ | ||
return descriptor.ImplementationInstance.GetType(); | ||
return implementationInstance?.GetType(); | ||
} | ||
|
||
if (descriptor.ImplementationFactory != null) | ||
if (descriptor.TryGetImplementationFactory(out var implementationFactory)) | ||
{ | ||
return descriptor.ImplementationFactory.GetType().GenericTypeArguments[1]; | ||
return implementationFactory?.GetType().GenericTypeArguments[1]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static object? GetImplementationInstance(this ServiceDescriptor serviceDescriptor) => serviceDescriptor.IsKeyedService | ||
? serviceDescriptor.KeyedImplementationInstance | ||
: serviceDescriptor.ImplementationInstance; | ||
|
||
public static object? GetImplementationFactory(this ServiceDescriptor serviceDescriptor) => serviceDescriptor.IsKeyedService | ||
? serviceDescriptor.KeyedImplementationFactory | ||
: serviceDescriptor.ImplementationFactory; | ||
|
||
public static bool TryGetImplementationType(this ServiceDescriptor serviceDescriptor, out Type? type) | ||
{ | ||
type = serviceDescriptor.GetImplementationType(); | ||
|
||
return type is not null; | ||
} | ||
|
||
public static bool TryGetImplementationInstance(this ServiceDescriptor serviceDescriptor, out object? instance) | ||
{ | ||
instance = serviceDescriptor.GetImplementationInstance(); | ||
|
||
return instance is not null; | ||
} | ||
|
||
public static bool TryGetImplementationFactory(this ServiceDescriptor serviceDescriptor, out object? factory) | ||
{ | ||
factory = serviceDescriptor.GetImplementationFactory(); | ||
|
||
return factory is not null; | ||
} | ||
|
||
internal static Type? GetImplementationTypeInternal(this ServiceDescriptor serviceDescriptor) => serviceDescriptor.IsKeyedService | ||
? serviceDescriptor.KeyedImplementationType | ||
: serviceDescriptor.ImplementationType; | ||
|
||
internal static bool TryGetImplementationTypeInternal(this ServiceDescriptor serviceDescriptor, out Type? type) | ||
{ | ||
type = serviceDescriptor.GetImplementationTypeInternal(); | ||
|
||
return type is not null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters