.NET 在运行时更改类(对象)实例的所有者
.NET C# 中是否可以在运行时更改对象的所有者?
例如:
class abc {
MyClass ClassInstance = new MyClass();
AnotherClass AnotherClassInstance = new AnotherClass();
// Some how set the owner of "AnotherClassInstance" to "ClassInstance"
}
谢谢!
Is it possible in .NET C# to change the owner of an object at runtime?
For example:
class abc {
MyClass ClassInstance = new MyClass();
AnotherClass AnotherClassInstance = new AnotherClass();
// Some how set the owner of "AnotherClassInstance" to "ClassInstance"
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改实例的所有者是什么意思? .NET 对象没有所有者,因此并不清楚您想要什么。
如果您的意思是希望
AnotherClass
类始终有一个MyClass
在类逻辑中被视为其“所有者”,那么只需向添加一个构造函数即可AnotherClass
它将采用MyClass
作为参数并保留此引用。像这样:
What do you mean by change the owner of an instance? .NET objects don't have owners, so it's really not clear what you want.
If you meant you want the
AnotherClass
class to always have aMyClass
which is considered as its "owner" in the class' logic, then simply add a constructor toAnotherClass
which would take aMyClass
as a parameter and will keep this reference.Like this: