开发者

Find a private interface field with Reflection? [duplicate]

This question already has answers here: 开发者_运维百科 Find a private field with Reflection? (11 answers) Closed last year.

Given this class

public partial class Default : Page
{
    private IRepository repo;
    ...
}

I want to find and set the private repo field. Is that possible?

UPDATE

I tried using the GetFields(BindingFlags.NonPublic), it returns {System.Reflection.FieldInfo[0]}.

UPDATE II

I tried using the GetFields(BindingFlags.NonPublic | BindingFlags.Instance) , it returns all the fields of the Page but not repo.


Use the GetFields overload that allows you to specify flags:

GetFields(BindingFlags.NonPublic | BindingFlags.Instance)


you can use the following code :

MemberInfo[] mi = System.Runtime.Serialization.FormatterServices.GetSerializableMembers(MyType);

and convert FieldInfo fi = (FieldInfo) mi[i]; this code return Serializable Members (privates)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜