【发布时间】:2020-10-23 06:25:57
【问题描述】:
这是我在here 之前提出的问题的略微修改版本。
实际位置如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace cns01
{
class Program
{
public class DataFieldInfo2<T>
{
public bool IsSearchValue { get; set; } = false;
public T Value { get; set; }
}
public class SmartRowVertrag
{
public DataFieldInfo2<int> ID { get; set; }
public DataFieldInfo2<string> VSNR { get; set; }
}
static void Main(string[] args)
{
SmartRowVertrag tester = new SmartRowVertrag();
tester.ID = new DataFieldInfo2<int>() { Value = 777, IsSearchValue = false };
tester.VSNR = new DataFieldInfo2<string>() { Value = "234234234", IsSearchValue = true };
var g = RetData3(tester);
}
public static object RetData3(object fsr)
{
object retVal = new object();
var props = fsr.GetType().GetProperties();
foreach (var prop in props)
{
PropertyInfo propInfo = prop.GetType().GetProperty("IsSearchValue"); // <-- here I get null
propInfo = prop.PropertyType.GetProperty("IsSearchValue"); // here I get a propertyInfo, but...
dynamic property = propInfo.GetValue(fsr, null); // ...<-- here fires error
var result = property.IsSearchValue;
if (result == true)
{
// doThis
}
}
return retVal;
}
}
}
我确实需要获取存储在IsSearchValue 中的布尔值来完成任务。
先谢谢了。
我非常感谢任何有帮助的答案。
【问题讨论】:
-
"here fires error" 并不能很好地说明使用情况。请在帖子中使用堆栈跟踪指定错误。 (很高兴有一个完整的例子 - 谢谢。)
标签: c# generics casting propertyinfo