【问题标题】:How to "EnforceConstraints" to avoid linq errors?如何“强制约束”以避免 linq 错误?
【发布时间】:2010-12-15 20:04:52
【问题描述】:

我正在制作我购买的书中的样本。而且,由于未知原因,我收到以下错误消息“找不到源类型'System.Type'的查询模式的实现。'Where' not found.

VS2008 帮助说我需要添加 System.Linq 和 System.Collections 命名空间来解决问题。不幸的是,我仍然收到相同的错误消息。在 MSDN 论坛中,它说我需要将 EnforceConstraints 设置为 true;

我想知道什么是“EnforceConstraints”以及我该怎么做。

谢谢。


这里是代码

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

命名空间WebUI { 公共类 WindsorControllerFactory : DefaultControllerFactory { WindsorContainer 容器;

    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
}

示例在第 98 页。

这本书是“Pro ASP.NET MVC Framework”/Steven Sanderson/APress ISBN-13 (pbk): 978-1-4302-1007-8

【问题讨论】:

  • 你能发布代码示例吗?

标签: c# linq visual-studio-2008 system.type


【解决方案1】:

行内:

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

GetTypes() 末尾缺少一个“s”。这应该可以解决问题,因为 GetType() 返回单个 Type 实例,而 GetTypes() 返回 Type 对象的数组。

【讨论】:

    【解决方案2】:

    这表明您正在尝试执行以下操作:

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

    System.Type 中没有定义“选择”方法或作为扩展方法定义 - 基本上,Type 不是 LINQ 查询的有效数据源。你能发布完整的例子(最好是它来自哪本书)?这很可能只是一个错字 - 无论是在您复制的内容中还是在书本身中。

    编辑:现在您已经发布了代码(应该是问题编辑,而不是答案)我可以看到这只是一个错字。而不是这个:

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

    你应该有

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

    注意末尾的“s”:)

    GetType() 返回对象的类型(即typeof(Assembly) 或某个子类),而GetTypes() 返回集合中的类型集合。后者绝对是你想要的。

    【讨论】:

    • 非常感谢(感谢一切,包括如何提出和编辑问题)
    猜你喜欢
    • 1970-01-01
    • 2020-12-04
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    相关资源
    最近更新 更多