转自:http://blog.163.com/kunkun0921@126/blog/static/16920433220127193511096/
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
ResourceDemo
{
public
partial
class
Form1
:
Form
{
public
Form1()
{
InitializeComponent();
//
Reso_1();
Reso_2();
}
///
<summary>
///
调用资源文件中类的方法
///
方法中包含参数、没有返回值
///
</summary>
void
Reso_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",
new
Type[]
{
typeof(string),
typeof(string),
typeof(string)
});
//创建资源文件中类的实例(命名空间+类的方法名称)
obj
=
ass.CreateInstance("PackDLL.FileAndDir.Log.PackApplicaitonLog");
//调用资源文件中类的方法(第一个参数类的实例,第二个参数列表)
mi.Invoke(obj,
new
object[]
{
"app",
"app.txt",
"我定义了异常"
});
}
///
<summary>
///
调用资源文件中类的方法
///
方法中不包含参数、带有返回值
///
</summary>
void
Reso_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();
}
}
}