Resolving Generic Types with Fully Qualified Names

Oct 5, 2010 at 7:06 PM

I can't seem to get unity to resolve a generic type, using a fully qualified name for the type.  I have a generic grabber, that grabs a value.  In this case, I want to create a grabber for getting a role provider.

This is my code, and corresponding config:

The interface:

namespace DanOved.Common.Grabbers
{
    /// <summary>
    /// Grabs a value
    /// </summary>
    /// <typeparam name="TValueToGrab"></typeparam>
    public interface IGrabber<TTypeToGrab>
    {
        TTypeToGrab GrabValue();
    }
}

The implementation:

namespace DanOved.Common.Implementation.Grabbers
{
    /// <summary>
    /// Grabs role provider based on provider name, from configuration
    /// </summary>
    public class ConfigRoleProviderGrabber : IGrabber<RoleProvider>
    {
        private string _RoleProviderName;

        public ConfigRoleProviderGrabber(string roleProviderName)
        {
            this._RoleProviderName = roleProviderName;
        }

        public RoleProvider GrabValue()
        {
            return System.Web.Security.Roles.Providers[_RoleProviderName];
        }
    }
}

and my config:

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <container>
      <register type="DanOved.Common.Grabbers.IGrabber`1[[System.Web.Security.RoleProvider, System.Web.ApplicationServices]], DanOved.Common" mapTo="DanOved.Common.Implementation.Grabbers.ConfigRoleProviderGrabber, DanOved.Common">
        <constructor>
          <param name="roleProviderName" value="AspNetSqlRoleProvider"></param>
        </constructor>
      </register>
    </container>

  </unity>

When I attempt to resolve, I get the error:

The type name or alias DanOved.Common.Grabbers.IGrabber`1[[System.Web.Security.RoleProvider, System.Web.ApplicationServices]], DanOved.Common could not be resolved. Please check your configuration file and verify this type name.

 

How am I configuring the generic type wrongly?

Oct 5, 2010 at 9:22 PM

You probably need to fully specify the assembly name for System.Web.ApplicationServices. If it's in the GAC, it won't be found without the full assembly name, including the version #, culture, and public key token.

Remember in the end this all turns into a call to Type.GetType(string), the rules with Unity match up with the rules for Type.GetType.