Make changes per discussion

Instead of using fields, we will now use properties (that must have a setter).
This commit is contained in:
Christopher F
2016-12-14 17:07:24 -05:00
parent b33df6ad77
commit 7fb032c9d2
2 changed files with 10 additions and 10 deletions

View File

@@ -2,8 +2,8 @@
namespace Discord.Commands
{
[AttributeUsage(AttributeTargets.Field)]
public class InjectAttribute : Attribute
[AttributeUsage(AttributeTargets.Property)]
public sealed class InjectAttribute : Attribute
{
}
}

View File

@@ -42,23 +42,23 @@ namespace Discord.Commands
try
{
T type = (T)constructor.Invoke(args);
var fields = type.GetType().GetRuntimeFields().Where(p => p.GetCustomAttribute<InjectAttribute>() != null);
T instance = (T)constructor.Invoke(args);
var fields = instance.GetType().GetRuntimeProperties().Where(p => p.GetCustomAttribute<InjectAttribute>() != 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)
{