#33: Add extension method to convert an action:

Needed to handle OnCreateActions from inheriting types later on
pull/37/head
Simon Gockner 6 years ago
parent 165cfd16f2
commit c25896a56e
  1. 26
      LightweightIocContainer/ActionExtension.cs
  2. 99
      LightweightIocContainer/LightweightIocContainer.xml
  3. 52
      Test.LightweightIocContainer/ActionExtensionTest.cs

@ -0,0 +1,26 @@
// Author: Gockner, Simon
// Created: 2019-12-11
// Copyright(c) 2019 SimonG. All Rights Reserved.
using System;
namespace LightweightIocContainer
{
internal static class ActionExtension
{
/// <summary>
/// Convert an <see cref="Action{T2}"/> to an <see cref="Action{T1}"/> of an inherited <see cref="Type"/>
/// </summary>
/// <typeparam name="T1">The <see cref="Type"/> of the <see cref="Action{T1}"/> to convert to, has to be implemented by <typeparamref name="T2"/></typeparam>
/// <typeparam name="T2">The <see cref="Type"/> of the given <see cref="Action{T2}"/>, has to implement <typeparamref name="T1"/></typeparam>
/// <param name="action">The given <see cref="Action{T2}"/> to convert</param>
/// <returns>An <see cref="Action{T1}"/> converted from the given <see cref="Action{T2}"/></returns>
public static Action<T1> Convert<T1, T2>(this Action<T2> action) where T1 : T2
{
if (action == null)
return null;
return t => action(t);
}
}
}

@ -4,6 +4,15 @@
<name>LightweightIocContainer</name> <name>LightweightIocContainer</name>
</assembly> </assembly>
<members> <members>
<member name="M:LightweightIocContainer.ActionExtension.Convert``2(System.Action{``1})">
<summary>
Convert an <see cref="T:System.Action`1"/> to an <see cref="T:System.Action`1"/> of an inherited <see cref="T:System.Type"/>
</summary>
<typeparam name="T1">The <see cref="T:System.Type"/> of the <see cref="T:System.Action`1"/> to convert to, has to be implemented by <typeparamref name="T2"/></typeparam>
<typeparam name="T2">The <see cref="T:System.Type"/> of the given <see cref="T:System.Action`1"/>, has to implement <typeparamref name="T1"/></typeparam>
<param name="action">The given <see cref="T:System.Action`1"/> to convert</param>
<returns>An <see cref="T:System.Action`1"/> converted from the given <see cref="T:System.Action`1"/></returns>
</member>
<member name="M:LightweightIocContainer.EnumerableExtension.FirstOrGiven``2(System.Collections.Generic.IEnumerable{``0})"> <member name="M:LightweightIocContainer.EnumerableExtension.FirstOrGiven``2(System.Collections.Generic.IEnumerable{``0})">
<summary> <summary>
Returns the first element of a <see cref="T:System.Collections.Generic.IEnumerable`1"/>, or a new instance of a given <see cref="T:System.Type"/> if the <see cref="T:System.Collections.Generic.IEnumerable`1"/> contains no elements Returns the first element of a <see cref="T:System.Collections.Generic.IEnumerable`1"/>, or a new instance of a given <see cref="T:System.Type"/> if the <see cref="T:System.Collections.Generic.IEnumerable`1"/> contains no elements
@ -418,6 +427,55 @@
</summary> </summary>
<param name="container">The current <see cref="T:LightweightIocContainer.Interfaces.IIocContainer"/></param> <param name="container">The current <see cref="T:LightweightIocContainer.Interfaces.IIocContainer"/></param>
</member> </member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IOnCreate`1">
<summary>
Provides an <see cref="M:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IOnCreate`1.OnCreate(System.Action{`0})"/> method to an <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/>
</summary>
<typeparam name="TInterface">The registered interface</typeparam>
</member>
<member name="P:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IOnCreate`1.OnCreateAction">
<summary>
This <see cref="T:System.Action"/> is invoked when an instance of this type is created.
<para>Can be set in the <see cref="T:LightweightIocContainer.Interfaces.Installers.IIocInstaller"/> by calling <see cref="M:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IOnCreate`1.OnCreate(System.Action{`0})"/></para>
</summary>
</member>
<member name="M:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IOnCreate`1.OnCreate(System.Action{`0})">
<summary>
Pass an <see cref="T:System.Action`1"/> that will be invoked when an instance of this type is created
</summary>
<param name="action">The <see cref="T:System.Action`1"/></param>
<returns>The current instance of this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/></returns>
</member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IWithParameters`1">
<summary>
Provides a <see cref="M:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IWithParameters`1.WithParameters(System.Object[])"/> method to an <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/>
</summary>
<typeparam name="TInterface">The registered interface</typeparam>
</member>
<member name="P:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IWithParameters`1.Parameters">
<summary>
An <see cref="T:System.Array"/> of parameters that are used to <see cref="M:LightweightIocContainer.Interfaces.IIocContainer.Resolve``1"/> an instance of this <see cref="P:LightweightIocContainer.Interfaces.Registrations.IRegistration.InterfaceType"/>
<para>Can be set in the <see cref="T:LightweightIocContainer.Interfaces.Installers.IIocInstaller"/> by calling <see cref="M:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IWithParameters`1.WithParameters(System.Object[])"/></para>
</summary>
</member>
<member name="M:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IWithParameters`1.WithParameters(System.Object[])">
<summary>
Pass parameters that will be used to<see cref="M:LightweightIocContainer.Interfaces.IIocContainer.Resolve``1"/> an instance of this <see cref="P:LightweightIocContainer.Interfaces.Registrations.IRegistration.InterfaceType"/>
<para>Parameters set with this method are always inserted at the beginning of the argument list if more parameters are given when resolving</para>
</summary>
<param name="parameters">The parameters</param>
<returns>The current instance of this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/></returns>
<exception cref="T:LightweightIocContainer.Exceptions.InvalidRegistrationException"><see cref="P:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IWithParameters`1.Parameters"/> are already set or no parameters given</exception>
</member>
<member name="M:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IWithParameters`1.WithParameters(System.ValueTuple{System.Int32,System.Object}[])">
<summary>
Pass parameters that will be used to<see cref="M:LightweightIocContainer.Interfaces.IIocContainer.Resolve``1"/> an instance of this <see cref="P:LightweightIocContainer.Interfaces.Registrations.IRegistration.InterfaceType"/>
<para>Parameters set with this method are inserted at the position in the argument list that is passed with the parameter if more parameters are given when resolving</para>
</summary>
<param name="parameters">The parameters with their position</param>
<returns>The current instance of this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/></returns>
<exception cref="T:LightweightIocContainer.Exceptions.InvalidRegistrationException"><see cref="P:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IWithParameters`1.Parameters"/> are already set or no parameters given</exception>
</member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"> <member name="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1">
<summary> <summary>
The <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/> to register a <see cref="T:System.Type"/> for the Interface it implements The <see cref="T:LightweightIocContainer.Interfaces.Registrations.IDefaultRegistration`1"/> to register a <see cref="T:System.Type"/> for the Interface it implements
@ -466,43 +524,6 @@
The Lifestyle of Instances that are created with this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> The Lifestyle of Instances that are created with this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/>
</summary> </summary>
</member> </member>
<member name="P:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1.OnCreateAction">
<summary>
This <see cref="T:System.Action`1"/> is invoked when an instance of this type is created.
<para>Can be set in the <see cref="T:LightweightIocContainer.Interfaces.Installers.IIocInstaller"/> by calling <see cref="M:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1.OnCreate(System.Action{`0})"/></para>
</summary>
</member>
<member name="M:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1.OnCreate(System.Action{`0})">
<summary>
Pass an <see cref="T:System.Action`1"/> that will be invoked when an instance of this type is created
</summary>
<param name="action">The <see cref="T:System.Action`1"/></param>
<returns>The current instance of this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/></returns>
</member>
<member name="P:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1.Parameters">
<summary>
An <see cref="T:System.Array"/> of parameters that are used to <see cref="M:LightweightIocContainer.Interfaces.IIocContainer.Resolve``1"/> an instance of this <see cref="P:LightweightIocContainer.Interfaces.Registrations.IRegistration.InterfaceType"/>
<para>Can be set in the <see cref="T:LightweightIocContainer.Interfaces.Installers.IIocInstaller"/> by calling <see cref="M:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1.WithParameters(System.Object[])"/></para>
</summary>
</member>
<member name="M:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1.WithParameters(System.Object[])">
<summary>
Pass parameters that will be used to<see cref="M:LightweightIocContainer.Interfaces.IIocContainer.Resolve``1"/> an instance of this <see cref="P:LightweightIocContainer.Interfaces.Registrations.IRegistration.InterfaceType"/>
<para>Parameters set with this method are always inserted at the beginning of the argument list if more parameters are given when resolving</para>
</summary>
<param name="parameters">The parameters</param>
<returns>The current instance of this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/></returns>
<exception cref="T:LightweightIocContainer.Exceptions.InvalidRegistrationException"><see cref="P:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1.Parameters"/> are already set or no parameters given</exception>
</member>
<member name="M:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1.WithParameters(System.ValueTuple{System.Int32,System.Object}[])">
<summary>
Pass parameters that will be used to<see cref="M:LightweightIocContainer.Interfaces.IIocContainer.Resolve``1"/> an instance of this <see cref="P:LightweightIocContainer.Interfaces.Registrations.IRegistration.InterfaceType"/>
<para>Parameters set with this method are inserted at the position in the argument list that is passed with the parameter if more parameters are given when resolving</para>
</summary>
<param name="parameters">The parameters with their position</param>
<returns>The current instance of this <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/></returns>
<exception cref="T:LightweightIocContainer.Exceptions.InvalidRegistrationException"><see cref="P:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1.Parameters"/> are already set or no parameters given</exception>
</member>
<member name="T:LightweightIocContainer.Interfaces.Registrations.ISingleTypeRegistration`1"> <member name="T:LightweightIocContainer.Interfaces.Registrations.ISingleTypeRegistration`1">
<summary> <summary>
The <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> to register either only an interface or only a <see cref="T:System.Type"/> The <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> to register either only an interface or only a <see cref="T:System.Type"/>
@ -683,12 +704,12 @@
</member> </member>
<member name="M:LightweightIocContainer.IocContainer.UpdateArgumentsWithRegistrationParameters``1(LightweightIocContainer.Interfaces.Registrations.IRegistrationBase{``0},System.Object[])"> <member name="M:LightweightIocContainer.IocContainer.UpdateArgumentsWithRegistrationParameters``1(LightweightIocContainer.Interfaces.Registrations.IRegistrationBase{``0},System.Object[])">
<summary> <summary>
Update the given arguments with the <see cref="P:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1.Parameters"/> of the given <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> Update the given arguments with the <see cref="P:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IWithParameters`1.Parameters"/> of the given <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/>
</summary> </summary>
<typeparam name="T">The given <see cref="T:System.Type"/></typeparam> <typeparam name="T">The given <see cref="T:System.Type"/></typeparam>
<param name="registration">The <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> of the given <see cref="T:System.Type"/></param> <param name="registration">The <see cref="T:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1"/> of the given <see cref="T:System.Type"/></param>
<param name="arguments">The constructor arguments</param> <param name="arguments">The constructor arguments</param>
<returns>The argument list updated with the <see cref="P:LightweightIocContainer.Interfaces.Registrations.IRegistrationBase`1.Parameters"/></returns> <returns>The argument list updated with the <see cref="P:LightweightIocContainer.Interfaces.Registrations.FluentProviders.IWithParameters`1.Parameters"/></returns>
</member> </member>
<member name="M:LightweightIocContainer.IocContainer.ResolveConstructorArguments(System.Type,System.Object[],System.Collections.Generic.List{System.Type})"> <member name="M:LightweightIocContainer.IocContainer.ResolveConstructorArguments(System.Type,System.Object[],System.Collections.Generic.List{System.Type})">
<summary> <summary>

@ -0,0 +1,52 @@
// Author: Gockner, Simon
// Created: 2019-12-11
// Copyright(c) 2019 SimonG. All Rights Reserved.
using System;
using LightweightIocContainer;
using NUnit.Framework;
namespace Test.LightweightIocContainer
{
[TestFixture]
public class ActionExtensionTest
{
#region TestClasses
private interface IBar
{
void Throw();
}
private interface IFoo : IBar
{
}
private class Foo : IFoo
{
public void Throw()
{
throw new Exception();
}
}
#endregion TestClasses
[Test]
public void TestConvert()
{
Action<IBar> barAction = bar => bar.Throw();
Action<IFoo> action = barAction.Convert<IFoo, IBar>();
Assert.Throws<Exception>(() => action(new Foo()));
}
[Test]
public void TestConvertActionNull()
{
Action<IBar> barAction = null;
Assert.Null(barAction.Convert<IFoo, IBar>());
}
}
}
Loading…
Cancel
Save