Dynamically Creating instances of classes which subclass a comman abstract class

B

Brett Kelly

Sorry, for the somewhat confusing subject, but my problem is equally
odd (imo).

So, I have an assembly that contains an abstract class (File) and 5
other classes that all subclass it (Exe, Dll, Txt, Config, Sql). I'm
able to do the Assembly.GetTypes() thing and figure out which classes
are subclasses of File, but I need to be able to create instances of
them.

Is there a way I can do something like:

string filename = "foo.sql";
ArrayList types = new ArrayList();
foreach(Type t in Assembly.GetTypes())
{
if(t.IsSubclassOf(typeof(File)))
{
// create an instance of the class T
if(instance.Ext = ".sql")
return instance;
}
}

I hope I'm being clear.

I've pasted all of the code that creates the abstract class and
subclasses here:
http://pastebin.com/427380

Any suggestions?
 
B

Brett Kelly

OK, I'm looking at that now, but the problem is that the constructor
takes parameters and I'm having a hell of a time figuring out how to
pass them. I figured i could use the overload that took a Type arg and
an object array representing the constructor parameters, but that's not
working either. Here's what I've got:

private void button1_Click(object sender, System.EventArgs e)
{
string fname = "foobar.txt";
string target = @"C:\";
string execargs = "";

object[] targs = new object[] {fname,target,execargs};

Assembly a = Assembly.Load("FilePusherFramework");
Type[] types = a.GetTypes();


foreach(Type t in types)
{
if(t.IsSubclassOf(typeof(FilePusherFramework.FileTypes.File)))
{
FilePusherFramework.FileTypes.File i =
(FilePusherFramework.FileTypes.File)Activator.CreateInstance(t,targs);
textBox1.Text += "Type Extension: " + i.TypeExt +
Environment.NewLine;
}
}
}

this code throws a MemberNotFound exception.

Mark - thanks for the reply!

Any other ideas? :)
 
G

Guest

Hi Brett,
looking at your code it looks like your problem is in your variable
definitions which are not the same as the constructor, hence the class you
are trying to create and the variables you are passing do not match:
string fname = "foobar.txt";
string target = @"C:\";
string execargs = "";

the execargs is an array of strings in your exe class constructor not a
string, it should be:

string[] execargs = new string[]{};

also you are going to have problems with your properties, in the set methods
you are not assigning to a variable, you are just calling the property
recursively and it will cause a stack overflow, your code is like:

public class Sql : File
{

public override string Filename
{
get { return this.Filename; }
set { this.Filename = value; }
}

The filename is calling itself in an infinite recursive state.

Hope that helps
Mark
http://www.markdawson.org



Brett Kelly said:
OK, I'm looking at that now, but the problem is that the constructor
takes parameters and I'm having a hell of a time figuring out how to
pass them. I figured i could use the overload that took a Type arg and
an object array representing the constructor parameters, but that's not
working either. Here's what I've got:

private void button1_Click(object sender, System.EventArgs e)
{
string fname = "foobar.txt";
string target = @"C:\";
string execargs = "";

object[] targs = new object[] {fname,target,execargs};

Assembly a = Assembly.Load("FilePusherFramework");
Type[] types = a.GetTypes();


foreach(Type t in types)
{
if(t.IsSubclassOf(typeof(FilePusherFramework.FileTypes.File)))
{
FilePusherFramework.FileTypes.File i =
(FilePusherFramework.FileTypes.File)Activator.CreateInstance(t,targs);
textBox1.Text += "Type Extension: " + i.TypeExt +
Environment.NewLine;
}
}
}

this code throws a MemberNotFound exception.

Mark - thanks for the reply!

Any other ideas? :)
 
G

Guest

Probably the problem is that the reflection subclass of File is NOT the "Text
File" subclass.

the construction needs to be explicit
for example:
//assuming the "text file" subclass of File name is TextFile

Type type = typeof(FilePusherFramework.FileTypes.TextFile);
object[] targs = new object[] {fname,target,execargs};
File file = (File)System.Activator.CreateInstance(type, targs);

regards
 
B

Brett Kelly

I've been thinking about this for awhile now...

How can i set up the accessor/mutator for the properties so they behave
this way?
 

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