type definition

  • Thread starter Thread starter ZhangZQ
  • Start date Start date
Z

ZhangZQ

The Assembly provides GetTypes() for retrieving the defined type, is there a
way to add my type dynamically?


Thank you very much!
 
Hi,

Any types (classes, structs enums etc.) you define in the Assembly are
automatically part of that Assembly and accessable through reflection.

Hope this helps

Chris Taylor
 
The Assembly provides GetTypes() for retrieving the defined type, is
there a way to add my type dynamically?


You can create Types dynamically with System.Reflection.Emit

Greets
Peter

--
------ooo---OOO---ooo------

Peter Koen - www.kema.at
MCAD CAI/RS CASE/RS IAT

------ooo---OOO---ooo------
 
Chris Taylor,

Thank you.

Yes, that is what I have to do. But I want to know if it is possible to add
another assembly into current running assembly? then I can refer that data
type in current running assembly.

Thanks.
 
Hi,

Please forgive me if I am not understanding you correctly. The current
running Assembly can use a public type in another Assembly by using either
Activator.CreateInstance() of Assembly.Load().

Regards

Chris Taylor
 
Dear Chris Taylor,

I tried this, supposing I used VC++ to create an OCX call MyOCX, and there
is a object in the OCX call MyObject. I used the AxImp.exe to generate the
proxy assembly call MyOCXLib.dll. Now in my code

Assembly myAssembly = Assembly.LoadFrom("MyOCXLib.dll");
Type myObjType = myAssembly.GetType("MyOCXLib.MyObject");

MyOCXLib.MyObject myObj =
(MyOCXLib.MyObject)Activator.CreateInstance(myObjType);
// the above line will cause an exception to be thorwn with error message
"specified cast in invalid"
// If I change to the following line
MyOCXLib.MyObject myObj =
(MyOCXLib.MyObject)Activator.CreateInstance(typeof(MyOCXLib.MyObject));
// then it is ok.

that is the problem. do you know why? I think the MyOCXLib.MyObject type
does not exist in the current Assembly. What do you think?


Regards,


ZhangZQ
 
Hi,

You do not need to use Assembly.LoadFrom and Activator.Create..., I gave
both as an alternative.
Try the following.

MyOCXLib.MyObject myObj = (MyOCXLib.MyObject)Activator.CreateInstance(
"MyOCXLib.dll", "MyOCXLib.MyObject" );

I did not test this so excuse any typos.

Hope this helps

Chris Taylor
 
Thanks, let me try it.

Chris Taylor said:
Hi,

You do not need to use Assembly.LoadFrom and Activator.Create..., I gave
both as an alternative.
Try the following.

MyOCXLib.MyObject myObj = (MyOCXLib.MyObject)Activator.CreateInstance(
"MyOCXLib.dll", "MyOCXLib.MyObject" );

I did not test this so excuse any typos.

Hope this helps

Chris Taylor
 
Back
Top