CanResolve

Feb 24, 2011 at 7:15 AM

I needed to check whether a type has been registered or not, without crashing. I haven't seen this discussed so, if anybody else wants to use the new methods, here they are:

 


    #region CanResolve Members

    public bool CanResolve<TType>() where TType : class
    {
      return CanResolve(typeof (TType));
    }

    public bool CanResolve<TType>(string name) where TType : class
    {
      return CanResolve(name, typeof (TType));
    }

    public bool CanResolve(Type type)
    {
      var entry = typeRegistry[new UnNamedRegistrationKey(type)] as Registration;
      return entry != null;
    }

    public bool CanResolve(string name, Type type)
    {
      var entry = typeRegistry[new NamedRegistrationKey(name, type)] as Registration;
      return entry != null;
    }

    #endregion
Coordinator
Mar 12, 2011 at 4:35 PM

A great idea.  I've added a similar implementation to the source.

One issue with it, however, is that this only tells you if the name/type is registered.  If it is a class, then is may be resolvable even if it isn't registered.

 

Matthew