C# 反射期间“未找到属性设置方法”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9364092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:02:34  来源:igfitidea点击:

"Property set method not found" error during reflection

c#.netreflection

提问by Alain

I'm trying to reflect over some class properties and set them programaticlly, but it looks like one of my PropertyInfo filters isn't working:

我试图反映一些类属性并以编程方式设置它们,但看起来我的 PropertyInfo 过滤器之一不起作用:

//Get all public or private non-static properties declared in this class (no inherited properties) - that have a getter and setter.
PropertyInfo[] props = this.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.SetProperty );

I'm getting an error on the line

我在线时遇到错误

pi.SetValue(this, valueFromData, null);

Because the property has only a get{}method, no set{}method.

因为属性只有get{}方法,没有set{}方法。

My question is, why wasn't this property filtered out of props? I thought that was the purpose of BindingFlags.SetProperty.

我的问题是,为什么这个属性没有从 props 中过滤掉?我认为这是 BindingFlags.SetProperty 的目的。

The property not getting filtered out is:

未被过滤掉的属性是:

    public String CollTypeDescription
    {
        get { return _CollTypeDescription; }
    }

Note that I want to filter properties that won't work ahead of time, because I'm listing them all at once. I do notwant to use pi.GetSetMethod()after the fact to determine whether I can use the setter.

请注意,我想过滤无法提前使用的属性,因为我将一次性列出所有这些属性。我希望使用pi.GetSetMethod()的事实后,以确定自己是否可以使用二传手。

采纳答案by ken

From the documentation:

从文档:

BindingFlags.SetProperty

Specifies that the value of the specified property should be set. For COM properties, specifying this binding flag is equivalent to specifying PutDispProperty and PutRefDispProperty.

BindingFlags.SetProperty

指定应设置指定属性的值。对于 COM 属性,指定此绑定标志等效于指定 PutDispProperty 和 PutRefDispProperty。

BindingFlags.SetPropertyand BindingFlags.GetPropertydo notfilter properties that are missing setters or getters, respectively.

BindingFlags.SetProperty并且BindingFlags.GetProperty不要分别过滤缺少 setter 或 getter 的属性。

To check if a property can be set, use the CanWriteproperty.

要检查是否可以设置CanWrite属性,请使用该属性。

if (pi.CanWrite)
    pi.SetValue(this, valueFromData, null);

回答by brgerner

I understand the GetProperties() method so that it returns every property that has BindingFlags.GetPropertyorBindingFlags.SetProperty.
So if you want only properties that have setters you must remove the BindingFlags.GetPropertyflag. But I did not tested it so I can be wrong.

我了解 GetProperties() 方法,因此它返回每个具有BindingFlags.GetProperty或 的属性BindingFlags.SetProperty
因此,如果您只想要具有 setter 的属性,则必须删除该BindingFlags.GetProperty标志。但我没有测试过,所以我可能是错的。

My answer got a -1. So it seems that my answer is wrong.

我的回答是-1。所以看来我的回答是错误的。

回答by Alain

Thanks to ken for the information. It looks like the best solution I can get it to filter them out by testing GetSetMethod(true) in a LINQ filter:

感谢 ken 提供的信息。它看起来是我可以通过在 LINQ 过滤器中测试 GetSetMethod(true) 来过滤掉它们的最佳解决方案:

// Get all public or private non-static properties declared in this class
// (e.g. excluding inherited properties) that have a getter and setter.
PropertyInfo[] props = this.GetType()
    .GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance |
                   BindingFlags.Public | BindingFlags.NonPublic)
    .Where(p => p.GetGetMethod(true) != null && p.GetSetMethod(true) != null)
    .ToArray();

Alternatively, using CanReadand CanWrite:

或者,使用CanReadCanWrite

PropertyInfo[] props = this.GetType()
    .GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance |
                   BindingFlags.Public | BindingFlags.NonPublic)
    .Where(p => p.CanRead && p.CanWrite)
    .ToArray();

It's unclear to me whether these different approaches will yield different results for different protection levels of the get/set methods.

我不清楚这些不同的方法是否会对 get/set 方法的不同保护级别产生不同的结果。