field is not a TextBox--field is a FieldInfo instance, which, if you look up
the inheritance hierarchy, has no inheritance relationship to TextBox
whatsoever.

Don't confuse the representation of the reflective type with the type
itself.

Ted Neward
{.NET || Java} Course Author & Instructor, DevelopMentor
(http://www.develop.com)
http://www.javageeks.com/tneward
http://www.clrgeeks.com/tneward

----- Original Message -----
From: "donatas" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, May 21, 2002 11:06 AM
Subject: [ADVANCED-DOTNET] InvalidCastException when using Reflection


> Hi all,
>
> I have strange problem with .NET Reflection. The basic scenario follows:
> Using reflection I create the object of the type in the my own assembly.
> Instance is created fine. Then use reflection to get info about type
fields.
> I want to use the field of the object just created:
>
>        object field = fieldInfo.GetValue(form);
>        switch (fieldInfo.FieldType.FullName)
>        {
>  case "System.Windows.Forms.TextBox":
>   txtBox = (TextBox)field;
>               break;
>         default:
>        break;
>        }
> The cast statement txtBox = (TextBox)field; throws the
InvalidCastException.
> What I'm doing wrong ? Is casting not allowed on reflected types?
>
> You can read messages from the Advanced DOTNET archive, unsubscribe from
Advanced DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.
>

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to