基类扩展的 Visual Studio 2010 编译器错误 [英] Visual Studio 2010 compiler error for base class extensions

查看:31
本文介绍了基类扩展的 Visual Studio 2010 编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 c# 类,提供一些简单的类和一些基类扩展,例如这个..

I have a c# class providing some simple classes and some base class extensions such as this one..

public static Boolean ToBooleanOrDefault(this String s, Boolean Default)
{
    return ToBooleanOrDefault((Object)s, Default);
}


public static Boolean ToBooleanOrDefault(this Object o, Boolean Default)
{
    Boolean ReturnVal = Default;
    try
    {
        if (o != null)
        {
            switch (o.ToString().ToLower())
            {
                case "yes":
                case "true":
                case "ok":
                case "y":
                    ReturnVal = true;
                    break;
                case "no":
                case "false":
                case "n":
                    ReturnVal = false;
                    break;
                default:
                    ReturnVal = Boolean.Parse(o.ToString());
                    break;
            }
        }
    }
    catch
    {
    }
    return ReturnVal;
}

该类编译良好,似乎没有问题.然后我在一个 web 项目中引用了该项目,VS2010 智能感知识别基类扩展,F12/got to definition 按预期跳转到原始源代码.但是,当我编译 Web 项目时,每次使用基类扩展都会出错...

The class compiles fine and appears to have no issues. I have then referenced the project in a web project and VS2010 intellisense recognises the base class extensions and F12/got to definition jumps to the original source code as expected. However when I compile the web project I get an error for each usage of the base class extension...

Error   28  'string' does not contain a definition for 'ToBooleanOrDefault'

在我看来,编译器没有使用该引用,因此它忽略了我的所有基类扩展.想法?该解决方案是从 VS2008 迁移而来的,一切正常.

This looks to me like the reference is not used by the compiler so it ignores all my base class extensions. Ideas? The solution was migrated from VS2008 where all worked fine.

推荐答案

除了常见错误(忘记包含扩展类的命名空间)之外,您应该确保两个项目使用相同的 .NET 版本.有时,v4 项目不会正确引用为框架的早期版本构建的程序集

Apart from for common mistakes (forgetting to include the extension class's namespace), you should make sure that both projects use the same .NET version. Sometimes a v4 project will not reference properly assemblies built for earlier versions of the framework

无时无刻不在

这篇关于基类扩展的 Visual Studio 2010 编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆