首页 > 解决方案 > 如何在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;
}

我该怎么做呢?

标签: c#

解决方案


你可以使用反射。

var resultObj = DownloadFaxById(faxId);
var result = resultObj.GetType().GetProperty("<propertyName>").GetValue(resultObj);

请注意,您需要将结果转换为适当的对象


推荐阅读