Sure can:

using System;

namespace ReflectTest
{
        public abstract class Test
        {
                public static Test CreateTest(string type)
                {
        
return((Test)typeof(Test).Assembly.CreateInstance(type,true));
                }

        }

        public class TestA : Test
        {
        }

        public class TestB : Test
        {
        }

        public class DoTestCreation
        {
                public static void Run()
                {
                        object o = Test.CreateTest("ReflectTest.TestA");

                        if(o != null)
                        {
        
System.Windows.Forms.MessageBox.Show(o.GetType().Name);
                        }

                        o = Test.CreateTest("ReflectTest.TestB");

                        if(o != null)
                        {
        
System.Windows.Forms.MessageBox.Show(o.GetType().Name);
                        }
                }
        }
}

-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Bill Bassler
Sent: Monday, April 05, 2004 11:36 AM
To: [EMAIL PROTECTED]
Subject: [ADVANCED-DOTNET] Using reflection internally to create an
instance based on a string name

Can reflection be used internally to create a class instance based on an
input string containing the class name? Something somewhat similar to
the
code below however typeof takes a type as an argument, not a string.

Activator.CreateInstance seems to require that a reference to the
containing .dll is obtained. I'm interested in creating an instance of a
class defined in the same assembly as the creator method based on an
input
string that matches the class name.

// Some dll
public abstract class InstanceCreator
{
   public static InstanceCreator GetObject(string type)
   {
 return typeof(type); // return type with name corresponding to
input string
   }
}

public class SomeClass1
{
}

public class SomeClass2
{
}

// end some dll

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

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

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

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

Reply via email to