Add IDependencyMap injection for public properties

This commit is contained in:
james7132
2017-02-10 15:44:24 +00:00
parent 8be4cb72e3
commit a551064eaf

View File

@@ -19,6 +19,7 @@ namespace Discord.Commands
var constructor = constructors[0];
System.Reflection.ParameterInfo[] parameters = constructor.GetParameters();
System.Reflection.PropertyInfo[] properties = typeInfo.DeclaredProperties.Where(p => p.CanWrite).ToArray();
return (map) =>
{
@@ -40,15 +41,33 @@ namespace Discord.Commands
args[i] = arg;
}
T obj;
try
{
return (T)constructor.Invoke(args);
obj = (T)constructor.Invoke(args);
}
catch (Exception ex)
{
throw new Exception($"Failed to create \"{typeInfo.FullName}\"", ex);
}
foreach(var property in properties)
{
object prop;
if (map == null || !map.TryGet(property.PropertyType, out prop))
{
if (property.PropertyType == typeof(CommandService))
prop = service;
else if (property.PropertyType == typeof(IDependencyMap))
prop = map;
else
throw new InvalidOperationException($"Failed to create \"{typeInfo.FullName}\", dependency \"{property.PropertyType.Name}\" was not found.");
}
property.SetValue(prop, null);
}
return obj;
};
}
}
}