diff --git a/Syrette/ServiceContainer.cs b/Syrette/ServiceContainer.cs index 06a58e9..181930a 100644 --- a/Syrette/ServiceContainer.cs +++ b/Syrette/ServiceContainer.cs @@ -24,7 +24,7 @@ public class ServiceContainer { /// public List GetServices() where TService : class => descriptors.Where(d => d.ServiceType == typeof(TService)) - .Select(d => (TService)GetService(d.ImplementationType)).ToList(); + .Select(d => (TService)GetService(d.ImplementationType, d.Arguments?.ToArray())).ToList(); /// /// Registers a singleton service with its implementation. @@ -158,19 +158,21 @@ public class ServiceContainer { // so we use reflection to call the generic GetService method with the provided type // Basically we build the method GetService() at runtime and then call it. // "Classic black magic sorcery" in reflection. - private object GetService(Type serviceType) { + private object GetService(Type serviceType, object[]? args = null) { + List arguments = [serviceType]; + + if (args != null) arguments.AddRange(args.ToList().Select(a => a.GetType())); + var method = typeof(ServiceContainer) .GetMethod(nameof(GetService))! - .MakeGenericMethod(serviceType); - return method.Invoke(this, null)!; + .MakeGenericMethod(arguments.ToArray()); + + return method.Invoke(this, args)!; } - private object? TryGetService(Type serviceType) { - var method = typeof(ServiceContainer) - .GetMethod(nameof(GetService))! - .MakeGenericMethod(serviceType); + private object? TryGetService(Type serviceType, object[]? args = null) { try { - return method.Invoke(this, null)!; + return GetService(serviceType, args); } catch { return null!; } @@ -230,7 +232,7 @@ public class ServiceContainer { throw new Exception($"Multiple constructors found for type {descriptor.ImplementationType}. Please provide a specific constructor."); List par; - List args = descriptor.Arguments ?? new List(); + List args = descriptor.Arguments != null ? new List(descriptor.Arguments) : new List(); if (ctor == null) par = descriptor.ImplementationType diff --git a/Syrette/ServiceDescriptor.cs b/Syrette/ServiceDescriptor.cs index 4217a9f..bb13483 100644 --- a/Syrette/ServiceDescriptor.cs +++ b/Syrette/ServiceDescriptor.cs @@ -8,20 +8,24 @@ public class ServiceDescriptor /// /// Gets or sets the type of the service to be provided. /// - public required Type ServiceType { get; set; } + public required Type ServiceType { get; init; } /// /// Gets or sets the concrete type that implements the service. /// - public required Type ImplementationType { get; set; } + public required Type ImplementationType { get; init; } /// /// Gets or sets the lifetime of the service (e.g., Singleton or Transient). /// - public required ServiceLifetime Lifetime { get; set; } + public required ServiceLifetime Lifetime { get; init; } /// /// Arguments to be passed to the constructor of the implementation type. /// - public List? Arguments { get; set; } + public List? Arguments { get; init; } + + public override string ToString() { + return $"{ImplementationType.Name} as {ServiceType.Name} ({Lifetime})"; + } } \ No newline at end of file diff --git a/Syrette/Syrette.csproj b/Syrette/Syrette.csproj index 7a62022..91f27d8 100644 --- a/Syrette/Syrette.csproj +++ b/Syrette/Syrette.csproj @@ -9,7 +9,7 @@ latest enable enable - 0.0.1.6-alpha + 0.0.1.7-alpha Syrette Lorefice Samuele Syrette is a minimalistic dependency injection library for C#. It aims to provide a simple and efficient way to achieve dependency injections in your applications without the overhead of larger frameworks.