FieldInfo.SetValue on nested types

  • Thread starter Thread starter Mark D. (GY)
  • Start date Start date
M

Mark D. (GY)

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(BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance);
foreach (MemberInfo mi in MemberList)
{
if (mi.MemberType == MemberTypes.Field)
{
fld.SetValue(this, someValue);
}
else if (me.MemberType == MemberTypes.NestedType)
{
// enumerate the nested types so that we find _nestedType
// assuming fld = (MemberTypeInfo)_nestedType
// How do we do fld.SetValue(whatInstance, someValue);
}

}
}
}


Setting values on this.notNestedType is not a problem.

The problem is setting values on this.instance.nestedType 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.
 
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(BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance);
            foreach (MemberInfo mi in MemberList)
            {
                if (mi.MemberType == MemberTypes.Field)
                {
                   fld.SetValue(this, someValue);
                }
                else if (me.MemberType == MemberTypes..NestedType)
                {
                   // enumerate the nested types so that we find _nestedType
                   // assuming fld = (MemberTypeInfo)_nestedType
                   // How do we do fld.SetValue(whatInstance, someValue);

fld.SetValue(this.nested, someValue);
 
fld.SetValue(this.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.
 
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:

The problem is setting values on this.instance.nestedType 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
 
Back
Top