473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Dynamically Creating instances of classes which subclass a comman abstract class

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?

Nov 17 '05 #1
5 4444
Hi Brett,
you can use Activator.CreateInstance to create an instance of a type from
a Type definition.

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

"Brett Kelly" wrote:
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?

Nov 17 '05 #2
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.FileT ypes.File)))
{
FilePusherFramework.FileTypes.File i =
(FilePusherFramework.FileTypes.File)Activator.Crea teInstance(t,targs);
textBox1.Text += "Type Extension: " + i.TypeExt +
Environment.NewLine;
}
}
}

this code throws a MemberNotFound exception.

Mark - thanks for the reply!

Any other ideas? :)

Nov 17 '05 #3
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" wrote:
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.FileT ypes.File)))
{
FilePusherFramework.FileTypes.File i =
(FilePusherFramework.FileTypes.File)Activator.Crea teInstance(t,targs);
textBox1.Text += "Type Extension: " + i.TypeExt +
Environment.NewLine;
}
}
}

this code throws a MemberNotFound exception.

Mark - thanks for the reply!

Any other ideas? :)

Nov 17 '05 #4
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

Nov 17 '05 #5
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?

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Wohlbier | last post by:
Hi, I have a basic programming question regarding classes in python. I want to have a list of "primaryClass" instances, and in each instance of primaryClass I would like a list of "subClass"...
6
by: Ben Finney | last post by:
Howdy all, Okay, so Guido doesn't like Abstract Base Classes, and interfaces are the way of the future. But they're not here now, and I understand ABCs better. I want my modules to...
3
by: chrisspen | last post by:
Is there a way to loop through all instantiated objects and update their classes when a source file changes? I know about Michael Hudson's method...
0
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
9
by: sashang | last post by:
Hi I'd like to use metaclasses to dynamically generate a class based on a parameter to the objects init function. For example: class MetaThing(type): def __init__(cls, name, bases, dict,...
3
by: Matt F. | last post by:
I have an abstract class that about a dozen sub-classes inherit from. I want to enforce that each sub-class shadows an event in the abstract class, but can't quite figure out how to do this. ...
9
by: MikeB | last post by:
Hi, I'd appreciate some help, please. I'm writing a VS2005 VB project for school and one of the requirements is that every screen should have a "Help" button. I could do it by writing a clumsy...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
3
by: Peter Morris | last post by:
Hi all Let's say I am creating a model to represent classes and properties. In addition to this I need instances of classes and values for those properties. KEY: (AssociationEndName)
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.