1

Copied some code from one controller to another. Both files have the same using statements but it wont 'resolve' this issue for me.

On my first file address.Name.Value = source.Name.TrimSafe(); is fine however on my second file. .TrimSafe flags up as does not exist From the telescense i can choose trim, trimEnd or trimStart where has trimSafe gone to and why cant I use it?

4

3 回答 3

3

TrimSafe()不是标准的 .NET 方法。这表明它TrimSafe()作为方法存在于您的第一个文件中,但从未被复制到第二个文件中,并且它是私有函数或非静态函数。

我也会清理和重建你的项目。

于 2013-08-01T15:17:19.090 回答
0

看起来很熟悉,我猜你也在做 Orchard Webshop 教程 :) 你需要在你的项目中添加一个名为 Helpers 的文件夹,它应该有这个类:

    public static class StringExtensions {
    public static string TrimSafe(this string s) {
        return s == null ? string.Empty : s.Trim();
    }
}

只需包含需要您的方法的名称空间。

于 2014-03-16T20:21:59.190 回答
0

对我来说,这听起来像是一个命名空间问题。

考虑一下:

// In some file somewhere
namespace firstNamespace
{
    Class MyString : String
    {
        public static TrimSafe() {}
    }
}

// The first file you copied from
namespace firstNamespace
{
    public void foo() { TrimSafe(); } // Works!
}

namespace secondNamespace
{
    public void fee() { TrimSafe(); } // Nope :(
}

要修复最后一个,您需要using firstNamespace;在顶部添加其他 using 语句。原因是您复制的第一个文件与定义 TrimSafe 的名称空间相同。

在 C++ 中,您通常#include为您使用的每个类提供所有引用的 .h 文件。

在 C# 中,您不会using为类添加语句,而是为命名空间添加语句。您只需要成为using命名空间,该命名空间内的所有内容都会随之而来。命名空间中定义的任何xyz东西都可以看到命名空间中定义的所有其他东西,xyz而不必using为每个类都有一个。

您的第二个文件位于不同的命名空间中,因此它不知道第一个命名空间中的内容,因此看不到 TrimSafe。

反正这是我的猜测。

于 2013-08-01T15:31:09.540 回答