在线工具 在线编程 在线白板 在线工具 在线编程 在线白板

C#中如何调用 DLL文件中的资源文件,取的资

是这样的,想请说下,C#中如何调用 DLL文件中的资源文件,取的资
最新回答
指尖星光在流浪

2025-03-01 05:55:07

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;

namespaceResourceDemo
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();

//
Reso_1();
Reso_2();
}

///
<summary>
///
调用资源文件中类的方法
///
方法中包含参数、没有返回值
///
</summary>
voidReso_1()
{
//资源文件中的类
object
obj;
//资源文件中的DLL
System.Reflection.Assembly
ass =System.Reflection.Assembly.Load(ResourceDemo.Properties.Resources.PackDLL);
//获取DLL中的类型(命名空间+类的方法名称)
Type
type =
ass.GetType("PackDLL.FileAndDir.Log.PackApplicaitonLog");
//获取类中的方法(第一个参数类的方法名称,第二个参数是)
System.Reflection.MethodInfo
mi =
type.GetMethod("WriteLog",newType[]{typeof(string),typeof(string),typeof(string)});
//创建资源文件中类的实例(命名空间+类的方法名称)
obj=
ass.CreateInstance("PackDLL.FileAndDir.Log.PackApplicaitonLog");
//调用资源文件中类的方法(第一个参数类的实例,第二个参数列表)
mi.Invoke(obj,newobject[]{"app","app.txt","我定义了异常"});

}

///
<summary>
///
调用资源文件中类的方法
///
方法中不包含参数、带有返回值
///
</summary>
voidReso_2()
{
//PackDLL.Local.Localhost.PackComputerPhysics.GetCpuID
//资源文件中的类
object
obj;
//资源文件中的DLL
System.Reflection.Assembly
ass =System.Reflection.Assembly.Load(ResourceDemo.Properties.Resources.PackDLL);
//获取DLL中的类型(命名空间+类的方法名称)
Type
type =
ass.GetType("PackDLL.Local.Localhost.PackComputerPhysics");
//获取类中的方法(第一个参数类的方法名称,第二个参数是)
System.Reflection.MethodInfo
mi =
type.GetMethod("GetCpuID");
//创建资源文件中类的实例(命名空间+类的方法名称)
obj=
ass.CreateInstance("PackDLL.Local.Localhost.PackComputerPhysics");
//调用方法,如果方法中不包含参数,invoke第二个参数列表可以用null代替
string
cpuId =
mi.Invoke(obj,null).ToString();
}
}
}