在Resources.resx资源文件中添加资源后,编译后资源可以嵌入在exe文件中,常见的资源有:图片,音频,文本等等。在程序中通过如下代码即可调用:
Properties.Resources.*****
星号部分就是添加的资源名称,点出来就有。
同样在Resources.resx中,嵌入一个编译好的DLL文件,如db.dll,通过代码Properties.Resources.db,返回类型是byte[],二进制格式。此时,如果想要调用其中的方法,字段,需要对这个二进制数据做处理了。下面是一个简单的方法示例:
/// <summary>
/// 动态调用资源文件
/// </summary>
/// <param name="nameSpace">使用到的命名空间</param>
/// <param name="className">使用到的类名</param>
/// <param name="lpProcName">调用的方法</param>
/// <param name="ObjArray_Parameter">方法的参数数组(如果没有则为null)</param>
/// <returns>如果调用的方法有返回值则返回,如果没有返回null</returns>
public object InvokeMethod(string nameSpace,string className,string lpProcName,object[] ObjArray_Parameter)
{
try
{
Assembly assembly = Assembly.Load(Properties.Resources.db);//加载指定的DLL程序集
Type[] type = assembly.GetTypes();
foreach(Type t in type)
{
if (t.Namespace == nameSpace && t.Name == className)//存在指定的命名空间和类
{
MethodInfo m=t.GetMethod(lpProcName);//加载需要调用的方法
if(m!=null)
{
object _object=Activator.CreateInstance(t);
return m.Invoke(_object, ObjArray_Parameter);//调用指定的方法,并返回结果(如果有)
}
else
System.Windows.Forms.MessageBox.Show("方法:"+lpProcName+" 不存在!");
}
else
System.Windows.Forms.MessageBox.Show("命名空间:" + nameSpace + ",类:" + className+" 不存在!");
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
return null;
}