标签:反射 CalDLL int ICalculator System 接口 计算器 using Calculator
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using Ical;//引入接口类库
namespace Reflection1
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void btnReflection_Click(object sender, EventArgs e)
{
//ICalculator objCal = new CalDLL.Calculator();
//动态加载程序集并创建对象
//ICalculator objCal =
// (ICalculator)Assembly.LoadFrom("CalDLL.dll").CreateInstance("CalDLL.Calculator"); //类的程序集下面的DLL. 只需要在命名空间中引用接口库Ical,而不需要引用类库CalDLL. 通过字符串可以找到CalDLL.Calculator
ICalculator objCal =
(ICalculator)Assembly.Load("CalDLL").CreateInstance("CalDLL.Calculator"); //类的程序集
//通过接口运算完成
int result = objCal.Add(Convert.ToInt32(this.txtNum1.Text.Trim()), Convert.ToInt32(this.txtNum2.Text.Trim()));
this.txtResult.Text = result.ToString();
}
}
}
新建一个类库CalDLL, 下面包含了类 Calculator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalDLL
{
public class Calculator : Ical.ICalculator
{
public int Add(int a, int b)
{
return a + b;
}
public int Sub(int a, int b)
{
return a - b;
}
}
}
新建一个接口类库Ical, 下面包含了 ICalculator 接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ical
{
public interface ICalculator
{
int Add(int a ,int b);
int Sub(int a, int b);
}
}
标签:反射,CalDLL,int,ICalculator,System,接口,计算器,using,Calculator 来源: https://blog.csdn.net/helldoger/article/details/121802922
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。