Cleaned up new DependencyMap system
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
@@ -13,18 +12,6 @@ namespace Discord.Commands
|
||||
map = new Dictionary<Type, object>();
|
||||
}
|
||||
|
||||
public object Get(Type t)
|
||||
{
|
||||
if (!map.ContainsKey(t))
|
||||
throw new KeyNotFoundException($"The dependency map does not contain \"{t.FullName}\"");
|
||||
return map[t];
|
||||
}
|
||||
|
||||
public T Get<T>() where T : class
|
||||
{
|
||||
return Get(typeof(T)) as T;
|
||||
}
|
||||
|
||||
public void Add<T>(T obj)
|
||||
{
|
||||
var t = typeof(T);
|
||||
@@ -32,5 +19,37 @@ namespace Discord.Commands
|
||||
throw new InvalidOperationException($"The dependency map already contains \"{t.FullName}\"");
|
||||
map.Add(t, obj);
|
||||
}
|
||||
|
||||
public T Get<T>()
|
||||
{
|
||||
return (T)Get(typeof(T));
|
||||
}
|
||||
public object Get(Type t)
|
||||
{
|
||||
object result;
|
||||
if (!TryGet(t, out result))
|
||||
throw new KeyNotFoundException($"The dependency map does not contain \"{t.FullName}\"");
|
||||
else
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool TryGet<T>(out T result)
|
||||
{
|
||||
object untypedResult;
|
||||
if (TryGet(typeof(T), out untypedResult))
|
||||
{
|
||||
result = (T)untypedResult;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = default(T);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public bool TryGet(Type t, out object result)
|
||||
{
|
||||
return map.TryGetValue(t, out result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
public interface IDependencyMap
|
||||
{
|
||||
object Get(Type t);
|
||||
T Get<T>() where T : class;
|
||||
void Add<T>(T obj);
|
||||
|
||||
T Get<T>();
|
||||
bool TryGet<T>(out T result);
|
||||
|
||||
object Get(Type t);
|
||||
bool TryGet(Type t, out object result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user