I wrote a class that is basically a wrapper for creating dynamic objects
based on their assembly and type name (as string). I use:

 
Type.GetType(TypeNameAndAssemblyString).GetContructor(ParamTypes[]).Invo
ke(ParamValueObjects[]);

I have that wrapped in a generic method that:

 return T returnObject;


Here is a quick stripped down version of the method I wrote:

        public static T CreateObjectFromString(string typeName, Type[]
types, string[] paramValues)
        {
            T returnObject;
            object[] parameters = new object[types.Length];
            for (int i = 0; i <= types.GetUpperBound(0); i++)
            {
                parameters[i] = Convert.ChangeType(paramValues[i],
types[i]);
            }
            returnObject =
(T)Type.GetType(typeName).GetConstructor(types).Invoke(parameters);
            return returnObject;
        }

I have it wrapped in a class since I have other method to compliment
this one:

        public class DynamicCode<T> {}


This class/method is useful if you know the base type of the object, in
your case (and mine) it is System.Windows.Forms.Form, in which case you
would call it like so:

        Form thisForm = 
                DynamicCode<Form>.CreateObjectFromString(
                        "MyNamespace.MyForm,MyAssembly", 
                        new Type[] {}, 
                        new string[] {});

The empty Type and string arrays are needed, but if they are populated:

        Form thisForm = 
                DynamicCode<Form>.CreateObjectFromString(
                        "MyNamespace.MyForm,MyAssembly", 
                        new Type[] {typeof(string)}, 
                        new string[] {"User"});

The CreateObjectFromString, and therefore the GetContructor() and
Invoke() methods, would look for a contrcutor that accepts a string
param and the pass the value of "User" to it.


Greg

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Ryan Heath
Sent: Thursday, February 16, 2006 05:31
To: [email protected]
Subject: Re: Dynamically Displaying WinForms.

> Thanks for responding, but I need to soft code the form names because 
> I do not know what are called at design time.
>

When I read your question, AppDomain.CreateInstanceAndUnwrap did cross
my mind, but I skipped it as soon as I read you had done similar stuff
in VB6 several years ago... ;)

Calling Dim frm as new form("form1") in VB6, requires VB6 to have a
(hard) reference to Form1 wouldnt it?

Doing a soft reference (or should I say *late bound*) to Form1 would
require a COM registration of Form1.

// Ryan

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to