By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,566 Members | 1,719 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,566 IT Pros & Developers. It's quick & easy.

FieldInfo.SetValue on nested types

P: n/a
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.

Jul 11 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jul 11, 8:15*pm, Mark D. (GY) <Mark...@discussions.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(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);
Jul 11 '08 #2

P: n/a
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.
Jul 11 '08 #3

P: n/a
On Jul 11, 5:15 pm, Mark D. (GY) <Mark...@discussions.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.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
Jul 12 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.