Cleaned up bugs in DependencyMap and ReflectionUtils
This commit is contained in:
@@ -8,6 +8,11 @@ namespace Discord.Commands
|
|||||||
{
|
{
|
||||||
private Dictionary<Type, object> map;
|
private Dictionary<Type, object> map;
|
||||||
|
|
||||||
|
public DependencyMap()
|
||||||
|
{
|
||||||
|
map = new Dictionary<Type, object>();
|
||||||
|
}
|
||||||
|
|
||||||
public T Get<T>() where T : class
|
public T Get<T>() where T : class
|
||||||
{
|
{
|
||||||
var t = typeof(T);
|
var t = typeof(T);
|
||||||
|
|||||||
@@ -16,23 +16,23 @@ namespace Discord.Commands
|
|||||||
if (constructor.GetParameters().Length == 0)
|
if (constructor.GetParameters().Length == 0)
|
||||||
return constructor.Invoke(null);
|
return constructor.Invoke(null);
|
||||||
else if (constructor.GetParameters().Length > 1)
|
else if (constructor.GetParameters().Length > 1)
|
||||||
throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\"");
|
throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (Found too many parameters)");
|
||||||
var parameter = constructor.GetParameters().FirstOrDefault();
|
var parameter = constructor.GetParameters().FirstOrDefault();
|
||||||
if (parameter == null)
|
if (parameter == null)
|
||||||
throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\"");
|
throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (No valid parameters)");
|
||||||
if (parameter.GetType() == typeof(CommandService))
|
if (parameter.ParameterType == typeof(CommandService))
|
||||||
return constructor.Invoke(new object[1] { commands });
|
return constructor.Invoke(new object[1] { commands });
|
||||||
else if (parameter is IDependencyMap)
|
else if (parameter.ParameterType == typeof(IDependencyMap))
|
||||||
{
|
{
|
||||||
if (map == null) throw new InvalidOperationException($"The constructor for \"{typeInfo.FullName}\" requires a Dependency Map.");
|
if (map == null) throw new InvalidOperationException($"The constructor for \"{typeInfo.FullName}\" requires a Dependency Map.");
|
||||||
return constructor.Invoke(new object[1] { map });
|
return constructor.Invoke(new object[1] { map });
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\"");
|
throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (Invalid Parameter Type: \"{parameter.ParameterType.FullName}\")");
|
||||||
}
|
}
|
||||||
catch
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\"");
|
throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (Error invoking constructor)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user