diff --git a/src/Discord.Net.Commands/Attributes/InjectAttribute.cs b/src/Discord.Net.Commands/Attributes/InjectAttribute.cs new file mode 100644 index 000000000..1ad1d4ee3 --- /dev/null +++ b/src/Discord.Net.Commands/Attributes/InjectAttribute.cs @@ -0,0 +1,9 @@ +using System; + +namespace Discord.Commands +{ + [AttributeUsage(AttributeTargets.Field)] + public class InjectAttribute : Attribute + { + } +} diff --git a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs index 27ea601bf..b7320ba5a 100644 --- a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs +++ b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs @@ -42,7 +42,23 @@ namespace Discord.Commands try { - return (T)constructor.Invoke(args); + T type = (T)constructor.Invoke(args); + var fields = type.GetType().GetRuntimeFields().Where(p => p.GetCustomAttribute() != null); + foreach (var field in fields) + { + object arg; + if (map == null || !map.TryGet(field.FieldType, out arg)) + { + if (field.FieldType == typeof(CommandService)) + arg = service; + else if (field.FieldType == typeof(IDependencyMap)) + arg = map; + else + throw new InvalidOperationException($"Failed to inject \"{typeInfo.FullName}\", dependency \"{field.FieldType.FullName}\" was not found."); + } + field.SetValue(type, arg); + } + return type; } catch (Exception ex) {