Question

I am trying to create an instance of a typed dataset dynamically in my code at runtime. I have the type available to me, but when I try to do this:

object obj = Activator.CreateInstance(Type.GetType("TYPED DATASET TYPE HERE"));

The problem is the type doesn't seem to be valid according to the code when I try and run it. What could I be doing wrong here?

Was it helpful?

Solution

Where is the type defined? (which assembly).

Unless you give it an assembly qualified name, it will only look in the calling assembly, and a few other key assemblies. Options:

  • use an assembly qualified name ("somen.amespace.sometype, someassembly, ...")
  • get the Assembly instance (from a known type in that assembly), and use GetType(fullyQualifiedName) on the Assembly instance

OTHER TIPS

Assuming you are emitting the correct dataset code you may also need to load the assembly.

Look into the following .net namespaces reflection.emit, and reflection.

You could also look at the open source projects such as windsor which use reflection to emit new classes to create interceptors. There may be code in there that you can learn from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top