Browse Source

Merge pull request #546 from khionu/dev

Fix detection of IDependencyMap impl
tags/1.0-rc
RogueException GitHub 8 years ago
parent
commit
334ceacdbf
2 changed files with 11 additions and 1 deletions
  1. +8
    -0
      src/Discord.Net.Commands/Dependencies/DependencyMap.cs
  2. +3
    -1
      src/Discord.Net.Commands/Utilities/ReflectionUtils.cs

+ 8
- 0
src/Discord.Net.Commands/Dependencies/DependencyMap.cs View File

@@ -38,6 +38,10 @@ namespace Discord.Commands
public void AddFactory<T>(Func<T> factory) where T : class
{
var t = typeof(T);
if (typeof(T) == typeof(IDependencyMap))
throw new InvalidOperationException("IDependencyMap is used internally and cannot be added as a dependency");
if (typeof(T) == typeof(CommandService))
throw new InvalidOperationException("CommandService is used internally and cannot be added as a dependency");
if (map.ContainsKey(t))
throw new InvalidOperationException($"The dependency map already contains \"{t.FullName}\"");
map.Add(t, factory);
@@ -48,6 +52,10 @@ namespace Discord.Commands
var t = typeof(T);
if (map.ContainsKey(t))
return false;
if (typeof(T) == typeof(IDependencyMap))
throw new InvalidOperationException("IDependencyMap is used internally and cannot be added as a dependency");
if (typeof(T) == typeof(CommandService))
throw new InvalidOperationException("CommandService is used internally and cannot be added as a dependency");
map.Add(t, factory);
return true;
}


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

@@ -51,6 +51,8 @@ namespace Discord.Commands
};
}

private static readonly TypeInfo _dependencyTypeInfo = typeof(IDependencyMap).GetTypeInfo();

internal static object GetMember(Type targetType, IDependencyMap map, CommandService service, TypeInfo baseType)
{
object arg;
@@ -58,7 +60,7 @@ namespace Discord.Commands
{
if (targetType == typeof(CommandService))
arg = service;
else if (targetType == typeof(IDependencyMap))
else if (targetType == typeof(IDependencyMap) || targetType == map.GetType())
arg = map;
else
throw new InvalidOperationException($"Failed to create \"{baseType.FullName}\", dependency \"{targetType.Name}\" was not found.");


Loading…
Cancel
Save