mirror of
https://github.com/Stone-Red-Code/DynamicInjector.git
synced 2026-09-04 00:56:04 +02:00
Add project files.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>DynamicInjector_Test</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DynamicInjector\DynamicInjector.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace DynamicInjector_Test;
|
||||
|
||||
internal class FirstTestService : IFirstTestService
|
||||
{
|
||||
private int count = 0;
|
||||
|
||||
public void Print()
|
||||
{
|
||||
Console.WriteLine(nameof(FirstTestService) + " | " + count++);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace DynamicInjector_Test;
|
||||
|
||||
internal interface IFirstTestService
|
||||
{
|
||||
public void Print();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using DynamicInjector;
|
||||
|
||||
namespace DynamicInjector_Test;
|
||||
|
||||
internal class Prgramm
|
||||
{
|
||||
private static void Main()
|
||||
{
|
||||
InjectionContainer injector = new InjectionContainer();
|
||||
injector.RegisterGlobal<IFirstTestService, FirstTestService>();
|
||||
injector.RegisterLocal<SecondTestService>();
|
||||
|
||||
TestClass? testClass = injector.Resolve<TestClass>();
|
||||
|
||||
testClass.Test();
|
||||
|
||||
injector.Invoke<TestClass>(testClass, (c) => c.Test);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace DynamicInjector_Test;
|
||||
|
||||
internal class SecondTestService
|
||||
{
|
||||
private readonly IFirstTestService firstTestService;
|
||||
private int count = 0;
|
||||
|
||||
public SecondTestService(IFirstTestService firstTestService)
|
||||
{
|
||||
this.firstTestService = firstTestService;
|
||||
}
|
||||
|
||||
public void Output()
|
||||
{
|
||||
firstTestService.Print();
|
||||
Console.WriteLine(nameof(SecondTestService) + " | " + count++);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using DynamicInjector;
|
||||
|
||||
namespace DynamicInjector_Test;
|
||||
|
||||
internal class TestClass
|
||||
{
|
||||
private readonly IFirstTestService testService;
|
||||
|
||||
[Inject]
|
||||
private SecondTestService? SecondTestService { get; set; }
|
||||
|
||||
public TestClass(IFirstTestService yes)
|
||||
{
|
||||
testService = yes;
|
||||
}
|
||||
|
||||
public void Test()
|
||||
{
|
||||
testService.Print();
|
||||
SecondTestService?.Output();
|
||||
}
|
||||
|
||||
public int SecondTest(SecondTestService secondTestService)
|
||||
{
|
||||
secondTestService.Output();
|
||||
return 100;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.0.31912.275
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DynamicInjector", "DynamicInjector\DynamicInjector.csproj", "{B5C9B128-72FD-4278-AC8F-24B948F00679}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DynamicInjector-Test", "DynamicInjector-Test\DynamicInjector-Test.csproj", "{E5415270-33A1-4BB7-8BC5-04E036461F36}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{B5C9B128-72FD-4278-AC8F-24B948F00679}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B5C9B128-72FD-4278-AC8F-24B948F00679}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B5C9B128-72FD-4278-AC8F-24B948F00679}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B5C9B128-72FD-4278-AC8F-24B948F00679}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E5415270-33A1-4BB7-8BC5-04E036461F36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E5415270-33A1-4BB7-8BC5-04E036461F36}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E5415270-33A1-4BB7-8BC5-04E036461F36}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E5415270-33A1-4BB7-8BC5-04E036461F36}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {EBB4F955-9160-48BC-B0EE-B9156D360682}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace DynamicInjector
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
|
||||
public class InjectAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DynamicInjector
|
||||
{
|
||||
public class InjectionContainer
|
||||
{
|
||||
private readonly Dictionary<string, object> globalServices = new Dictionary<string, object>();
|
||||
private readonly Dictionary<string, Type> localServices = new Dictionary<string, Type>();
|
||||
|
||||
public void RegisterGlobal<TService>(string? name = null)
|
||||
{
|
||||
globalServices.Add(name ?? typeof(TService).FullName, Resolve<TService>()!);
|
||||
}
|
||||
|
||||
public void RegisterGlobal<TService, TImplementation>(string? name = null) where TImplementation : TService
|
||||
{
|
||||
globalServices.Add(name ?? typeof(TService).FullName, Resolve<TImplementation>()!);
|
||||
}
|
||||
|
||||
public void RegisterGlobal<TService, TImplementation>(TImplementation implementation, string? name = null) where TImplementation : TService
|
||||
{
|
||||
if (implementation is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(implementation));
|
||||
}
|
||||
|
||||
globalServices.Add(name ?? typeof(TService).FullName, implementation);
|
||||
}
|
||||
|
||||
public void RegisterLocal<TService>(string? name = null)
|
||||
{
|
||||
localServices.Add(name ?? typeof(TService).FullName, typeof(TService));
|
||||
}
|
||||
|
||||
public void RegisterLocal<TService, TImplementation>(string? name = null) where TImplementation : TService
|
||||
{
|
||||
localServices.Add(name ?? typeof(TService).FullName, typeof(TService));
|
||||
}
|
||||
|
||||
public T Resolve<T>()
|
||||
{
|
||||
return (T)Resolve(typeof(T));
|
||||
}
|
||||
|
||||
public object Resolve(Type type)
|
||||
{
|
||||
ConstructorInfo[]? constructorInfos = type.GetConstructors();
|
||||
|
||||
if (constructorInfos.Length == 0)
|
||||
{
|
||||
return Activator.CreateInstance(type);
|
||||
}
|
||||
|
||||
if (constructorInfos.Length > 1)
|
||||
{
|
||||
throw new InvalidOperationException($"Service \"{type.FullName}\" contains multiple constructors!");
|
||||
}
|
||||
|
||||
ConstructorInfo constructorInfo = constructorInfos[0];
|
||||
ParameterInfo[] parameterInfos = constructorInfo.GetParameters();
|
||||
PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
|
||||
if (parameterInfos.Length == 0)
|
||||
{
|
||||
return Activator.CreateInstance(type);
|
||||
}
|
||||
|
||||
List<object> arguments = new List<object>();
|
||||
|
||||
foreach (ParameterInfo parameterInfo in parameterInfos)
|
||||
{
|
||||
if (CheckIfServiceExists(parameterInfo.Name, out object? serviceInstance))
|
||||
{
|
||||
arguments.Add(serviceInstance!);
|
||||
}
|
||||
else if (CheckIfServiceExists(parameterInfo.ParameterType.FullName, out serviceInstance))
|
||||
{
|
||||
arguments.Add(serviceInstance!);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException($"Service \"{parameterInfo.ParameterType.FullName}\" not found!");
|
||||
}
|
||||
}
|
||||
|
||||
object instance = Activator.CreateInstance(type, arguments.ToArray());
|
||||
|
||||
foreach (PropertyInfo propertyInfo in propertyInfos)
|
||||
{
|
||||
if (propertyInfo.GetCustomAttribute<InjectAttribute>() == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (CheckIfServiceExists(propertyInfo.Name, out object? serviceInstance))
|
||||
{
|
||||
propertyInfo.SetValue(instance, serviceInstance);
|
||||
}
|
||||
else if (CheckIfServiceExists(propertyInfo.PropertyType.FullName, out serviceInstance))
|
||||
{
|
||||
propertyInfo.SetValue(instance, serviceInstance);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException($"Service \"{propertyInfo.PropertyType.FullName}\" not found!");
|
||||
}
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public object Invoke<TInstance>(TInstance instance, Func<TInstance, Delegate> method)
|
||||
{
|
||||
Delegate delegateMethod = method.Invoke(instance);
|
||||
MethodInfo methodInfo = delegateMethod.Method;
|
||||
ParameterInfo[] parameterInfos = methodInfo.GetParameters();
|
||||
|
||||
if (parameterInfos.Length == 0)
|
||||
{
|
||||
return delegateMethod.DynamicInvoke();
|
||||
}
|
||||
|
||||
List<object> arguments = new List<object>();
|
||||
|
||||
foreach (ParameterInfo parameterInfo in parameterInfos)
|
||||
{
|
||||
if (CheckIfServiceExists(parameterInfo.Name, out object? serviceInstance))
|
||||
{
|
||||
arguments.Add(serviceInstance!);
|
||||
}
|
||||
else if (CheckIfServiceExists(parameterInfo.ParameterType.FullName, out serviceInstance))
|
||||
{
|
||||
arguments.Add(serviceInstance!);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException($"Service \"{parameterInfo.ParameterType.FullName}\" not found!");
|
||||
}
|
||||
}
|
||||
|
||||
return delegateMethod.DynamicInvoke(arguments.ToArray());
|
||||
}
|
||||
|
||||
private bool CheckIfServiceExists(string typeName, out object? serviceInstance)
|
||||
{
|
||||
foreach (KeyValuePair<string, object> serviceType in globalServices)
|
||||
{
|
||||
if (typeName == serviceType.Key)
|
||||
{
|
||||
serviceInstance = globalServices[serviceType.Key];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, Type> serviceType in localServices)
|
||||
{
|
||||
if (typeName == serviceType.Key)
|
||||
{
|
||||
serviceInstance = Resolve(localServices[serviceType.Key]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
serviceInstance = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
|
||||
namespace DynamicInjector
|
||||
{
|
||||
public static class MethodInjectionContainerExtensions
|
||||
{
|
||||
public static void Invoke<TService>(this InjectionContainer injectionContainer, Func<TService, Delegate> method)
|
||||
{
|
||||
injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TInstance, TReturn>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TInstance, TReturn, T1>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<T1, TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TInstance, TReturn, T1, T2>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<T1, T2, TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TInstance, TReturn, T1, T2, T3>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<T1, T2, T3, TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TInstance, TReturn, T1, T2, T3, T4>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<T1, T2, T3, T4, TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TInstance, TReturn, T1, T2, T3, T4, T5>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<T1, T2, T3, T4, T5, TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TInstance, TReturn, T1, T2, T3, T4, T5, T6>(this InjectionContainer injectionContainer, TInstance instance, Func<TInstance, Func<T1, T2, T3, T4, T5, T6, TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(instance, method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TService, TReturn>(this InjectionContainer injectionContainer, Func<TService, Func<TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TService, TReturn, T1>(this InjectionContainer injectionContainer, Func<TService, Func<T1, TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TService, TReturn, T1, T2>(this InjectionContainer injectionContainer, Func<TService, Func<T1, T2, TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TService, TReturn, T1, T2, T3>(this InjectionContainer injectionContainer, Func<TService, Func<T1, T2, T3, TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TService, TReturn, T1, T2, T3, T4>(this InjectionContainer injectionContainer, Func<TService, Func<T1, T2, T3, T4, TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TService, TReturn, T1, T2, T3, T4, T5>(this InjectionContainer injectionContainer, Func<TService, Func<T1, T2, T3, T4, T5, TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
||||
}
|
||||
|
||||
public static TReturn Invoke<TService, TReturn, T1, T2, T3, T4, T5, T6>(this InjectionContainer injectionContainer, Func<TService, Func<T1, T2, T3, T4, T5, T6, TReturn>> method)
|
||||
{
|
||||
return (TReturn)injectionContainer.Invoke(injectionContainer.Resolve<TService>(), method);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user