处理反射中的空值

问题描述 投票:0回答:1

如何处理非静态方法的空值,该方法返回带有计数的属性值,即当我们具有propertyName并且没有为此属性设置任何值时>]

public object Property(propertyName)
{
    return car.GetType().GetProperty(propertyName).GetValue(car, null);
}

我尝试过的不同方法:

第一种方法:

public object Property(propertyName)
{
    return (car.GetType().GetProperty(propertyName).GetValue(car, null)).Value;
}

这对我不起作用。

第二种方法:

public object Property(propertyName)
{
    var value = car.GetType().GetProperty(propertyName).GetValue(car, null);
    if (value != null)
        return  value;
    else
        return value;
}

如何实现?以上方法均不适用于我。

如何处理非静态方法的空值,该方法返回带有计数的属性值,即当我们有propertyName并且没有为此属性设置值时,公共对象Property(...

c# system.reflection
1个回答
0
投票

不确定您要问什么。但是此代码有效。看到这个帮助吗?

© www.soinside.com 2019 - 2024. All rights reserved.