/// <summary> /// Register a method with the container. /// </summary> /// <param name="method">The method name.</param> /// <param name="target">The invoking target.</param> /// <param name="methodInfo">The method info to invoke.</param> /// <returns>The method binding data.</returns> public IMethodBind Bind(string method, object target, MethodInfo methodInfo) { Guard.ParameterNotNull(method, nameof(method)); Guard.ParameterNotNull(methodInfo, nameof(methodInfo)); if (!methodInfo.IsStatic) { Guard.Requires <ArgumentNullException>(target != null); } lock (syncRoot) { if (methodMappings.ContainsKey(method)) { throw new LogicException($"Method [{method}] is already {nameof(Bind)}"); } var methodBind = new MethodBind(this, container, method, target, methodInfo); methodMappings[method] = methodBind; if (target == null) { return(methodBind); } if (!targetToMethodsMappings.TryGetValue(target, out List <string> targetMappings)) { targetToMethodsMappings[target] = targetMappings = new List <string>(); } targetMappings.Add(method); return(methodBind); } }
/// <summary> /// Unbinds a method from the container. /// </summary> /// <param name="methodBind">The method binding data.</param> internal void Unbind(MethodBind methodBind) { methodMappings.Remove(methodBind.Service); if (methodBind.Target == null) { return; } if (!targetToMethodsMappings.TryGetValue(methodBind.Target, out List <string> methods)) { return; } methods.Remove(methodBind.Service); if (methods.Count <= 0) { targetToMethodsMappings.Remove(methodBind.Target); } }