我有A类,其中包含B类,我想在B类中使用A类而不实例化A类

希望标题相当清楚,但是这里有更多细节:

A类包含一个ChromeDriver变量和对其进行操作的方法。我已经在代表菜单的A类中定义了B类,并且需要访问B类中A类的某些方法。

提出问题的另一种方式:如何在不创建A类另一个实例的情况下从B类(内部类)访问A类(外部类)?

我对C#还是很陌生,到目前为止,还没有找到实现此目的的方法,到目前为止,我的研究仅发现了死胡同。可能吗?如果没有,为什么不呢?

感谢您提供正确答案的答案和指示!

-更新-

很抱歉,延迟很长时间,在这种情况下,需要上述条件:

 public sealed class CatalogPane
 {

    protected CatalogPane(Application application,string automationId,int index)
    {
        Pane = application.MainWindow.FindElementByClassname("CatalogMenu")
            .Where(item => item.AutomationId().StartsWith(automationId))
            .ElementAtOrDefault(index);
    }

    public AppiumWebElement Pane { get; set; }

    public static class contextMenu
    {       

        public static void Select(MenuOption option) // MenuOption has property id that holds an automationId
        {
            Pane.FindElementByaccessibilityId(option.id).Click(); // I do not have access to Pane so this is not possible. I would like to be able to do this.
        }

    }
 }
Hiram908416047 回答:我有A类,其中包含B类,我想在B类中使用A类而不实例化A类

很抱歉-这确实应该是一条评论,但我没有足够的声誉积分来发表评论... 如果您需要在内部类中使用外部类中的方法,这听起来好像您的分工是错误的(您的对象应该执行他们需要做的事情)。但是,如果没有看到任何代码,我们将无法知道您的实际目标是什么。 也许它应该是B类的方法,如果您在A类中需要它,则使用B的实例?

,

一种好的方法是使您可以给类B引用您的类A,例如,给类B提供第二个(或在第一个中执行)构造函数,并在其中传递A的实例。类A实例化:

let html = '';
,

根据我的理解,这就是您需要的示例。

public class A 
{
    public B ClassB { get; private set; }

    public A()
    {
        ClassB = new B(this); //pass the parent class as a parameter
    }

    public class B
    {
        private A ClassA { get; set; } //With this property you can access the values of class A

        public B(A _classA)
        {
            ClassA = _classA;
        }
    }
}
本文链接:https://www.f2er.com/3116115.html

大家都在问