Monday, April 23, 2007

Delegates, Reflections, INVOKE

Delegates:
-Similar to pointers in C++.
-Has something to do with Callback functions.
-Generic delegate copy is created at the run-time.

Reflections:
-Enables us to query the namespaces, methods...

foreach statement:
-Quite useful when there is an array of strings, or an array of ints, or an array of objects to talk about. Simply, an array of something.

Below program section makes use of the Reflections and foreach statement and used to query the methods in a .dll file. The methods are added to the listbox in the Windows form.

Notice the return types of below methods: They are arrays.
Type[] Assembly.GetTypes();
MethodInfo[] Type.GetMethods();

private void Discover_button_Click_1(object sender, EventArgs e)
{
System.Reflection.Assembly cx = System.Reflection.Assembly.LoadFile(textBox1.Text);

foreach (Type t in cx.GetTypes())
{

foreach( System.Reflection.MethodInfo mi in t.GetMethods())
{
listBox1.Items.Add(mi.Name);
}

}

}

Use the code below to easily select the .dll file you need. Don't forget to add a OpenFileDialog and an Open File button to your design.

private void OpenFile_button_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
string filename = openFileDialog1.FileName;
textBox1.Text = filename;
}

BONUS QUESTION:
-We have queried the methods in our CalculateInterest Class Library. The query produced two methods: CalculateSimpleInterest() & CalculateCompoundInterest().
-Now, how can we actually run the CalculateSimpleInterest() method and learn the interest for a given principal, rate and time?

We have to INVOKE this function to achieve the above purpose.
Research Activator.Getobject() method!

1 comment:

Unknown said...

thanks for the opendialog!!