FieldInfo.SetValue on nested types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWFyayBELiAoR1kp?=

    FieldInfo.SetValue on nested types

    I have a class which loads it own values at startup from a database. I've
    abridged the following a little, but given the following:

    public class AppSettings
    {
    int notNestedType;

    public class _nestedClass
    {
    int nestedType;
    }
    _nestedClass nested = new _nestedClass();


    protected void Load()
    {
    MemberInfo[] MemberList = me.GetMembers(B indingFlags.Non Public |
    BindingFlags.Pu blic | BindingFlags.In stance);
    foreach (MemberInfo mi in MemberList)
    {
    if (mi.MemberType == MemberTypes.Fie ld)
    {
    fld.SetValue(th is, someValue);
    }
    else if (me.MemberType == MemberTypes.Nes tedType)
    {
    // enumerate the nested types so that we find _nestedType
    // assuming fld = (MemberTypeInfo )_nestedType
    // How do we do fld.SetValue(wh atInstance, someValue);
    }

    }
    }
    }


    Setting values on this.notNestedT ype is not a problem.

    The problem is setting values on this.instance.n estedType as I have an
    instance of this but don't know how to find the instance of nestedType.


    I'd be very grateful for any pointers as I've spent hours searching google
    and trying to come up with a solution.

    Thanks,
    Mark.

  • Pavel Minaev

    #2
    Re: FieldInfo.SetVa lue on nested types

    On Jul 11, 8:15 pm, Mark D. (GY) <Mark...@discus sions.microsoft .com>
    wrote:
    I have a class which loads it own values at startup from a database. I've
    abridged the following a little, but given the following:
    >
    public class AppSettings
    {
         int notNestedType;
    >
         public class _nestedClass
         {
             int nestedType;
         }
         _nestedClass nested = new _nestedClass();
    >
        protected void Load()
        {
                MemberInfo[] MemberList = me.GetMembers(B indingFlags.Non Public |
    BindingFlags.Pu blic | BindingFlags.In stance);
                foreach (MemberInfo mi in MemberList)
                {
                    if (mi.MemberType == MemberTypes.Fie ld)
                    {
                       fld.SetValue(th is, someValue);
                    }
                    else if (me.MemberType == MemberTypes..Ne stedType)
                    {
                       // enumerate the nested types so that we find _nestedType
                       // assuming fld = (MemberTypeInfo )_nestedType
                       // How do we do fld.SetValue(wh atInstance, someValue);
    fld.SetValue(th is.nested, someValue);

    Comment

    • =?Utf-8?B?TWFyayBELiAoR1kp?=

      #3
      Re: FieldInfo.SetVa lue on nested types

      fld.SetValue(th is.nested, someValue);
      >

      No good. All I have if fld.name, I don't know the instance name as I'm
      itterating through them. I'm also writing this in a base class and want it
      to be used for future derived classes; I won't know all the member names.

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: FieldInfo.SetVa lue on nested types

        On Jul 11, 5:15 pm, Mark D. (GY) <Mark...@discus sions.microsoft .com>
        wrote:
        I have a class which loads it own values at startup from a database. I've
        abridged the following a little, but given the following:
        <snip>
        The problem is setting values on this.instance.n estedType as I have an
        instance of this but don't know how to find the instance of nestedType.
        Why would you believe there is (or has to be) and instance of the
        nested type? Nested types aren't like Java inner classes - there's no
        "implicit reference to an outer class" or anything like that.

        Effectively the nested type is a completely separate type. There are
        differences around access to privates in the outer class, but nothing
        that should be relevant here, I think.

        Jon

        Comment

        Working...