Can i access outer class objects in inner class(我可以访问内部类中的外部类对象吗)
问题描述
我有三个这样的课程.
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.
这篇关于我可以访问内部类中的外部类对象吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以访问内部类中的外部类对象吗
基础教程推荐
- Moq It.Is<>不匹配 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
