我需要检查在"parent“对象中,是否有一个可以接受的方法在某个特定时刻调用”孩子“中的某个方法。例如,父对象(组件)包含子对象(换句话说就是组件部分),而父对象现在正在释放,因此必须禁止所有(或部分)子活动(即启动新服务线程、排队新客户端请求等)。
public class Parent
{
public bool IsMethodCallAcceptable(reference_to_method) {...}
}
public class Child
{
public int SomeMethod(int intArg, string stringArg)
{
if(!_parent.IsMethodCallAcceptable(reference_to_SomeMethod_with_actual_args))
throw new ...
...
}
private void AnotherMethod(string param = null) {...}
{
if(!_parent.IsMethodCallAcceptable(reference_to_AnotherMethod_with_actual_args))
throw new ...
...
}
private Guid ThirdMethod()
{
if(!_parent.IsMethodCallAcceptable(reference_to_ThirdMethod))
throw new ...
...
}
}有什么办法可以做到吗?
发布于 2012-07-22 02:05:52
最简单的方法是传递URI而不是引用:
例如,"NS.Child.ThirdMethod“。
否则,委托是最接近函数引用的对象。如果你愿意,你可以通过它。
然而,这种方法不符合OOP概念规则:基类应该对其子类一无所知。最好使用某种锁定机制来告诉孩子,他们不能访问资源。
https://stackoverflow.com/questions/11594630
复制相似问题