C# – How to “EnforceConstraints” to avoid linq errors

clinqsystem.typevisual-studio-2008

I'm working on a sample from the book I bought. And, for unknown reason, I get the following error message " Could not find an implementation of the query pattern for source type 'System.Type'. 'Where' not found."

The VS2008 help says that I need to add System.Linq and System.Collections namespaces to solve the issue. Unfortunatelly, I still get the same error message. In MSDN forum, it said that I need to set EnforceConstraints to true;

I would like to know what's "EnforceConstraints" and how can I do it.

Thanks.


Here is the code

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mvc;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Core.Resource;
using System.Reflection;
using Castle.Core;
using System.Collections;

namespace WebUI
{
public class WindsorControllerFactory : DefaultControllerFactory
{
WindsorContainer Container;

    public WindsorControllerFactory()
    {
        //Instatiate a container, taking configuration from web.conf
        Container = new WindsorContainer(
            new XmlInterpreter(new ConfigResource("Castle"))
            );

        //Also register all the controller types as transient
        var controllerTypes =
            from t in Assembly.GetExecutingAssembly().GetType()
            where typeof(IController).IsAssignableFrom(t)
            select t;
        foreach (Type t in controllerTypes)
            Container.AddComponentWithLifestyle(t.FullName, t, LifestyleType.Transient);
        }

        //Constructs the controller instance needed to service each request
    protected override IController GetControllerInstance(Type controllerType)
    {
        return (IController)Container.Resolve(controllerType);
    }    

    }//The constructor
}

The sample is on page 98.

the book is "Pro ASP.NET MVC Framework"/Steven Sanderson/APress ISBN-13 (pbk): 978-1-4302-1007-8

Best Answer

In the line:

from t in Assembly.GetExecutingAssembly().GetType()

you are missing a 's' at the end of GetTypes(). This should solve the problem, as GetType() is returning a single Type instance, whereas GetTypes() returns an array of Type objects.