开发者

How to "EnforceConstraints" to avoid linq errors?

开发者 https://www.devze.com 2022-12-11 09:27 出处:网络
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\'.\'W

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开发者_如何学C.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


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.


That suggests you're trying to do something like:

Type type = typeof(int);
var methods = from method in type
              select method;

There's no "Select" method defined in System.Type or as an extension method - basically a Type isn't a valid data source for a LINQ query. Could you post the full example (and ideally which book it comes from)? It may well just be a typo - either in what you've copied or in the book itself.

EDIT: Now you've posted the code (which should be in a question edit, not an answer btw) I can see it's just a typo. Instead of this:

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

you should have

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

Note the "s" at the end :)

GetType() returns the type of the object (i.e. typeof(Assembly) or some subclass) whereas GetTypes() returns the collection of types within the assmebly. The latter is definitely what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消