Re: Dynamic type convertion..

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Sunny (sunnyask_at_icebergwireless.com)
Date: 04/06/04


Date: Tue, 6 Apr 2004 08:55:06 -0500

Hi Ladislav,
As you already have a base class (it have to inherit from MBR to be
remoted), what you have to do is to cast the newly dynamically created
classes to this base class. Even if your derived classes are
functionally different, they share a common base methods (names) and you
can invoke them, if you cast the classes to the base class.
Will you post an example with simple base class and 2 derived classes.

Sunny

In article <OBBPmx8GEHA.2848@TK2MSFTNGP10.phx.gbl>,
jemi.vsechno@volny.cz says...
> Hi,
>
> Thank you very much for your reply, idea is very good, but I'm not sure if does it suit for my problem, I tried it, and I found some limitations while using this implementation.
>
> I have already written baseClass which implements many Properties and Methosd and other classes are derived from this one. I need, of course, dynamically instantiate many derived classes, which are specific one by one. I'm not still firmly decided to rewrite written code and implements Interfaces, but.. You'll be very kind,if you can shortly descibe the first path to requested solution or have some another idea.
>
> Best regards
>
> Ladislav
>
> "Sunny" <sunnyask@icebergwireless.com> wrote in message news:O4zVSqxGEHA.684@tk2msftngp13.phx.gbl...
> > Hi Ladislav,
> >
> > Still your problem is not related to remoting. But nevertheless, I'll
> > try to help:
> > If you read the docs for ChangeType, you will see that the return type
> > of that method is Object, and if you look at the example, you will see
> > that you have to cast it to the proper type.
> >
> > For your situation, you do not need that method at all, as your object
> > is already the type you want. As I see, your problem is that you do want
> > to use it after that in the code with the proper methods. You can not do
> > this in that way, if during development time you do not know the exact
> > type, so you can put in your code something like:
> > ASMBL.SomeButton myobj = (ASMBL.SomeButton) o;
> >
> > If you want to do it dynamically, you have 2 paths:
> > 1. Use reflection (slow)
> > 2. Use interfaces (or abstract classes). The all the types you want to
> > use that way, should implement that interface (inherit from the abstract
> > class), and you can then cast the object to the interface or abstract
> > class. Here is sample code with interfaces (I type it here, so it may
> > contain some errors, but you'll get the idea):
> >
> > public interface IMyObj
> > {
> > public void Do_1();
> > public void Do_2();
> > ......
> > }
> >
> > //All the classes you want to load dynamically shouldd implement IMyObj
> > public class SomeButton1 : IMyObj
> > {
> > //....some methods
> > public void Do_1()
> > {
> > //do what your class have to do
> > }
> > public void Do_2()
> > {
> > //do what your class have to do
> > }
> > }
> >
> > public class SomeButton2 : IMyObj
> > {
> > //....some methods for class 2
> > public void Do_1()
> > {
> > //do what your class have to do
> > }
> > public void Do_2()
> > {
> > //do what your class have to do
> > }
> > }
> >
> > //then your code, instead of ChangeType will be:
> >
> > IMyObj mybutton = (IMyObj)o;
> >
> > //now you can use
> > mybutton.Do_1();
> > mybutton.Do_2();
> >
> > Hope that helps
> > Sunny
> >
> > In article <eu57VyZGEHA.3772@TK2MSFTNGP12.phx.gbl>,
> > jemi.vsechno@volny.cz says...
> > > Sorry about uncomplete post and now again and better :)
> > > Here is the code:
> > > public Object GetObject(string inType,string inAssmbl)
> > > {
> > > Object objtemp;
> > > System.Runtime.Remoting.ObjectHandle ino;
> > > object[] activationAttributes = new object[0];
> > > ino=Activator.CreateInstance( inAssmbl,
> > > inType,
> > > true,
> > >
> > > BindingFlags.Instance|BindingFlags.Public,
> > > null,
> > > FormDataObjectArray,
> > > //private property of Object type
> > > null,
> > > activationAttributes,
> > > //private property of Object type
> > > null);
> > > objtemp = ino.Unwrap();
> > > return objtemp;
> > > }
> > > someType = "ASMBL.SomeButton";
> > > someAssmbl = "ASMBL";
> > > Object o = GetObject(someType, someAssmbl);
> > > ??? Convert.ChangeType(o,((System.Type)((o.GetType()))));
> > >
> > > And problem description:
> > > I need the returned object change to type "ASMBL.SomeButton" and work with
> > > it like with new instance of that object (set property, call methods,...),
> > > but the method ChangeType still returns object of Object type.
> > >
> > > Can somebody some idea or recomendation where to find solution?
> > > Thanks a lot
> > > LK
> > >
> > >
> > > "Allen Anderson" <allen@sparkysystems.com> wrote in message
> > > news:c69r605v0rmhkgoi4i46m13gqk3rf1nunm@4ax.com...
> > > > so what exactly is your question? I don't see what this has to do
> > > > with remoting at all.
> > > >
> > > > Allen Anderson
> > > > http://www.glacialcomponents.com
> > > > mailto: allen@put my website url here.com
> > > >
> > > >
> > > > On Fri, 2 Apr 2004 16:11:54 +0200, "Ladislav Kepl"
> > > > <jemi.vsechno@volny.cz> wrote:
> > > >
> > > > >Halo, I have one qustion about dynamic type convertion.
> > > > >My idea is to dynamically create Object of some type using Activator
> > > class.
> > > > >Code is:
> > > > >
> > > > >public Object GetObject(string inType){
> > > > >
> > > > >Object objtemp;
> > > > >
> > > > >System.Runtime.Remoting.ObjectHandle ino;
> > > > >
> > > > >object[] activationAttributes = new object[0];
> > > > >
> > > > >ino=Activator.CreateInstance("EWC",
> > > > >
> > > > >inType,
> > > > >
> > > > >true,
> > > > >
> > > > >BindingFlags.Instance|BindingFlags.Public,
> > > > >
> > > > >null,
> > > > >
> > > > >FormDataObjectArray,
> > > > >
> > > > >null,
> > > > >
> > > > >activationAttributes,
> > > > >
> > > > >null);
> > > > >
> > > > >objtemp = ino.Unwrap();
> > > > >
> > > > >return (EWC.EWCWebCustomControls.EwcImgButton)objtemp;
> > > > >
> > > > >//Convert.ChangeType(obj,(System.Type)((myExtenderInterface).GetType()));
> > > > >
> > > > >}
> > > > >
> > > >
> > >
> > >
> > >


Quantcast