type definition

Z

ZhangZQ

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


Thank you very much!
 
C

Chris Taylor

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
 
P

Peter Koen

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------
 
Z

ZhangZQ

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.
 
C

Chris Taylor

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
 
Z

ZhangZQ

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
 
C

Chris Taylor

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
 
Z

ZhangZQ

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top