Creating Objects from Types at Runtime

  • Thread starter Thread starter Mark Olbert
  • Start date Start date
M

Mark Olbert

I think I'm running into a problem caused by the strong-typing system in dotnet.

I have a custom control which can be configured, at design time, to work with one of a number of
DataTables in a DataSet. The custom control has a method which puts up a modal form at runtime; the
type of modal form created depends on the specific DataTable selected at designtime. All of the
specific modal forms are descendants of GenericModalForm.

The way I'm trying to handle this is to require the user, at designtime, to select a particular type
of modal form. I've written a routine that scans, at designtime, the project assembly and extracts
every type that is descended from GenericModalForm. Selecting the one to use then becomes a simple
matter of writing a UITypeEditor for the modal form property.

All of that works fine. However, at runtime, when I try to create an instance of the specific modal
form, Type.GetType() fails, claiming it can't find the type in the assembly. This is odd, since the
string I pass Type.GetType() is qualified with the assembly name (e.g.,
TestProject.SpecificModalForm), so the type ought to be findable.

I'm wondering if the fact that the project is recompiled before it runs is causing the type string
to be "one version behind" the type actually in the running assembly. I think that would cause the
problem I'm seeing, but I'm not sure.

Does anyone have some references on how to retrieve types from a project assembly at designtime, and
then use that information to create instances of the type at runtime?

- Mark
 
Hi mark,

If the type is defined in the loaded assemblies,
the type string like
"ClassLibrary1.Class1,ClassLibrary1" ("FullTypeName,AssemblyName")
should be enough to get a type in the loaded assemblies.

Using the full qualified type name will force CLR load the type in the
assembly with defined version, which causes the exception.




Does it resolve your problem?

Feel free to let me know if you still have problem on it.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Ying-Shen,

Is there a method call that will generate that particular format of type-name string for me?

What I've been doing is just:

string typeNameString = typeof(className).ToString();

which is not working.

- Mark
 
Thanks for your reply,
then does this way resolve your problem?

I didn't find a method or property which will return this format, but you
can concat the string like the code below, or substring the
AssemblyQualifiedName property.
string typeString = String.Format("{0},{1}",typeof(className).ToString,
typeof(className).Assembly.GetName().Name);


Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Ying-Shen,

Thanx for the suggestion. Oddly enough, it was exactly what I came up with myself after fumbling
around through the docs for a bit.

- Mark
 
Back
Top