Dynamically creating a type at runtime.

P

Psypher8

I really, really want to store some settings somewhere that my control
utilizes to create new object(s)(i.e., class, struct) at runtime without it
knowing about the type it could create at compile time. The type would
support a particular interface or base class let's say.

Couple thoughts:

1. Store the 'serialized' data in the store and let the deserializer create
it for me.
Slower
2. Lookup table (but then my control needs to know all the types it could
create before compile time).
Would be faster than the serialized version.
3. ActiveX control
Here we would have the coclass lookup issue.
4. ?

There must be an easier way. How does the serializer instance the type from
data? I know it stores the class name in the stream but how does it go from
just the classname to the object instance?

Thx for the help.
 
I

Ignacio Machin ( .NET/ C# MVP )

I really, really want to store some settings somewhere that my control
utilizes to create new object(s)(i.e., class, struct) at runtime without it
knowing about the type it could create at compile time.  The type would
support a particular interface or base class let's say.

Couple thoughts:

1.  Store the 'serialized' data in the store and let the deserializer create
it for me.
    Slower
2.  Lookup table (but then my control needs to know all the types it could
create before compile time).
     Would be faster than the serialized version.
3.  ActiveX control
    Here we would have the coclass lookup issue.
4.  ?

There must be an easier way.  How does the serializer instance the type from
data?  I know it stores the class name in the stream but how does it go from
just the classname to the object instance?

Thx for the help.

Hi,

You need to provide more details, but you can always use an Object
reference and access all the members by reflection.
Another alternative that I eager you to explore is define an interface
and make that "dynamic" object you have implement it, in this case at
runtime you can use any type of object as long as it implements that
interface
 
P

Psypher8

Here's an example:

At runtime I load a string that identifies a type.
Example: "SomePackage.SomeFolder.SomeTypeObject"

I would like to create that type now.

I have tried utilizing the Assembly object but it only exposes types inside
that assembly which is nice but won't work in the case I'm developing.
 
M

Marc Gravell

Does Activator.CreateInstance come close? Note that you might need to
work the assembly-qualified names to get the best results.

Marc
 

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