📄 ex-22-07
字号:
// Example 22-07: Late binding of COM objects
private void btnAdd_Click(
object sender, System.EventArgs e)
{
Invoke("Add");
}
private void btnSubtract_Click(
object sender, System.EventArgs e)
{
Invoke("Subtract");
}
private void btnMultiply_Click(
object sender, System.EventArgs e)
{
Invoke("Multiply");
}
private void btnDivide_Click(
object sender, System.EventArgs e)
{
Invoke("Divide");
}
private void Invoke(string whichMethod)
{
Double left, right, result;
left = Double.Parse(textBox1.Text);
right = Double.Parse(textBox2.Text);
// create a Type object to hold type information
Type comCalcType;
// an array for the arguments
object[] inputArguments =
{left, right };
// get the type info from the COM object
comCalcType =
Type.GetTypeFromProgID(
"ComCalculator.ComCalc");
// create an instance
object comCalcObject =
Activator.CreateInstance(comCalcType);
// invoke the method dynamically and
// cast the result to double
result = (Double) comCalcType.InvokeMember(
whichMethod, // the method to invoke
BindingFlags.InvokeMethod, // how to bind
null, // binder
comCalcObject, // the COM object
inputArguments); // the method arguments
label1.Text = result.ToString();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -