polenius

Hi,

My code does somethink like this:

  1. Load assembly from file
  2. Create Type using CLID (mainType)
  3. Activator.CreateInstance(mainType)
  4. Now my code gets properties in this way : o.GetType().GetProperty("ActiveDocument", appDoc).GetValue(o, null); where o is object made by Activator and in appDoc is type from loaded assembly.

This all work fine, everytime I can get Type of object type variable. However now I need to make cast. I have type named Entity and type named ExternalReference. Relation between this types is illustrated below. How can I cast Entity variable to ExternalReference type Varible of type Entity is made by method call so I cannot suppress return type of method.

When I add assembly to references in VS2005 and I do something like

Entity variable = value;

ExternalReference ext = (ExternalReference)variable;

it works. For my situation (assembly is loaded when program is running) a try TypeConverter class to made a cast, but i throws exception that Entity cannot be cast to ExternalReference.

Hierarchy of types (rewrited from object browser)

ExternalReference

- Base Types

- IExternalReference

- IBlockReference

- IEntity

- IObject (this is not System.Object from .NET)

Thanks in advance...

Jind ich B ezina




Re: Visual C# General Casting from Type to Type of loaded assembly

OmegaMan

If the original object is/was an ExternalReference then the cast would work; but because it appears the code is upcasting from an Entity object, that is not allowed.

When the original object is a lower (on the hierarchical food chain) object such as the entity object mentioned, then the best one could do would to create a copy constructor on the ExternalReference object that takes in the Entity and create a new object of the desired type.




Re: Visual C# General Casting from Type to Type of loaded assembly

polenius

OmegaMan wrote:
If the original object is/was an ExternalReference then the cast would work; but because it appears the code is upcasting from an Entity object, that is not allowed.

When the original object is a lower (on the hierarchical food chain) object such as the entity object mentioned, then the best one could do would to create a copy constructor on the ExternalReference object that takes in the Entity and create a new object of the desired type.




Re: Visual C# General Casting from Type to Type of loaded assembly

OmegaMan

A copy constructor for this situation is just that you will have your top level object take in its constructor the lower level object. Within the initialization code you will assign the values present in the original object. Here is some psuedo code.


Public MyTopClass(myLower ml)
{
// Items shared are copied over.
this.propA = ml.propA;
this.probB = ml.probB;

// Other items not shared are setup to default values.
this.State = MyMid.EState.working;
....

}









Re: Visual C# General Casting from Type to Type of loaded assembly

polenius

OmegaMan wrote:
A copy constructor for this situation is just that you will have your top level object take in its constructor the lower level object. Within the initialization code you will assign the values present in the original object. Here is some psuedo code.


Public MyTopClass(myLower ml)
{
// Items shared are copied over.
this.propA = ml.propA;
this.probB = ml.probB;

// Other items not shared are setup to default values.
this.State = MyMid.EState.working;
....

}




I imagine something like this. But I'm not sure how to do this because I don't have source codes, I have only assembly which contains types which I use in my code.

Regards Jind ich B ezina






Re: Visual C# General Casting from Type to Type of loaded assembly

OmegaMan

Then my suggestion is at an end...for you need to either work with what is returned or create a new object of what you want.