【发布时间】:2018-07-25 07:29:41
【问题描述】:
我知道这有几个规范的答案(我过去曾成功使用过!)请参阅 https://stackoverflow.com/a/1778410/1675729 和 https://stackoverflow.com/a/1565766/1675729。作为参考,这些方法包括在属性的PropertyInfo 上使用SetValue 方法,或者通过反射调用setter 方法,或者在极端情况下,直接设置支持字段。但是,这些方法现在似乎都不适用于我,我很好奇是否发生了某些改变而破坏了此功能。
考虑以下几点:
using System;
using System.Reflection;
public class Program
{
public static void Main()
{
var exampleInstance = new Example();
var exampleType = typeof(Example);
var fooProperty = exampleType.GetProperty("Foo"); // this works fine - the "GetSetMethod" method returns null, however
// fails with the following:
// [System.MethodAccessException: Attempt by method 'Program.Main()' to access method 'Example.set_Foo(Int32)' failed.]
//fooProperty.SetValue(exampleInstance, 24);
// same error here:
//Run-time exception (line 14): Attempt by method 'Program.Main()' to access method 'Example.set_Foo(Int32)' failed.
//exampleType.InvokeMember(fooProperty.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, exampleInstance, new object[] { 24 });
}
}
public class Example
{
public int Foo { get; private set; }
}
Foo 属性有一个 setter,它只是私有的。如果我将 setter 更改为 protected,它也会失败,这看起来更奇怪,因为它不再是 C#6 初始化器专用的 setter。使用 .NET 4.5 和 .NET Core 2 作为运行时,这将失败。我错过了什么?
【问题讨论】:
-
私人还是公共?您的示例显示了公共属性,而您的问题询问的是私有属性。
-
属性是公共的,setter是私有的。我将澄清我的问题标题。
-
我有点困惑,上面的代码对我来说很好用。
Example类是否在不同的程序集中? -
我也很困惑,因此提出了这个问题。试试看:dotnetfiddle.net/o4JtEq
-
@NWard 对我来说,在线编译器没有完全信任运行似乎很合乎逻辑。就这个链接而言,这似乎是预期的行为docs.microsoft.com/en-us/dotnet/framework/…
标签: c# reflection c#-7.2