【发布时间】:2013-06-25 19:51:06
【问题描述】:
以下是我的代码:
class Foo
{
public string Bar { get; private set; }
}
和
var prop = typeof(Foo).GetProperty("Bar");
if (prop != null)
{
// The property exists
var setter = prop.GetSetMethod(true);
if (setter != null)
{
// There's a setter
Console.WriteLine(setter.IsPublic);
}
}
是的,您可以想象,这完全正确。但是当继承时,情况就不同了:
class Foo
{
public string Bar { get; private set; }
}
class A : Foo
{
}
当然我改变了这一行:
var prop = typeof(Foo).GetProperty("Bar");
到
var prop = typeof(A).GetProperty("Bar");
然后,setter 变为 null,控制台不打印任何内容!
所以...为什么?
顺便说一句,是否有一些解决方法可以实现这一点,或者完全是另一种方式?
任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: c# inheritance reflection properties