How to cast to a dynamic type ?

  • Thread starter Thread starter John
  • Start date Start date
J

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!
 
John said:
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 :)
 
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()));
 
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
 
John said:
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.
 
Back
Top