From 7fb032c9d2230d02ad3a88b0020f8e5cbe0a39af Mon Sep 17 00:00:00 2001 From: Christopher F Date: Wed, 14 Dec 2016 17:07:24 -0500 Subject: [PATCH] Make changes per discussion Instead of using fields, we will now use properties (that must have a setter). --- .../Attributes/InjectAttribute.cs | 4 ++-- .../Utilities/ReflectionUtils.cs | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Discord.Net.Commands/Attributes/InjectAttribute.cs b/src/Discord.Net.Commands/Attributes/InjectAttribute.cs index 1ad1d4ee3..836a4c668 100644 --- a/src/Discord.Net.Commands/Attributes/InjectAttribute.cs +++ b/src/Discord.Net.Commands/Attributes/InjectAttribute.cs @@ -2,8 +2,8 @@ namespace Discord.Commands { - [AttributeUsage(AttributeTargets.Field)] - public class InjectAttribute : Attribute + [AttributeUsage(AttributeTargets.Property)] + public sealed class InjectAttribute : Attribute { } } diff --git a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs index b7320ba5a..0bcb24882 100644 --- a/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs +++ b/src/Discord.Net.Commands/Utilities/ReflectionUtils.cs @@ -42,23 +42,23 @@ namespace Discord.Commands try { - T type = (T)constructor.Invoke(args); - var fields = type.GetType().GetRuntimeFields().Where(p => p.GetCustomAttribute() != null); + T instance = (T)constructor.Invoke(args); + var fields = instance.GetType().GetRuntimeProperties().Where(p => p.GetCustomAttribute() != null).Where(p => p.CanWrite); foreach (var field in fields) { object arg; - if (map == null || !map.TryGet(field.FieldType, out arg)) + if (map == null || !map.TryGet(field.PropertyType, out arg)) { - if (field.FieldType == typeof(CommandService)) + if (field.PropertyType == typeof(CommandService)) arg = service; - else if (field.FieldType == typeof(IDependencyMap)) + else if (field.PropertyType == typeof(IDependencyMap)) arg = map; else - throw new InvalidOperationException($"Failed to inject \"{typeInfo.FullName}\", dependency \"{field.FieldType.FullName}\" was not found."); + throw new InvalidOperationException($"Failed to inject \"{typeInfo.FullName}\", dependency \"{field.PropertyType.FullName}\" was not found."); } - field.SetValue(type, arg); + field.SetValue(instance, arg); } - return type; + return instance; } catch (Exception ex) {