c# - 如何在c#中返回生成对象的传入属性值
问题描述
我有一个返回特定类型的函数。前任。传真文件。在该对象中,有几种不同的格式可供下载,例如:PDF、LargeJpg、SmallJpg 等。
我可以将下载称为这样的pdf。
public FaxFile DownloadFaxPDFById(int faxId)
{
return DownloadFaxById(faxId).Pdf;
}
我想要做的是能够传递对象 ex 的属性。LargeJpg 格式下载。
须藤代码
public FaxFile DownloadFaxTypeById(int faxId, property)
{
return DownloadFaxById(faxId).property;
}
我该怎么做呢?
解决方案
你可以使用反射。
var resultObj = DownloadFaxById(faxId);
var result = resultObj.GetType().GetProperty("<propertyName>").GetValue(resultObj);
请注意,您需要将结果转换为适当的对象
推荐阅读
- python - Excel 文件在重新保存后仅在 Python 中使用 pandas 打开
- c# - 过滤数据导致项目不显示在android上
- swift - 如何获取保存在文件夹中的图像列表?
- python - 从 PyQT5 检查 3 个空字符串变量
- python - Pandas 使用非空值的其他列数组创建新列
- r - 是否有用于将日期和时间与 AM 或 PM 分开的 R 函数?
- css-selectors - 相当于剧作家中的 by.name 选择器
- python - LambdaContext 对象没有属性
- python - 我的 while 循环是否错误地比较了两个列表?
- twilio - 关于如何通过电话号码获取传真使用的任何想法