Browse Source

Make changes per discussion

Instead of using fields, we will now use properties (that must have a setter).
tags/1.0-rc
Christopher F 8 years ago
parent
commit
7fb032c9d2
2 changed files with 10 additions and 10 deletions
  1. +2
    -2
      src/Discord.Net.Commands/Attributes/InjectAttribute.cs
  2. +8
    -8
      src/Discord.Net.Commands/Utilities/ReflectionUtils.cs

+ 2
- 2
src/Discord.Net.Commands/Attributes/InjectAttribute.cs View File

@@ -2,8 +2,8 @@


namespace Discord.Commands namespace Discord.Commands
{ {
[AttributeUsage(AttributeTargets.Field)]
public class InjectAttribute : Attribute
[AttributeUsage(AttributeTargets.Property)]
public sealed class InjectAttribute : Attribute
{ {
} }
} }

+ 8
- 8
src/Discord.Net.Commands/Utilities/ReflectionUtils.cs View File

@@ -42,23 +42,23 @@ namespace Discord.Commands


try 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) foreach (var field in fields)
{ {
object arg; 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; arg = service;
else if (field.FieldType == typeof(IDependencyMap))
else if (field.PropertyType == typeof(IDependencyMap))
arg = map; arg = map;
else 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) catch (Exception ex)
{ {


Loading…
Cancel
Save