我可以访问内部类中的外部类对象吗

0

本文介绍了我可以访问内部类中的外部类对象吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有三个这样的课程.

class A
{
    public class innerB
       {
       //Do something
       }

    public class innerC
       {
        //trying to access objB here directly or indirectly over here. 
        //I dont have to create an object of innerB, but to access the object created by A
        //i.e.
             innerB objInnerB = objB;
        //not like this
             innerB objInnerB= new innerB();
       }

private innerB objB{get;set;}  **//Private**

public A()
   {
    objB= new innerB();
   }
}

我想访问由 A 类创建的 C 类中 B 类的对象.是否有可能以某种方式对 C 类中 A 类的对象进行更改.我可以通过创建事件或任何方式来获取 A 类的对象吗?

I want to access the object of class B in Class C that is created by class A. Is it possible somehow to make changes on object of Class A in Class C. Can i get Class A's object by creating event or anyhow.

我提出上述问题的错误.在 A 中创建的 B 的对象是私有的而不是公共的

是否可以通过创建事件来做到这一点

IS IT POSSIBLE TO DO THIS BY CREATING EVENT

如果我能够引发一个 A 类可以处理的事件,那么我的问题就可以解决了.

If anyhow I become able to raise an event that can be handled by Class A, then my problem can be solved.

推荐答案

如果我没看错,你想在 innerC 中访问类 A 的 objB 属性而不传递它.

If I'm reading you correctly you want to access the objB property of class A within innerC WITHOUT passing it along.

这不是 C# 内部类的工作方式,如本文所述:C# 嵌套类就像 C++ 嵌套类,而不是 Java 内部类

This isn't how C# inner classes work, as described in this article: C# nested classes are like C++ nested classes, not Java inner classes

如果您想从 innerC 访问 A.objB,那么您将不得不以某种方式将 A 类传递给 innerC.

If you want to access A.objB from innerC then you are going to have to somehow pass class A to innerC.

这篇关于我可以访问内部类中的外部类对象吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14