Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / August 2004

Tip: Looking for answers? Try searching our database.

Reflection and GetField

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eric - 03 Aug 2004 11:22 GMT
Hi,

I'm trying to find a field in a object.
I use this function:
  pObj.GetType().GetField(pstrFieldname)

But it seems that he finds only fields which are initiated.

Ex.:

Public Class xy

   Dim zz as YY

End Class

Public Class YY

  Dim str as String

End Class

If zz is nothing/null he doesn't find the fields (for example 'str') in the
class zz.

Is there an other way to find these fields?

Thanks

Eric
Jon Skeet [C# MVP] - 03 Aug 2004 11:31 GMT
> I'm trying to find a field in a object.
> I use this function:
[quoted text clipped - 20 lines]
>
> Is there an other way to find these fields?

I don't think that's the case.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Eric - 03 Aug 2004 13:07 GMT
Thanks for answering.

Perhaps my question wasn't so clear.
Here's the example (Sorry, it's in VB):
He finds 'insurance', but not 'ofacid'

Imports System.Reflection

Public Class CardIndex

'<remarks/>

Public insurance As insuranceType

Public empiid As Long

End Class

Public Class insuranceType

Public ofacid As Integer

End Class

Module Module1

Sub Main()

Dim field As FieldInfo

Dim pobj As New CardIndex

field = pobj.GetType().GetField("ofacid")

If field Is Nothing Then

Console.WriteLine("OfacId: Nothing")

Else

Console.WriteLine("OfacId: " + field.FieldType.Name)

End If

field = pobj.GetType().GetField("insurance")

If field Is Nothing Then

Console.WriteLine("insurance: Nothing")

Else

Console.WriteLine("insurance: " + field.FieldType.Name)

End If

Console.ReadLine()

End Sub

End Module
Jon Skeet [C# MVP] - 03 Aug 2004 13:26 GMT
> Thanks for answering.
>
> Perhaps my question wasn't so clear.
> Here's the example (Sorry, it's in VB):
> He finds 'insurance', but not 'ofacid'

That's because ofacid is a member of the insuranceType class, not of
the CardIndex class, but you're asking for it as if it were part of
insuranceType.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Eric - 03 Aug 2004 13:52 GMT
Yes, this is know.

My question was, if there is an easy way to 'go down the class tree' to find
the field.
Is there no recursive function for doing this?

> > Thanks for answering.
> >
[quoted text clipped - 5 lines]
> the CardIndex class, but you're asking for it as if it were part of
> insuranceType.
Jon Skeet [C# MVP] - 03 Aug 2004 14:03 GMT
> Yes, this is know.

It makes your first question somewhat odd then - it has nothing to do
with the values of fields.

> My question was, if there is an easy way to 'go down the class tree'
> to find the field.

What, you want to use the type of the field to look for other
"subfields"?

(That wasn't the question you asked, btw. It would help if you could be
a bit more specific to start with.)

> Is there no recursive function for doing this?

Not built-in, no. It wouldn't be too hard to write though. Bear in mind
that the declared type of a field may be different from the actual
type. For instance, a field of type "object" might have a value which
is a reference to an object of type "CardIndex" - which type would you
want to search in that situation?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Eric - 03 Aug 2004 15:04 GMT
Sorry, I'm not perfect in English.

But what I try to do is the following:
- I have a flat text file with a header and rows of data
- I read the header and get this way the name of the desired (destinations)
fields.
- I create a new empty object to fill it up with the first data record from
the flat file
 (The class definition is created from a XSD file)
- Now I try to match the field name with the class structure
- If field is found, I set the value with the value from the flat file

Conditions:
* Field names are unique
* Only Basetypes are served

I like to do it this way, because the XSD can change often (Names and
Structure) and also the columns of the flat file.

> > Yes, this is know.
>
[quoted text clipped - 17 lines]
> is a reference to an object of type "CardIndex" - which type would you
> want to search in that situation?
Jon Skeet [C# MVP] - 03 Aug 2004 15:22 GMT
> Sorry, I'm not perfect in English.
>
[quoted text clipped - 14 lines]
> I like to do it this way, because the XSD can change often (Names and
> Structure) and also the columns of the flat file.

And you don't just want the field names of the type itself, but of
contained types.

Well, it's certainly doable fairly easily with recursion, but there's
nothing to do it automatically for you in the framework.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Mattias Sj?gren - 03 Aug 2004 13:31 GMT
Eric,

>He finds 'insurance', but not 'ofacid'

That's because you're checking if the CardIndex class has a field
named ofacid, which it doesn't. If you change

field = pobj.GetType().GetField("ofacid")

to

field = GetType(insuranceType).GetField("ofacid")

it should work.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Eric - 03 Aug 2004 13:55 GMT
Thanks, this I know too.

My question was, if there is an easy way to 'go down the class tree' to find
the field.
Is there no build in recursive function for doing this?

My problem is that I don't know the class structure.
The code should work on every arbitrary class structure.

Thanks
Eric

> Eric,
>
[quoted text clipped - 12 lines]
>
> Mattias
Mattias Sj?gren - 03 Aug 2004 11:32 GMT
Eric,

Can you post some code that reproduces the problem you're seeing? The
value of the field should certainly not affect whether it's found or
not.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jon Skeet [C# MVP] - 03 Aug 2004 11:41 GMT
Mattias Sjögren <mattias.dont.want.spam@mvps.org> wrote:
> Can you post some code that reproduces the problem you're seeing? The
> value of the field should certainly not affect whether it's found or
> not.

And indeed, thinking about it, it *can't* affect whether it's found or
not - because the Type object itself applies to all objects of that
type, and not to any one particular object.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.