【问题标题】:GetProperty works in .NET 3.5 but not in .NET 4GetProperty 在 .NET 3.5 中有效,但在 .NET 4 中无效
【发布时间】:2011-04-04 04:35:57
【问题描述】:

我使用以下代码:

protected object GetProperty(object target, string fieldName)
{
    Type type = target.GetType();
    PropertyInfo mi = type.GetProperty(
        fieldName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty);
    object obj = mi.GetValue(target, null);
    return obj;
}

它适用于 .NET 3.5。但如果我更改为 .NET 4,则 mi 变为 null。为什么?

【问题讨论】:

    标签: c# .net .net-3.5 .net-4.0 version-compatibility


    【解决方案1】:

    目标在 .net 4 中是否仍然具有所需的属性?有不少 API 更改。

    【讨论】:

    • 你是对的。该属性已在 .net 4 中删除。不好
    • @Magol:这就是依赖私人和/或无证事物的风险——它们可以而且确实会改变。
    猜你喜欢
    • 2012-09-05
    • 2014-07-10
    • 2012-11-17
    • 2010-10-11
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多