Hi Jürgen,
Here's one possbility:
Use the ComponentModel classes to your advantage by making class1 a
Component.
Another class (an ISite implementation) keeps track of the name for a single
component, an IContainer implementation (Container or a derived type, for
instance) keeps track of all of the instances of the component, and a naming
service (INameCreationService implementation or a custom interface) can
create and validate component names. The GUI is a simple matter of using
reflection to gather a list and invoke members on the selected class when a
button is clicked.
1. You could add a static field to class1 to keep track of all instances.
It could be a read-only instance of the System.ComponentModel.Container
class.
Note: you may want to create a class1Manager class that does all this
instead of using a static field on class1, but it will work either way.
2. class1 could derive from System.ComponentModel.Component, which will
provide two useful features:
A. Site property
I. Returns an ISite implementation that provides the classes unique
name within the context of your Container.
B. Canonical disposable pattern
I. When the class is disposed remove it from the static container.
3. In the constructor of class1 add the instance to the static container and
it will automatically create an ISite for it (assuming that you're using the
Container class).
4. Create a naming service that can be exposed through the class1.GetService
method (inherited from Component), which you can use to create a new name
for the Site of each new class within the Container. You can use the
System.ComponentModel.Design.Serialization.INameCr eationService interface as
a foundation if you'd like, or create your own.
5. Hook them up together so that when adding a new component without
specifying a name the naming service will provide a default name such as,
[[ componentType.Name + (container.Components.Count + 1) ]], or something
like that.
There's more to it, but hopefully you'll have a place to start.
As for the GUI:
1. Use reflection to create a list of strings that contains the names of
each component in the static container
2. Bind the list to the ComboBox or just iterate each component, adding a
new item to the list for each [[ component.Site.Name ]].
3. Use reflection to invoke a member on the selected class, which can be
retrieved from the static container by name.
That's the gist of one possible solution.
HTH
--
Dave Sexton
http://davesexton.com/blog http://www.codeplex.com/DocProject (Sandcastle in VS IDE)
<Ch*******@gmx.dewrote in message
news:11*********************@a75g2000cwd.googlegro ups.com...
Hi Group.
First of all I'm sorry for asking (maybe) such easy questions. But I'm
quite stuck now and couldn't come up with a solution by using my C#
book or googling. So it would be nice if someone could help me out.
Now first of all I want to describe the setting in a simpler (I'm using
Visual Studio 2005):
Form: One comboBox (dropdown list) and 2 buttons.
Classes: Class1 with a function do() which plainly returns a string.
Ok, what do I want to do with these?
The comboBox should show all current instances of Class1 (even if some
are instanced at runtime).
Button1 should create a new instance of Class1 with a fixed
instancename + a number (e.g. there is a string with the value
"testclass" and an increasing integer "i". Instances should be created
with a name combined from both -string+integer -testclass1 ,
testclass2 etc.)
Button2 should call a function from the currently in the comboBox
selected class. (e.g. "testclass2" is selected in the list -click
calls "testclass2.do()" )
So my problems with this setting are:
- Is it possible in any way to instance a class with a name combined of
string and an integer? Maybe with some kind of boxing?
- Keeping the comboBox updated. Maybe ILists are the way to go here?
- Calling functions of the currently selected class in the comboBox.
Hope it gets clear with this explanation.
Thanks alot.
Jürgen