Groups | Blog | Home
all groups > dotnet clr > august 2004 >

dotnet clr : Reflection and GetField


Jon Skeet [C# MVP]
8/3/2004 11:31:57 AM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Jon Skeet [C# MVP]
8/3/2004 11:41:19 AM
[quoted text, click to view]

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

--=20
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Eric
8/3/2004 12:22:24 PM
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





Mattias Sjögren
8/3/2004 12:32:15 PM
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Jon Skeet [C# MVP]
8/3/2004 1:26:33 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Jon Skeet [C# MVP]
8/3/2004 2:03:56 PM
[quoted text, click to view]

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

[quoted text, click to view]

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

[quoted text, click to view]

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?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Eric
8/3/2004 2:07:54 PM
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







Mattias Sjögren
8/3/2004 2:31:47 PM
Eric,

[quoted text, click to view]

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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Eric
8/3/2004 2:52:22 PM
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?

"Jon Skeet [C# MVP]" <skeet@pobox.com> schrieb im Newsbeitrag
news:MPG.1b7994cd7dc08ef998b07d@msnews.microsoft.com...
[quoted text, click to view]

Eric
8/3/2004 2:55:21 PM
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


"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> schrieb im Newsbeitrag
news:ed78NWVeEHA.3348@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

Jon Skeet [C# MVP]
8/3/2004 3:22:07 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Eric
8/3/2004 4:04:54 PM
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.


"Jon Skeet [C# MVP]" <skeet@pobox.com> schrieb im Newsbeitrag
news:MPG.1b799d972559b19f98b082@msnews.microsoft.com...
[quoted text, click to view]

AddThis Social Bookmark Button