C# 如何访问与分部类不同文件中的静态类

C# 如何访问与分部类不同文件中的静态类,c#,.net,class,static,C#,.net,Class,Static,我在Form1.cs中有部分课程Form1。 我在Sample.cs文件中有managediHelper静态类 我想从Form1类中的方法访问该静态类中的方法。但是当我使用这个时,显示在当前上下文中并不存在。 在这两个文件中,名称空间是相同的。我在Sample.cs文件中有另一个名为TcpRow的类。这是一个普通的公共类,可以通过Form1的方法访问它,并且没有错误 对此,建议的解决方案是什么 编辑 对不起 TcpRow a; foreach(managediHelper.GetExtended

我在Form1.cs中有部分课程Form1。 我在Sample.cs文件中有managediHelper静态类

我想从Form1类中的方法访问该静态类中的方法。但是当我使用这个时,显示在当前上下文中并不存在。 在这两个文件中,名称空间是相同的。我在Sample.cs文件中有另一个名为
TcpRow
的类。这是一个普通的
公共类
,可以通过Form1的方法访问它,并且没有错误

对此,建议的解决方案是什么

编辑

对不起

TcpRow a;
foreach(managediHelper.GetExtendedTcpTable中的TcpRow TcpRow(true))

对于此代码,第一行有错误
TcpRow找不到(您是否缺少using指令…
第二行只有一个错误:
managediHelper
在当前上下文中不存在

编辑2

sample.cs

表格1.cs


是静态类
private
protected
中会导致方法不可见的方法。

静态类的命名空间可能与Form1.Cs的命名空间不同,如果它们不同,则应确保在Form1.Cs类的顶部添加using语句

 using NetProject;
此外,要调用静态方法,您应该这样调用它

 ManagedHelper.MethodName(....)
您的sample.cs文件应如下所示。此代码将编译,但建议您将类划分为不同的文件

Form1.cs

namespace NetProject
{
    using System;
    using System.Collections.Generic;

    public partial class Form1
    {
        public void SomeMethod()
        {
            TcpRow row;

            foreach (TcpRow tcpRow in ManagedIpHelper.GetExtendedTcpTable(true))
            {

            }
        }
    }
}
namespace NetProject
{
    using System;
    using System.Collections.Generic;

    public class TcpTable : IEnumerable<TcpRow>
    {
        public IEnumerator<TcpRow> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            // You should implement this method
            throw new NotImplementedException();
        }
    }

    public class TcpRow
    {

    }

    public static class ManagedIpHelper
    {
        public static TcpTable GetExtendedTcpTable(bool value)
        {
            // You should implement this method
            return new TcpTable();
        }
    }
}
Sample.cs

namespace NetProject
{
    using System;
    using System.Collections.Generic;

    public partial class Form1
    {
        public void SomeMethod()
        {
            TcpRow row;

            foreach (TcpRow tcpRow in ManagedIpHelper.GetExtendedTcpTable(true))
            {

            }
        }
    }
}
namespace NetProject
{
    using System;
    using System.Collections.Generic;

    public class TcpTable : IEnumerable<TcpRow>
    {
        public IEnumerator<TcpRow> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            // You should implement this method
            throw new NotImplementedException();
        }
    }

    public class TcpRow
    {

    }

    public static class ManagedIpHelper
    {
        public static TcpTable GetExtendedTcpTable(bool value)
        {
            // You should implement this method
            return new TcpTable();
        }
    }
}
名称空间NetProject
{
使用制度;
使用System.Collections.Generic;
公共类TcpTable:IEnumerable
{
公共IEnumerator GetEnumerator()
{
抛出新的NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
//您应该实现这个方法
抛出新的NotImplementedException();
}
}
公共级TcpRow
{
}
公共静态类管理器帮助器
{
公共静态TcpTable GetExtendedTcpTable(bool值)
{
//您应该实现这个方法
返回新的TcpTable();
}
}
}

查看位于的名称空间文档。

您能给我们看一点代码吗?(例如类及其方法;其中方法的内容已被缩短/删除)您可以发布TcpRow、ManagedIpHelper和Form1的名称空间声明吗?@soham.m17快速健全性检查:您已经以当前形式保存并重新编译了它,对吗?是的。对于缩进和其他查看问题,非常抱歉。@soham.m17是否还有其他错误,或者只有这两个?方法在静态类中是公共的。我已经更改了名称空间它们都有相同的名称空间。我还通过ManagedIpHelper.Methodname()调用该方法尽管如此,它还是显示了错误。您还需要添加TcpRow的命名空间。TcpRow和managediHelper位于同一个文件sample.cs中。并且该文件与form1.cs具有相同的命名空间。@soham.m17要么是另一个类中的类,要么只是直接位于命名空间声明中?为什么我要再次在sample.cs中导入我的form1?它已经在sample.cs中了Form1.cs.sample.cs和Form1.cs只有一个名称空间NetProject。那么,您能解释一下吗?