开发者

Find a private interface field with Reflection? [duplicate]

开发者 https://www.devze.com 2022-12-10 22:23 出处:网络
This question already has answers here: 开发者_运维百科Find a private field with Reflection? (11 answers)
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

精彩评论

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

关注公众号