比方说,我有定义了一些类如下:如何*轻松*揭露底层对象的方法?
class Security
{
Boolean AuthenticateUser(String username, String password);
Boolean AddUser(String username, String password);
// many more methods
}
class NetworkedDevice
{
void Stop();
void Start();
// many more methods
}
然后我有一个包含上述类的实例的类。我怎样才能避免像下面这样的代码?我想通过这个类暴露class1和class2的所有方法。
class MyWindowsService
{
Security _security = new Security();
NetworkDevice _netDevice = new NetworkDevice();
Boolean AuthenticateUser(String username, String password)
{
return _security.AuthenticateUser(username, password);
}
// all the rest of "Security" methods implemented here
void StopNetworkDevice()
{
_netDevice.Stop();
}
void StartNetorkDevice()
{
_netDevice.Start();
}
// all the rest of "NetDevice" methods implemented here
}
编辑 我已经更新的代码更真正到我在做什么。我正在Windows服务中托管WCF服务。 Windows服务做了几件事情,包括用户认证和与联网设备的通信等等。我的WCF接口的实现调用了“MyWindowsService”类的方法。将底层对象公开为属性是我正在寻找的答案。上面的类看起来像这样:
class MyWindowsService
{
SecurityClass _security = new SecurityClass();
NetworkDevice _netDevice = new NetworkDevice();
Public NetworkDevice NetDevice
{
get { return _netDevice; }
}
Public SecurityClass Security
{
get { return _security; }
}
}
如果您公开所有方法,为什么不公开该成员? – FishBasketGordo
你能告诉我们更多关于你试图解决的问题吗?可能有更好的方法... –