Thank you.
Seems I've asked a foolish question, haha.
I am studying C# now, and I just want to write something to
test the reflection.
I want to write a small program with plug-in support.
Here's part of the code:
/*The variable "myPluginInfo" follow is a List<of structure PluginInfo
struct PluginInfo
{
string path;
public PluginInfo(string path)
{
this.path=path;
}
public string Path
{
get
{
return path;
}
}
}
*/
//all the code follow is in ListBox1.MouseDoubleClick event handler:
Assembly tmpPlugin =
Assembly.Load(myPluginInfo[listBox1.SelectedIndex].Path);
//go through the assembly and find the "EntryPoint" class
foreach (Type tmptype in tmpPlugin.GetTypes())
{
if (tmptype.Name == "EntryPoint")
{
//go through the class again and find our Main
method.
ConstructorInfo tmpcoutinfo =
typeof(tmptype).GetConstructor(BindingFlags.Public | BindingFlags.Static,
null, Type.EmptyTypes, null);
try
{
if (tmpcoutinfo != null)
{
tmpcoutinfo.Invoke(new object[0]);
myevent.AddEventHandler(tmptype, new
EventHandler(Plugin_Exit));
#warning Some exception handler should
be add here.
}
else
{
throw new ApplicationException("No such
method!");
}
}
catch (ApplicationException m)
{
if (m.Message == "No such method!")
{
MessageBox.Show("No such Method!");
break;
}
//and if we can't handle it, just pass it.
throw new ApplicationException("Unknown
excetpion", m);
}
}
}
And by the way, is this (I mean the Reflection) the usual way when we want
to write a program with plug-in support?
Thank you very mush for your help.
"Jon Skeet [C# MVP]" <sk***@pobox.com>
??????:MP********************@msnews.microsoft.com ...
David zha0 <zh*****@163.comwrote:
>I found these doesn't work:
private void button1_Click(object sender, EventArgs e)
{
string[] tmpstr = new string[] { "david", "bill", "mike" };
foreach (string tmp in tmpstr)
{
listBox1.Items.Add(typeof(tmp));//here raise an error
}
}
It says "cannot found class or namespace 'tmp'", I think it is because
the
compiler cannot ensure the variable is "null".
No, it's because the typeof operator should have a type name in
brackets - typeof(int) for example - not an expression like a variable.
What exactly are you trying to do here anyway?
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too