繁体   English   中英

在部分类中引用方法时,C#编译失败

[英]C# Compile fails when referencing method in partial class

建立C#专案时,我目前收到CS1061错误。 此错误仅发生在我的代码中非常特定的位置。

错误CS1061描述: 'type' does not contain a definition for 'member' and no extension method 'name' accepting a first argument of type 'type' could be found (are you missing a using directive or an assembly reference?).

我有两个文件, main.cspartial.cs partial.cs是一个局部类的main.cs main.cs内部包含两个类,即main类和helper类。

helper类实例化main类并调用partial.cs文件中定义的main类的方法时,将引发该错误。

据我了解,这应该不是问题。 main类分为两个文件,但在编译时将部分类组合在一起。

这个问题似乎非常相似,但是他的解决方案对我不起作用。 这两个文件都包含在项目中。 单独文件中的ASP.NET C#部分类不起作用CS1061

有任何想法吗?

main.cs

namespace Price.WS
{
    public partial class Main : BaseWebService
    {
        public Main()
        {
            //CODEGEN: This call is required by the ASP.NET Web Services Designer
            InitializeComponent();
        }

        private DataSet _TestMethod()
        {
            //Stuff     
        }          
    }

    public class Helper: IExchangeable
    {
        public void Test()
        {             
            using (Main main = new Main())
            {
                main.TestMethod();  //This is where the error gets thrown. The compiler doesn't see TestMethod() as a method of main
            }
        }
    } 
}

Partial.cs:

namespace Price.WS
{
    public partial class Main : BaseWebService
    {        
        [WebMethod()]
        public DataSet TestMethod()
        {
            //Stuff
            return _TestMethod();   
            //Stuff
        }          
    }      
}

编辑:似乎它并不特定于在助手类中被调用的方法。 在构建期间,在另一个类中引发了另一个错误。 此类的设置方法与上一类相同,但没有额外的帮助程序类。 当部分类的主要部分调用另一个文件中的部分类中声明的方法时,此方法将失败。

我发现了问题。 该错误实际上是在另一个具有此类链接的项目中。 具有链接的项目不包含指向部分类的链接,因此这些方法不可用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM