FieldInfo.SetValue on nested types

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.
 
P

Pavel Minaev

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);
 
M

Mark D. (GY)

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.
 
J

Jon Skeet [C# MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top