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.
这篇关于我可以访问内部类中的外部类对象吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以访问内部类中的外部类对象吗
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
