c# - Reflection get type of FieldInfo object? -
hi all, need access class someclass declared has private field in wrapper class, using reflection far have been able private field members . how cast original type access properties , other members.
internal class program { private static void main(string[] args) { wrapper wrap = new wrapper { someproperty = new someclass { number = 007 } }; type type = wrap.gettype(); fieldinfo[] infos = type.getfields(bindingflags.nonpublic | bindingflags.instance); foreach (var item in infos) { } } } internal class someclass { public int number { get; set; } } internal class wrapper { private someclass _tempsomeobj; public someclass someproperty { { return _tempsomeobj; } set { _tempsomeobj = value; } } }
i dont know if understand question correct. want type of private field (backing field)??
then check fieldtype property of fieldinfo....
like this:
internal class program { #region methods private static void main(string[] args) { var wrap = new wrapper { someproperty = new someclass { number = 007 } }; type type = wrap.gettype(); fieldinfo[] fieldinfos = type.getfields(bindingflags.nonpublic | bindingflags.instance); foreach (var fieldinfo in fieldinfos) { if (fieldinfo.fieldtype == typeof(someclass)) { console.writeline("yap!"); } } } #endregion } internal class someclass { #region properties public int number { get; set; } #endregion } internal class wrapper { #region properties public someclass someproperty { get; set; } #endregion }
Comments
Post a Comment