From 5b6429610be31e419fea8a9d7b6b699f517fcb85 Mon Sep 17 00:00:00 2001 From: "Simon G." Date: Wed, 23 Oct 2024 10:36:15 +0200 Subject: [PATCH] - upload expression test --- LightweightIocContainer/Expressions.cs | 31 ++++++++++++++++ .../Factories/TypedFactory.cs | 7 +++- .../LightweightIocContainer.csproj | 3 ++ .../ExpressionsTest.cs | 37 +++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 LightweightIocContainer/Expressions.cs create mode 100644 Test.LightweightIocContainer/ExpressionsTest.cs diff --git a/LightweightIocContainer/Expressions.cs b/LightweightIocContainer/Expressions.cs new file mode 100644 index 0000000..0528c6e --- /dev/null +++ b/LightweightIocContainer/Expressions.cs @@ -0,0 +1,31 @@ +// Author: Simon.Gockner +// Created: 2024-04-17 +// Copyright(c) 2024 SimonG. All Rights Reserved. + +using System.Linq.Expressions; +using System.Reflection; +using FastExpressionCompiler; + +namespace LightweightIocContainer; + +internal class Expressions +{ + public T Create(ConstructorInfo constructor, params object?[]? arguments) + { + (Expression? expression, IEnumerable parameters) = CreateConstructorExpression(constructor); + return ((Func) Expression.Lambda(expression, parameters).CompileFast())(); //TODO: Pass arguments to func? (don't use dynamicInvoke if possible), maybe use factory class? + } + + private (Expression expression, IEnumerable parameters) CreateConstructorExpression(ConstructorInfo constructor) + { + ParameterInfo[] parameters = constructor.GetParameters(); + if (parameters.Length == 0) + return (Expression.New(constructor), []); + + ParameterExpression[] parameterExpressions = new ParameterExpression[parameters.Length]; + for (int i = 0; i < parameters.Length; i++) + parameterExpressions[i] = Expression.Parameter(parameters[i].ParameterType); + + return (Expression.New(constructor, parameterExpressions.Cast()), parameterExpressions); + } +} \ No newline at end of file diff --git a/LightweightIocContainer/Factories/TypedFactory.cs b/LightweightIocContainer/Factories/TypedFactory.cs index 66e89ee..b9fd8b4 100644 --- a/LightweightIocContainer/Factories/TypedFactory.cs +++ b/LightweightIocContainer/Factories/TypedFactory.cs @@ -4,6 +4,7 @@ using System.Reflection; using System.Reflection.Emit; +using GProxy; using LightweightIocContainer.Exceptions; using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces.Factories; @@ -22,7 +23,11 @@ public class TypedFactory : TypedFactoryBase, ITypedFactory< /// The /// /// The current instance of the - public TypedFactory(IocContainer container) => Factory = CreateFactory(container); + public TypedFactory(IocContainer container) + { + //Factory = CreateFactory(container); + Factory = Proxy.Of(); + } /// /// The implemented abstract typed factory/> diff --git a/LightweightIocContainer/LightweightIocContainer.csproj b/LightweightIocContainer/LightweightIocContainer.csproj index 7e12565..742a3d6 100644 --- a/LightweightIocContainer/LightweightIocContainer.csproj +++ b/LightweightIocContainer/LightweightIocContainer.csproj @@ -23,6 +23,9 @@ + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Test.LightweightIocContainer/ExpressionsTest.cs b/Test.LightweightIocContainer/ExpressionsTest.cs new file mode 100644 index 0000000..868058b --- /dev/null +++ b/Test.LightweightIocContainer/ExpressionsTest.cs @@ -0,0 +1,37 @@ +// Author: Simon.Gockner +// Created: 2024-04-24 +// Copyright(c) 2024 SimonG. All Rights Reserved. + +using System.Reflection; +using LightweightIocContainer; +using NUnit.Framework; + +namespace Test.LightweightIocContainer; + +public class ExpressionsTest +{ + public class TestNoCtor; + public class TestWithCtor(string name); + + [Test] + public void TestCreateExpressionWithDefaultCtor() + { + Expressions expressions = new(); + + ConstructorInfo defaultCtor = typeof(TestNoCtor).GetConstructors().First(); + TestNoCtor testNoCtor = expressions.Create(defaultCtor); + + Assert.That(testNoCtor, Is.InstanceOf()); + } + + [Test] + public void TestCreateExpressionWithParamCtor() + { + Expressions expressions = new(); + + ConstructorInfo paramCtor = typeof(TestWithCtor).GetConstructors().First(); + TestWithCtor testWithCtor = expressions.Create(paramCtor, "TestName"); + + Assert.That(testWithCtor, Is.InstanceOf()); + } +} \ No newline at end of file