views:

46

answers:

0

I use this code to create my controller:

    public override IController CreateController(RequestContext requestContext, string controllerName) {
        try {
            Type controllerType = null;
            var baseControllerType = typeof(DashboardController<>);

            var item = _repository.GetByUrlSegment(requestContext.RouteData.Values["pagePath"] as string);
            var genericType = item.GetType();
            if (genericType != null) {
                controllerType = baseControllerType.MakeGenericType(genericType);
            }
            return ObjectFactory.GetInstance(controllerType) as IController;
        } catch (StructureMapException) {
            return base.CreateController(requestContext, controllerName);
        }
    }

... this works perfect but I want my basecontrollertype to load from the controllername and my controllers can be located in other assemblies.

I have tried to load the type with the code below but I always get null from dll.GetType even though I use the full.namespace.classname.

    public static Type GetTypeFromName(string typeNameStr, Assembly[] asms) {
        Type varType = null;
        string typeStr = typeNameStr.Split(',')[0];
        foreach (Assembly dll in asms) {
            varType = dll.GetType(typeNameStr) ?? dll.GetType(typeStr);
            if (varType != null)
                break;
        }
        return varType;
    }

So how can I load my generic type from the controllername?