c# - 处理嵌套属性值
问题描述
访问嵌套属性值的优雅解决方案是什么?
示例:
在某些情况下,它可能如下所示:
public void someFunction()
{
this.Device.ResponseHandler.Process(this.Device.TcpClient.responseMessage, this.Device.TcpClient.responseType)
}
我的解决方案是复制对象,然后缩短名称。
public void someFuntion()
{
// Just for shorten the access name afterwards
ResponseHandler responseHandler = this.Device.RepsonseHandler;
TcpClient tcpClient = this.Device.TcpClient;
responseHandler.Process(tcpClient.responseMessage, tcpClient.responseType);
}
解决方案
它主要基于意见,但通常有两种方式:
您正在使用的那个。
在新行中列出每个参数:
public void someFunction() { this.Device.ResponseHandler.Process( this.Device.TcpClient.responseMessage, this.Device.TcpClient.responseType ); }
IMO 都具有同样的可读性,在第二种方法中,您不需要其他变量:)
推荐阅读
- amazon-web-services - 登录堡垒主机后无法 ping 和 ssh 登录到 ec2 实例
- reactjs - 如何通过服务器端的值在 useEffect 中设置状态
- flutter - Flutter:从 appbar 调用 listview builder 中的方法
- android - Android 11 (API30) 以编程方式设置“所有文件”权限
- java - 输入日期大于给定日期值
- python - 将 df 保存为 {index - list of all values} dict 格式
- git - Git恢复撤消的更改?
- matlab - Cayley-Hamilton 定理的系数
- python - NameError: name 'open' is not defined 尝试登录文件时
- python - 错误:在字符串格式化期间并非所有参数都转换了