Home All Groups Group Topic Archive Search About

How to cast to a dynamic type ?

Author
12 Jan 2005 11:07 PM
John
I am confused how to cast to a type which is obtained from reflection:

Normally, this is what we do:

using ABC;
....
if (obj is ABC.MyClass)
{
    ((ABC.MyClass)obj).MyProperty = "value";
}

but now I want to obtain the type of ABC.MyClass from reflection, like this:
using System.Reflection;
....
Assembly objAssembly = Assembly.Load("ABC");
Type t = objAssembly.GetType("ABC.MyClass");
if (obj is t)
{
    ((t)obj).MyProperty = "value";
}

It will raise compile error: The type or namespace name 't' could not be
found (are you missing a using directive or an assembly reference?)

Anyone can give a hint on how to deal with dynamic Type that obtained from
Assembly ?
Thanks a lot!

Author
13 Jan 2005 7:11 AM
Jon Skeet [C# MVP]
John <J***@hotmail.com> wrote:
Show quote
> I am confused how to cast to a type which is obtained from reflection:
>
> Normally, this is what we do:
>
> using ABC;
> ...
> if (obj is ABC.MyClass)
> {
>     ((ABC.MyClass)obj).MyProperty = "value";
> }
>
> but now I want to obtain the type of ABC.MyClass from reflection, like this:
> using System.Reflection;
> ...
> Assembly objAssembly = Assembly.Load("ABC");
> Type t = objAssembly.GetType("ABC.MyClass");
> if (obj is t)
> {
>     ((t)obj).MyProperty = "value";
> }
>
> It will raise compile error: The type or namespace name 't' could not be
> found (are you missing a using directive or an assembly reference?)
>
> Anyone can give a hint on how to deal with dynamic Type that obtained from
> Assembly ?

The normal solution is to cast to an interface. The reason you cast is
to effectively tell the compiler that you know more information than it
does, so you can then call methods, use properties etc. In this case,
you haven't made it obvious that you *do* know anything more than the
compiler - what methods do you know that t contains, and are they
already encapsulated in an interface? If so, cast it to that interface.
If not, encapsulate it in an interface and then cast it :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Author
13 Jan 2005 9:24 PM
John
Thank for the replay.
I am not quite follow "cast it to that interface". In my case, ABC.MyClass
is just a normal class. I just want to use Assembly.Load("ABC") to get the
type of MyClass from ABC.dll.
Then I want to test is obj is the type of MyClass.  Would you please give me
a smaple code that will help more in understanding?  Thanks



Show quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1c502f806187cd5798bc04@msnews.microsoft.com...
> John <J***@hotmail.com> wrote:
> > I am confused how to cast to a type which is obtained from reflection:
> >
> > Normally, this is what we do:
> >
> > using ABC;
> > ...
> > if (obj is ABC.MyClass)
> > {
> >     ((ABC.MyClass)obj).MyProperty = "value";
> > }
> >
> > but now I want to obtain the type of ABC.MyClass from reflection, like
this:
> > using System.Reflection;
> > ...
> > Assembly objAssembly = Assembly.Load("ABC");
> > Type t = objAssembly.GetType("ABC.MyClass");
> > if (obj is t)
> > {
> >     ((t)obj).MyProperty = "value";
> > }
> >
> > It will raise compile error: The type or namespace name 't' could not be
> > found (are you missing a using directive or an assembly reference?)
> >
> > Anyone can give a hint on how to deal with dynamic Type that obtained
from
> > Assembly ?
>
> The normal solution is to cast to an interface. The reason you cast is
> to effectively tell the compiler that you know more information than it
> does, so you can then call methods, use properties etc. In this case,
> you haven't made it obvious that you *do* know anything more than the
> compiler - what methods do you know that t contains, and are they
> already encapsulated in an interface? If so, cast it to that interface.
> If not, encapsulate it in an interface and then cast it :)
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
Author
13 Jan 2005 10:22 PM
Jon Skeet [C# MVP]
John <J***@hotmail.com> wrote:
> Thank for the replay.
> I am not quite follow "cast it to that interface". In my case, ABC.MyClass
> is just a normal class. I just want to use Assembly.Load("ABC") to get the
> type of MyClass from ABC.dll.
> Then I want to test is obj is the type of MyClass.  Would you please give me
> a smaple code that will help more in understanding?  Thanks

Well, the object *will* be an instance of MyClass, because that's how
you've created. The question is what you then want to do with it. In
your sample, you've used a property (MyValue). Now, how did you know
that there's a property called MyValue? If you know that every class
you'll be loading using reflection has a property called MyValue, you
should make them all implement an interface which declares that
property. If you don't, you've got to use reflection to set the
property.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Author
13 Jan 2005 2:47 PM
Mark Newmister
Does any of the following code help?  Note the "Unwrap()" aspect.

System.Runtime.Remoting.ObjectHandle _ohtemp;

_ohtemp =
Activator.CreateInstance(sAssemblyReference,"spnProviderServices.com.apache1.com_cji_webservices_cjx_vo_LoginInfo");
object oWSLogin = _ohtemp.Unwrap();
Type WSLoginType = oWSLogin.GetType();

FieldInfo myInfo;
myInfo = WSLoginType.GetField("firmID");
myInfo.SetValue(oWSLogin,int.Parse(hd.Tables[0].Rows[0]["FirmID"].ToString()));

AddThis Social Bookmark Button