473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(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
3 2610
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Glen | last post by:
FieldInfo.SetValue() doesn't appear to work when trying to set the values of fields of a struct. Why? Because structs are passed by value and not reference? -- Thanks, Glen
6
by: ORC | last post by:
I will use FieldInfo to import values from a XML file into the fields of an objects. But how to convert to the proper type like in this: public void Load(object MyClass) { foreach( FieldInfo...
3
by: Laurie | last post by:
Hi, I need to be able to manipulate field values within a structure using FieldInfo.GetValue and FieldInfo.SetValue, in VB.Nt 2003. The GetValue is working fine and makes it really easy for me...
2
by: Brent | last post by:
I have variables in a structure loaded into a list box. I thought I could use FieldInfo.SetValue to update the items value when the user clicks on it, but it is not working. .. .. .. Dim fi...
5
by: Dennis | last post by:
I have an array "t" where each element is a structure of type "testing". I want to use the FieldInfo class to set the values of the array element fields. I can read the field properties and...
1
by: jw56578 | last post by:
Im trying to use FieldInfo.SetValue to set the values of fields in a class, the class is using reflection on itself to popluate its own fields. but i keep getting this error. Object type cannot...
1
by: David Gouge | last post by:
Posting on behalf of a colleague (no, really! ;) )... Hi, I was wondering if there is any equivalent of FieldInfo.SetValue(Object, Object) that is strongly typed or uses generics? I have been...
0
by: kernell | last post by:
Hi everyone, i'm trying to initialize all control of a UserControl to is appropriate class. The value is set but the event won't raise anymore. See the example: 'Not working 'Normally...
1
by: damiensawyer | last post by:
Hi, I need to invoke .getvalue and .setvalue on fieldinfo and propetyinfo objects. Even though they're both derived from MemberInfo, they don't share those two methods. I've finding myself...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.