开发者

How do I detect the "new" modifer on a field using reflection?

http://msdn.microsoft.com/en-us/library/435f1dw2.aspx

public c开发者_高级运维lass Base
{
    public string Field;
}
public class Child : Base
{
    public new string Field;
}

[TestFixture]
public class TestClass
{

    [Test]
    public void DetectNew()
    {
        var fieldInfo = typeof(Child).GetField("Field");
        //How do I tell fieldInfo has a new modifier?
        Debug.WriteLine(fieldInfo);
    }
}

Edit: I know for Methods and Properties I can check "MethodBase.IsHideBySig". Why doesnt a similar property exist for FieldInfo?


Just a guess, but I think you'd have to search the base-class(es) for a member with the same name.

You could also take a look at the generated IL for both fields to see if there're any differences that you can use through reflection.


The only effect of new here is to suppress a compiler warning.

My guess would be that it leaves no trace in the generated IL.

Counter question: Why are you interested? Just curious or do you have a practical scenario?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜