List Info

Thread: C-Sharp (C#) Group: Reflection Help




C-Sharp (C#) Group: Reflection Help
country flaguser name
United States
2007-09-10 13:11:58
I have a slight problem.  I am trying to get object
properties
dynamically.  This just screams reflection, the problem I'm
having is
getting the right property.  The properties I need from the
object are
in another object within the current object.  Specifically,
I have an
object ScheduleCountView, within it is CourseSection,
Course, and
Code.

I try to get the CourseSection.Course.Code property with
type.GetProperty("CourseSection.Course.Code"),
where type equals
ScheduleCountView, and for soem reason it always returns
null.  Does
anybody have any suggestions on how to get the information I
need from
the object?  Thanks in advance.


Jimmy


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://g
roups.google.com/group/C_Sharp?hl=en
-~----------~----~----~----~------~----~------~--~---


C-Sharp (C#) Group: Re: Reflection Help
user name
2007-09-10 13:19:37
What you need to do is not call the property on the property, if that makes any sense.  You should call GetProperty for "CourseSection" and then work with that object to get the value of Course.Code.

On 9/10/07, jimmysjams < jimmysjamsgmail.com">jimmysjamsgmail.com> wrote:

I have a slight problem.&nbsp; I am trying to get object properties
dynamically. &nbsp;This just screams reflection, the problem I'm having is
getting the right property.&nbsp; The properties I need from the object are
in another object within the current object.&nbsp; Specifically, I have an
object ScheduleCountView, within it is CourseSection, Course, and
Code.

I try to get the CourseSection.Course.Code property with
type.GetProperty("CourseSection.Course.Code&quot;), where type equals
ScheduleCountView, and for soem reason it always returns null. ; Does
anybody have any suggestions on how to get the information I need from
the object?&nbsp; Thanks in advance.


Jimmy




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/C_Sharp?hl=en
-~----------~----~----~----~------~----~------~--~---

C-Sharp (C#) Group: Re: Reflection Help
country flaguser name
United States
2007-09-10 14:12:15
All I want from the object is the value in
CourseSection.Course.Code.
I've set up a loop to go through the types listed in a
string array
until it gets to Code and when I try to get the value I no
longer have
the correct object to pass in as an parameter.  It should
also be
noted that I need this to be as dynamic as possible, this
program
won't always be working with a CourseSection object.


On Sep 10, 1:19 pm, "Marty Thompson"
<marty.thomp...gmail.com> wrote:
> What you need to do is not call the property on the
property, if that makes
> any sense.  You should call GetProperty for
"CourseSection" and then work
> with that object to get the value of Course.Code.
>
> On 9/10/07, jimmysjams <jimmysj...gmail.com> wrote:
>
>
>
>
>
> > I have a slight problem.  I am trying to get
object properties
> > dynamically.  This just screams reflection, the
problem I'm having is
> > getting the right property.  The properties I need
from the object are
> > in another object within the current object. 
Specifically, I have an
> > object ScheduleCountView, within it is
CourseSection, Course, and
> > Code.
>
> > I try to get the CourseSection.Course.Code
property with
> >
type.GetProperty("CourseSection.Course.Code"),
where type equals
> > ScheduleCountView, and for soem reason it always
returns null.  Does
> > anybody have any suggestions on how to get the
information I need from
> > the object?  Thanks in advance.
>
> > Jimmy- Hide quoted text -
>
> - Show quoted text -


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://g
roups.google.com/group/C_Sharp?hl=en
-~----------~----~----~----~------~----~------~--~---


C-Sharp (C#) Group: Re: Reflection Help
user name
2007-09-10 14:27:34
Well, my original point still stands, you will not be able to access the properties of a property without going through the process of discovery all over again.&nbsp; You cannot access "CourseSection.Course.Code&quot; directly.&nbsp; You'll have to call "CourseSection&quot;, then call "Course", then once you're working with that Course property, then you can directly access the "Code" property.

On 9/10/07, jimmysjams < jimmysjamsgmail.com">jimmysjamsgmail.com> wrote:

All I want from the object is the value in CourseSection.Course.Code.
I';ve set up a loop to go through the types listed in a string array
until it gets to Code and when I try to get the value I no longer have
the correct object to pass in as an parameter.  ;It should also be
noted that I need this to be as dynamic as possible, this program
won't always be working with a CourseSection object.


On Sep 10, 1:19 pm, "Marty Thompson&quot; < marty.thomp...gmail.com">marty.thomp...gmail.com> wrote:
>; What you need to do is not call the property on the property, if that makes
&gt; any sense.&nbsp; You should call GetProperty for "CourseSection&quot; and then work
> with that object to get the value of Course.Code.
>
>; On 9/10/07, jimmysjams < jimmysj...gmail.com"> jimmysj...gmail.com> wrote:
>;
>
>
>
>
> > I have a slight problem.&nbsp; I am trying to get object properties
> > dynamically. &nbsp;This just screams reflection, the problem I'm having is
> > getting the right property.&nbsp; The properties I need from the object are
> > in another object within the current object.&nbsp; Specifically, I have an
> > object ScheduleCountView, within it is CourseSection, Course, and
> > Code.
>
> > I try to get the CourseSection.Course.Code property with
> > type.GetProperty(&quot;CourseSection.Course.Code"), where type equals
>; > ScheduleCountView, and for soem reason it always returns null. ; Does
> > anybody have any suggestions on how to get the information I need from
> > the object?&nbsp; Thanks in advance.
&gt;
> > Jimmy- Hide quoted text -
>
>; - Show quoted text -

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/C_Sharp?hl=en
-~----------~----~----~----~------~----~------~--~---

C-Sharp (C#) Group: Re: Reflection Help
country flaguser name
United States
2007-09-10 15:01:27
I must not be doing it right then.

string[] strTypes = path.Split('.');
Type type = item.GetType();
for (int i = 0; i < strTypes.Length - 1; i++)
{
   PropertyInfo prop = type.GetProperty(strTypes[i]);
   type = prop.PropertyType;
}

PropertyInfo targetProperty =
type.GetProperty(strTypes[strTypes.Length - 1]);
string value = targetProperty(item, null);

The last line gives me an error saying "Object does not
match target
type."  More specifically, with what you've suggested,
how do I
retrieve the object from the type?



On Sep 10, 2:27 pm, "Marty Thompson"
<marty.thomp...gmail.com> wrote:
> Well, my original point still stands, you will not be
able to access the
> properties of a property without going through the
process of discovery all
> over again.  You cannot access
"CourseSection.Course.Code" directly.  You'll
> have to call "CourseSection", then call
"Course", then once you're working
> with that Course property, then you can directly access
the "Code" property.
>
> On 9/10/07, jimmysjams <jimmysj...gmail.com> wrote:
>
>
>
>
>
> > All I want from the object is the value in
CourseSection.Course.Code.
> > I've set up a loop to go through the types listed
in a string array
> > until it gets to Code and when I try to get the
value I no longer have
> > the correct object to pass in as an parameter.  It
should also be
> > noted that I need this to be as dynamic as
possible, this program
> > won't always be working with a CourseSection
object.
>
> > On Sep 10, 1:19 pm, "Marty Thompson"
<marty.thomp...gmail.com> wrote:
> > > What you need to do is not call the property
on the property, if that
> > makes
> > > any sense.  You should call GetProperty for
"CourseSection" and then
> > work
> > > with that object to get the value of
Course.Code.
>
> > > On 9/10/07, jimmysjams <jimmysj...gmail.com> wrote:
>
> > > > I have a slight problem.  I am trying to
get object properties
> > > > dynamically.  This just screams
reflection, the problem I'm having is
> > > > getting the right property.  The
properties I need from the object are
> > > > in another object within the current
object.  Specifically, I have an
> > > > object ScheduleCountView, within it is
CourseSection, Course, and
> > > > Code.
>
> > > > I try to get the
CourseSection.Course.Code property with
> > > >
type.GetProperty("CourseSection.Course.Code"),
where type equals
> > > > ScheduleCountView, and for soem reason
it always returns null.  Does
> > > > anybody have any suggestions on how to
get the information I need from
> > > > the object?  Thanks in advance.
>
> > > > Jimmy- Hide quoted text -
>
> > > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://g
roups.google.com/group/C_Sharp?hl=en
-~----------~----~----~----~------~----~------~--~---


C-Sharp (C#) Group: Re: Reflection Help
user name
2007-09-10 16:02:26
I'm assuming the last line of your snippet is actually targetProperty.GetValue(item, null), is that correct?
 
Based on that assumption, the issue is that you're trying to retrieve a property on a different object type. ; The targetProperty object is a reference to a property on the last type in the your strTypes array. For your desired outcome, you need to access the property in question on the item variable first.
 
Here's what you can do in a procedural way, I'll leave it up to you how you determine how to dynamically determine what values to retrieve.
 
PropertyInfo courseSectionProp = item.GetType().GetProperty(";CourseSection");
object courseSection = courseSectionProp.GetValue(item, null);
 
PropertyInfo courseProp = courseSection.GetType().GetProperty("Course");
object course = courseProp.GetValue(courseSection, null);
 
PropertyInfo codeProp = course.GetType().GetProperty(&quot;Code&quot;);
object code = codeProp.GetValue(course, null);
 
string retval = (string)code; // This is the value you're wanting
 
Assuming that "item" is an object that has a property called "CourseSection&quot;, this code should work.
&nbsp;
Like I said, I don't know how you're deciding what value to dynamically retrieve, so you'll need to use the above snippet and modify it to achieve your needs.
&nbsp;
On 9/10/07, jimmysjams < jimmysjamsgmail.com">jimmysjamsgmail.com> wrote:

I must not be doing it right then.

string[] strTypes = path.Split('.';);
Type type = item.GetType();
for (int i = 0; i < strTypes.Length - 1; i++)
{
&nbsp; PropertyInfo prop = type.GetProperty(strTypes[i]);
&nbsp; type = prop.PropertyType;
}

PropertyInfo targetProperty =
type.GetProperty (strTypes[strTypes.Length - 1]);
string value = targetProperty(item, null);

The last line gives me an error saying "Object does not match target
type."&nbsp; More specifically, with what you've suggested, how do I
retrieve the object from the type?



On Sep 10, 2:27 pm, "Marty Thompson&quot; < marty.thomp...gmail.com">marty.thomp...gmail.com> wrote:
>; Well, my original point still stands, you will not be able to access the
> properties of a property without going through the process of discovery all
> over again.&nbsp; You cannot access "CourseSection.Course.Code&quot; directly.&nbsp; You'll
> have to call "CourseSection&quot;, then call "Course", then once you're working
> with that Course property, then you can directly access the "Code" property.
>
> On 9/10/07, jimmysjams < jimmysj...gmail.com">jimmysj...gmail.com> wrote:
>;
>
&gt;
>
>;
> > All I want from the object is the value in CourseSection.Course.Code.
> > I've set up a loop to go through the types listed in a string array
> > until it gets to Code and when I try to get the value I no longer have
>; > the correct object to pass in as an parameter.  ;It should also be
> > noted that I need this to be as dynamic as possible, this program
&gt; > won't always be working with a CourseSection object.
>
> > On Sep 10, 1:19 pm, "Marty Thompson&quot; < marty.thomp...gmail.com">marty.thomp...gmail.com> wrote:
>; > > What you need to do is not call the property on the property, if that
>; > makes
> > > any sense.&nbsp; You should call GetProperty for "CourseSection&quot; and then
> > work
> > > with that object to get the value of Course.Code.
>
>; > > On 9/10/07, jimmysjams < jimmysj...gmail.com">jimmysj...gmail.com> wrote:
>;
> > > > I have a slight problem.&nbsp; I am trying to get object properties
> > > > dynamically. &nbsp;This just screams reflection, the problem I'm having is
> > > > getting the right property.&nbsp; The properties I need from the object are
> > > > in another object within the current object.&nbsp; Specifically, I have an
> > > > object ScheduleCountView, within it is CourseSection, Course, and
> > > > Code.
>
> > > > I try to get the CourseSection.Course.Code property with
> > > > type.GetProperty(&quot;CourseSection.Course.Code"), where type equals
>; > > > ScheduleCountView, and for soem reason it always returns null. ; Does
> > > > anybody have any suggestions on how to get the information I need from
> > > > the object?&nbsp; Thanks in advance.
&gt;
> > > > Jimmy- Hide quoted text -
>
>; > > - Show quoted text -- Hide quoted text -
>
&gt; - Show quoted text -

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/C_Sharp?hl=en
-~----------~----~----~----~------~----~------~--~---

[1-6]

about | contact  Other archives ( Real Estate discussion Medical topics )