You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/DependencyInjection/Internal/ConstructorMetadata.cs

115 lines
4.8 KiB

6 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
5 years ago
namespace ZeroLevel.DependencyInjection
6 years ago
{
/// <summary>
/// Constructor metadata
6 years ago
/// </summary>
internal class ConstructorMetadata
{
public ConstructorInfo Constructor { get; }
private IReadOnlyList<ConstructorParameter> Parameters { get; }
private readonly IContainer _parent;
public ConstructorMetadata(Container parent, ConstructorInfo info)
{
_parent = parent;
Constructor = info;
Parameters = info.
GetParameters().
Select(p =>
{
var parameterAttribute = p.GetCustomAttribute<ParameterAttribute>();
var resolveAttribute = p.GetCustomAttribute<ResolveAttribute>();
7 months ago
var kind = (parameterAttribute != null!) ? ConstructorParameterKind.Parameter :
(resolveAttribute != null!) ? ConstructorParameterKind.Dependency : ConstructorParameterKind.None;
6 years ago
return new ConstructorParameter
{
Type = p.ParameterType,
ParameterKind = kind,
7 months ago
ParameterResolveName = ((kind == ConstructorParameterKind.Parameter) ? parameterAttribute?.Name ?? p.Name :
(kind == ConstructorParameterKind.Dependency) ? resolveAttribute?.ResolveName : null)!,
6 years ago
ParameterResolveType = (kind == ConstructorParameterKind.Parameter) ? parameterAttribute?.Type ?? p.ParameterType :
7 months ago
(kind == ConstructorParameterKind.Dependency) ? resolveAttribute?.ContractType ?? p.ParameterType : null!,
6 years ago
IsNullable = IsNullable(p.ParameterType)
};
}).ToList();
}
private static bool IsNullable(Type type)
{
if (!type.IsValueType) return true; // ref-type
7 months ago
if (Nullable.GetUnderlyingType(type) != null!) return true; // Nullable<T>
6 years ago
return false; // value-type
}
6 years ago
/// <summary>
/// Determining whether the constructor is suitable for the specified arguments
6 years ago
/// </summary>
/// <param name="args">Arguments</param>
/// <param name="parameters">Prepared arguments for constructor call</param>
/// <returns>true - if the constructor can be called with the arguments passed</returns>
6 years ago
public bool IsMatch(object[] args, out object[] parameters)
{
7 months ago
parameters = null!;
6 years ago
int arg_index = 0;
if (Parameters.Count > 0)
{
parameters = new object[Parameters.Count];
for (int i = 0; i < parameters.Length; i++)
{
switch (Parameters[i].ParameterKind)
{
case ConstructorParameterKind.Parameter:
parameters[i] = _parent.Get(Parameters[i].ParameterResolveType, Parameters[i].ParameterResolveName);
break;
case ConstructorParameterKind.Dependency:
6 years ago
parameters[i] = _parent.Resolve(Parameters[i].ParameterResolveType, Parameters[i].ParameterResolveName);
break;
6 years ago
default:
if (args == null || arg_index >= args.Length) return false;
if (null == args[arg_index])
{
if (Parameters[i].IsNullable)
{
parameters[i] = args[arg_index];
}
else
{
return false;
}
}
else if (Parameters[i].Type.IsAssignableFrom(args[arg_index].GetType()))
{
parameters[i] = args[arg_index];
}
else
{
try
{
parameters[i] = Convert.ChangeType(args[i], Parameters[i].Type);
}
catch
{
return false;
}
}
arg_index++;
break;
}
}
return true;
}
if (args != null && args.Length > 0)
return false;
return true;
}
}
}

Powered by TurnKey Linux.