Reflection / Casting Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Angelos Karantzalis

    Reflection / Casting Question

    Hi guys,

    I'm trying to load a class instance dynamically, and then cast it to it's
    base type so I can use it in my app.

    More specifically, I'm dynamically instantiating a
    System.Web.UI.W ebControls.Requ iredFieldValida tor and attempting to cast it
    into an IValidator and/or a BaseValidator. Both casting attempts have
    failed miserably, throwing an InvalidCastExce ption ... how can it be i
    cannot cast to the base class of my instance ???

    More specifically, I load the Assembly directly from the System.Web.dll, and
    create the RequiredFieldVa lidator instance using .CreateInstance (string
    strClassName) on the Assembly.

    I get back a perfectly good object, for which I'm using reflection to set
    some properties before I cast it to it's base type (BaseValidator) and
    return it from a method to the caller. upon casting it to the base type, I
    get the InvalidCastExce ption. This shouldn't be happening normally, and i'm
    bangin' my head against a wall to find out what could be causing it ...

    Could it be that the application uses a reference to the System.Web.dll, and
    my own loader loads another instance of the assembly ? If this is it, how
    could I work around it ?

    Thanks a million,

    Angel
    O:]


  • Jorge Matos

    #2
    RE: Reflection / Casting Question

    Loading System.Web.dll should not load another instance of the assembly, once
    an assembly is loaded into memory, you cannot load additional instances of
    it. Please post an example of what your code. I would also try to create a
    simple test app to isolate the code you are having a problem with.

    "Angelos Karantzalis" wrote:
    [color=blue]
    > Hi guys,
    >
    > I'm trying to load a class instance dynamically, and then cast it to it's
    > base type so I can use it in my app.
    >
    > More specifically, I'm dynamically instantiating a
    > System.Web.UI.W ebControls.Requ iredFieldValida tor and attempting to cast it
    > into an IValidator and/or a BaseValidator. Both casting attempts have
    > failed miserably, throwing an InvalidCastExce ption ... how can it be i
    > cannot cast to the base class of my instance ???
    >
    > More specifically, I load the Assembly directly from the System.Web.dll, and
    > create the RequiredFieldVa lidator instance using .CreateInstance (string
    > strClassName) on the Assembly.
    >
    > I get back a perfectly good object, for which I'm using reflection to set
    > some properties before I cast it to it's base type (BaseValidator) and
    > return it from a method to the caller. upon casting it to the base type, I
    > get the InvalidCastExce ption. This shouldn't be happening normally, and i'm
    > bangin' my head against a wall to find out what could be causing it ...
    >
    > Could it be that the application uses a reference to the System.Web.dll, and
    > my own loader loads another instance of the assembly ? If this is it, how
    > could I work around it ?
    >
    > Thanks a million,
    >
    > Angel
    > O:]
    >
    >
    >[/color]

    Comment

    • Angelos Karantzalis

      #3
      Re: Reflection / Casting Question

      Here's what I did in my code initially ...

      public System.Web.UI.W ebControls.Base Validator[] GetValidators() {


      System.Web.UI.W ebControls.Base Validator[] validators = new
      System.Web.UI.W ebControls.Base Validator[this.ValidatorD escriptors.Leng th];


      for(int i=0; i<this.Validato rDescriptors.Le ngth; i++){

      ValidatorDescri ptor validator = this.ValidatorD escriptors[i];

      ObjectHandle objHndl = Activator.Creat eInstance("Syst em.Web",
      validator.Class Name);

      object instance = objHndl.Unwrap( );

      foreach(Propert y prop in validator.Prope rties)

      TypeInspector.S etPropertyValue (prop.Name, prop.Value, instance);


      try{

      validators[i] = (System.Web.UI. WebControls.Bas eValidator)inst ance;

      }catch(Exceptio n e){

      throw new InvalidCastExce ption("Cannot cast to IValidator for class
      "+instance.GetT ype().ToString( )+" original : "+validator.Cla ssName);

      }

      }

      return validators;


      }



      ... this is executed inside a Web Appliation, so the System.Web assembly
      *should* be available to the Activator .. however, I get an exception saying
      that the ASP.NET runtime cannot load the System.Web assembly !!! (Weird ?)

      So, I changed the code:

      ObjectHandle objHndl = Activator.Creat eInstance("Syst em.Web",
      validator.Class Name);

      object instance = objHndl.Unwrap( );

      ... to use a class I'd created on my own ...

      object instance =
      DynamicClassLoa der.GetInstance ().Load(validat or.ClassName);

      what the DynamicClassLoa der does, is locate the class name in it's
      configuration file, read the asembly it belongs to from there, and then:

      ... in the initialization method ...

      AssemblyDescrip tor ad = (AssemblyDescri ptor)lstAssembl ies[i];

      try{

      Assembly objAssembly = Assembly.LoadFr om(ad.CodeBase) ;

      ... . in the Load(strClassna me) method

      return objAssembly.Cre ateInstance(str ClassName);

      ....

      That code works ok, and returns an instance of the class I'm asking for, the
      RequiredFieldVa lidator. However, when I cast it to a BaseValidator, I get
      the InvalidCastExce ption ...

      ... and that's my problem right there. I get some weird exception in both
      attempts, either using dynamic assembly loading, or not :(

      Angel

      O:]





      "Jorge Matos" <JorgeMatos@dis cussions.micros oft.com> wrote in message
      news:7BCE116F-8D61-4D62-BC63-6E7A2AF752EB@mi crosoft.com...[color=blue]
      > Loading System.Web.dll should not load another instance of the assembly,[/color]
      once[color=blue]
      > an assembly is loaded into memory, you cannot load additional instances of
      > it. Please post an example of what your code. I would also try to create[/color]
      a[color=blue]
      > simple test app to isolate the code you are having a problem with.
      >
      > "Angelos Karantzalis" wrote:
      >[color=green]
      > > Hi guys,
      > >
      > > I'm trying to load a class instance dynamically, and then cast it to[/color][/color]
      it's[color=blue][color=green]
      > > base type so I can use it in my app.
      > >
      > > More specifically, I'm dynamically instantiating a
      > > System.Web.UI.W ebControls.Requ iredFieldValida tor and attempting to cast[/color][/color]
      it[color=blue][color=green]
      > > into an IValidator and/or a BaseValidator. Both casting attempts have
      > > failed miserably, throwing an InvalidCastExce ption ... how can it be i
      > > cannot cast to the base class of my instance ???
      > >
      > > More specifically, I load the Assembly directly from the System.Web.dll,[/color][/color]
      and[color=blue][color=green]
      > > create the RequiredFieldVa lidator instance using .CreateInstance (string
      > > strClassName) on the Assembly.
      > >
      > > I get back a perfectly good object, for which I'm using reflection to[/color][/color]
      set[color=blue][color=green]
      > > some properties before I cast it to it's base type (BaseValidator) and
      > > return it from a method to the caller. upon casting it to the base type,[/color][/color]
      I[color=blue][color=green]
      > > get the InvalidCastExce ption. This shouldn't be happening normally, and[/color][/color]
      i'm[color=blue][color=green]
      > > bangin' my head against a wall to find out what could be causing it ...
      > >
      > > Could it be that the application uses a reference to the System.Web.dll,[/color][/color]
      and[color=blue][color=green]
      > > my own loader loads another instance of the assembly ? If this is it,[/color][/color]
      how[color=blue][color=green]
      > > could I work around it ?
      > >
      > > Thanks a million,
      > >
      > > Angel
      > > O:]
      > >
      > >
      > >[/color][/color]


      Comment

      Working...