예제 #1
0
 /// <summary>
 /// Sets an instance for a binding.
 /// </summary>
 /// <param name="binding">The binding to set an instance for.</param>
 /// <param name="instance">The instance to set.</param>
 /// <param name="type">The type to set the instance for.</param>
 /// <param name="name">The name to set the instance for.</param>
 internal void SetInstance(IBinding binding, object instance, Type type, string name)
 {
     if (instance == null)
     {
         throw new InvalidOperationException($"Attempted to bind null to instance for binding of type {binding.BaseType}");
     }
     else if (name != null)
     {
         if (!InstanceCache.IsCached(type, name))
         {
             InstanceCache.Cache(instance, type, name);
         }
         else
         {
             throw new InvalidOperationException($"Binding for type {binding.BaseType} with name {binding.Name} attempts to override cached instance for {type}.");
         }
     }
     else
     {
         if (!InstanceCache.IsCached(type))
         {
             InstanceCache.Cache(instance, type);
         }
         else
         {
             throw new InvalidOperationException($"Binding for type {binding.BaseType} attempts to override cached instance for {type}.");
         }
     }
 }
예제 #2
0
 /// <summary>
 /// Checks if a binding is cached, either named or unnamed.
 /// </summary>
 /// <param name="type">The type to check if bindings exist for.</param>
 /// <param name="name">The name to check for bindings of.</param>
 /// <returns><c>true</c> if the type is cached, otherwise <c>false</c>.</returns>
 internal bool IsCached(Type type, string name)
 {
     return(name != null?                        //
            InstanceCache.IsCached(type, name) : //
                InstanceCache.IsCached(type));
 }