Add Dependency Map, Update Assembly Crawler
[Untested] Assembly Crawler will now accept constructors matching: new(), new(CommandService), new(IDependencyMap). Add IDependencyMap Add DependencyMap
This commit is contained in:
27
src/Discord.Net.Commands/Dependencies/DependencyMap.cs
Normal file
27
src/Discord.Net.Commands/Dependencies/DependencyMap.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
public class DependencyMap : IDependencyMap
|
||||
{
|
||||
private Dictionary<Type, object> map;
|
||||
|
||||
public T Get<T>() where T : class
|
||||
{
|
||||
var t = typeof(T);
|
||||
if (!map.ContainsKey(t))
|
||||
throw new KeyNotFoundException($"The dependency map does not contain \"{t.FullName}\"");
|
||||
return map[t] as T;
|
||||
}
|
||||
|
||||
public void Add<T>(T obj)
|
||||
{
|
||||
var t = typeof(T);
|
||||
if (map.ContainsKey(t))
|
||||
throw new InvalidOperationException($"The dependency map already contains \"{t.FullName}\"");
|
||||
map.Add(t, obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/Discord.Net.Commands/Dependencies/IDependencyMap.cs
Normal file
13
src/Discord.Net.Commands/Dependencies/IDependencyMap.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
public interface IDependencyMap
|
||||
{
|
||||
T Get<T>() where T : class;
|
||||
void Add<T>(T obj);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user