-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IConfigurationHelper interface to dynamically choose the method…
… to call given arguments.
- Loading branch information
1 parent
3b804aa
commit f30f420
Showing
8 changed files
with
343 additions
and
151 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/ConfigurationProcessor.Core/IConfigurationProcessor.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) almostchristian. All rights reserved. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace ConfigurationProcessor | ||
{ | ||
/// <summary> | ||
/// Represents a configuration helper for dynamically executing methods. | ||
/// </summary> | ||
public interface IConfigurationProcessor | ||
{ | ||
/// <summary> | ||
/// Invokes a specificed configuration method with specificed arguments. Method is chosen based on the argument types. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="instance"></param> | ||
/// <param name="methodName"></param> | ||
/// <param name="arguments"></param> | ||
void Invoke<T>(T instance, string methodName, params object[] arguments) | ||
where T : class; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/ConfigurationProcessor.Core/Implementation/ConfigurationHelperImplementation.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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) almostchristian. All rights reserved. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace ConfigurationProcessor.Core.Implementation | ||
{ | ||
internal class ConfigurationHelperImplementation : IConfigurationProcessor | ||
{ | ||
private readonly ResolutionContext resolutionContext; | ||
private readonly IConfigurationSection configurationSection; | ||
private readonly ConfigurationReaderOptions? options; | ||
|
||
public ConfigurationHelperImplementation(ResolutionContext resolutionContext, IConfigurationSection configurationSection, ConfigurationReaderOptions? options) | ||
{ | ||
this.resolutionContext = resolutionContext; | ||
this.configurationSection = configurationSection; | ||
this.options = options; | ||
} | ||
|
||
public void Invoke<T>(T instance, string methodName, params object[] arguments) | ||
where T : class | ||
{ | ||
resolutionContext.CallConfigurationMethod( | ||
typeof(T), | ||
methodName, | ||
configurationSection, | ||
null, | ||
Array.Empty<TypeResolver>(), | ||
null!, | ||
() => arguments.ToList(), | ||
(arguments, methodInfo) => | ||
{ | ||
if (methodInfo.IsStatic) | ||
{ | ||
arguments.Insert(0, instance); | ||
methodInfo.Invoke(null, arguments.ToArray()); | ||
} | ||
else | ||
{ | ||
methodInfo.Invoke(instance, arguments.ToArray()); | ||
} | ||
}); | ||
} | ||
} | ||
} |
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
356 changes: 208 additions & 148 deletions
356
src/ConfigurationProcessor.Core/Implementation/Extensions.cs
Large diffs are not rendered by default.
Oops, something went wrong.
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