I have a program which can read Data from more than one type of source - i.e. a File or XML feed. I have a listview with a list of sources and the source to retrieve from.
Is it possible to assign the type of object created without using an IF statement?
Here is some code detail. I have an Interface (IData) which has a method ReadData. Two classes - XMLData and CSVFileData implement this interface.
Then in a click button event on my Form I have:
Type
t = typeof(CSVFileData);messageQueue =
Control.LoadData(t, listViewDataSource.SelectedItems[0].SubItems[1].Text);
This sends the Type t, and source path (either filename or url) into the static control class.
In the Control.LoadData method I run:
object
o = Activator.CreateInstance(TypeObj); //check that it implements the IData Interface IData dataReader = o as IData; if (dataReader != null){
messages = dataReader.ReadData(Source);
}
I have everything in place to do everything dynamically. The only thing that I would like to get rid of is the need to say:
(pseudocode)
if (listboxValue="CSV") {
Type t = typeof(CSVFileData);
} else {
Type t = typeof(XMLData);
}
How can I assign the Type (CSVFileData) more dynamically?
I thought about adding an extra field to the listview to hold the Type, but I'm not sure how you'd convert the string literal to a type.
I am very new to C# so I hope the above makes sense. Any help/pointers would be greatly appreciated!
AlanPosted Jun 26, 2007, 2:22 PM
http://msdn2.microsoft.com/en-us/library/w3f99sx1.aspx
It works fine as long as you pass it a string which contains the fully qualified name of a type.
MarkPosted Jun 26, 2007, 11:18 AM
Ahh, no what I mean is that there doesn't seem to be an overload for Type that accepts a parameter as in your example:
Type t = Type.GetType(listboxValue);
I can only get it to work with an object instance, ie:
Object
obj = new Object();MessageBox
.Show(obj.GetType().ToString());which returns System.Object as you'd expect.
Did you mean that I would be able to convert the string value held in the listview to a type to be used by:
Activator.CreateInstance(Type);
and if so, how do I do it. That is what I want to achieve as I don't have an object instance until runtime (ie. I don't know which data source the user will select). Sorry for the confusion!
Thanks
Andrew
AlanPosted Jun 26, 2007, 10:45 AM
MarkPosted Jun 26, 2007, 9:57 AM
Have I misunderstood how this works or the way I should be using it?
Many Thanks
Andrew
MarkPosted Jun 26, 2007, 9:41 AM
AlanPosted Jun 26, 2007, 9:33 AM
Well, you could replace the 'if' statement with
Type t = Type.GetType(listboxValue);
where listboxValue contains the name of the type as a string. However, the problem here is that you have to supply it with the fully qualified name of the type or else it won't work:
http://msdn2.microsoft.com/en-us/library/w3f99sx1.aspx
You'd therefore need to save the fully qualified name to the ListBox or code a method which mapped an abbreviated name to the fully qualified name and then use that:
Type t = Type.GetType(GetFullName(listboxValue));