title: Operation is not valid due to the current state of the object
body: <p>I am a student and my job was to build my own IoC container. I've read some tutorials/code samples and finally created it but when I try to create instance of it, I get exception like in the topic. Here is my IoC code and test code, I've been trying to figure out what's wrong but just can't find it.</p>
  
  <p>IoC</p>
  
  <pre><code>public class Mkontener: UnresolvedDependenciesException, IContainer
  {
      private readonly Dictionary&lt;Type, object&gt; container = new Dictionary&lt;Type, object&gt; { };
  
      public void Register(System.Reflection.Assembly assembly)
      {
  
          Type[] mytypes = assembly.GetTypes();
  
  
          foreach (Type t in mytypes)
          {
              if (!container.ContainsKey(t))
              {
                  container.Add(t, Activator.CreateInstance(t));
              }
          }
          //throw new NotImplementedException();
      }
  
      public void Register(Type type)
      {
          if (!container.ContainsKey(type))
          {
              container.Add(type, Activator.CreateInstance(type));
          }
          //throw new NotImplementedException();
      }
  
      public void Register(object impl)
      {
          Type t = impl.GetType();
          if (!container.ContainsKey(t))
          {
              container.Add(t, impl);
          }
  
          // throw new NotImplementedException();
      }
  
      public void Register&lt;T&gt;(Func&lt;T&gt; provider) where T : class
      {
  
          container.Add(provider.GetType(), provider);
          //throw new NotImplementedException();
      }
  
      public T Resolve&lt;T&gt;() where T : class
      {
  
          return (T)Resolve(typeof(T));
      }
  
      public object Resolve(Type type)
      {
          List&lt;ConstructorInfo&gt; konstruktor = new List&lt;ConstructorInfo&gt; { };
          foreach (ConstructorInfo cons in type.GetConstructors())
          {
              konstruktor.Add(cons);
          }
          if (konstruktor.Count == 0)
          {
              return Activator.CreateInstance(type);
          }
          else
          {
              ConstructorInfo active = null;
              int lparams = 0;
              foreach (ConstructorInfo cnst in konstruktor)
              {
                  int inner=0;
                  foreach (ParameterInfo a in cnst.GetParameters())
                  {
                      inner++;
                  }
                  if (inner &gt; lparams)
                  {
                      active = cnst;
                  }
  
              }
              List&lt;object&gt; lista = new List&lt;object&gt; { };
              foreach(ParameterInfo param in active.GetParameters())
              {
                  if (container.ContainsKey(param.GetType()))
                  {
                      lista.Add(container[param.GetType()]);
                  }
                  else
                  {
                      //throw new UnresolvedDependenciesException();
                  }
              }
  
              object[] obiekty= lista.ToArray();
              return  Activator.CreateInstance(type, obiekty);
          }
          if (container.ContainsKey(type))
          {
              return container[type];
          }
          else
          {
              //throw new UnresolvedDependenciesException();
          }
  
      }
  
  
      public void Register&lt;T&gt;(T impl) where T : class
      {
          container.Add(impl.GetType(), impl);
      }
  }
  </code></pre>
  
  <p>Part of test unit</p>
  
  <pre><code>public void P1__Container_Should_Register_Singleton_As_Object()
  {
      // Arrange
      var container = (IContainer)Activator.CreateInstance(LabDescriptor.Container);
      var singleton = new FakeImpl();
  
      // Act
      container.Register(singleton);
      var result = container.Resolve&lt;IFake&gt;();
  
      // Assert
      Assert.That(result, Is.Not.Null);
      Assert.That(result, Is.SameAs(singleton));
  }
  </code></pre>
  
  <p>Sorry for clumsy code etc. Just starting here.</p>