应用程序RELEASE编译后,需要应用与应用程序在同一目录下不同文件夹下的C#.NET DLL 如何实现?
解决方案 »
- 在WinForm开发中,如何从数据库中检索符合当前设定的时间范围的记录?
- 帮忙写一个小程序
- 向dataset中添加datatable的问题 在线!
- @@@@@@从文件中读出的内容怎样直接帮定到grid中?@@@@@
- 不是很理解CreateMutex的用法。。谁举个例子说明一下。。
- 100分:下面的文件我用LoadXml方法怎么才能读到xmldocument中,很多次都出问题!
- 如何在WinForm DataGrid中实现象True DBGrid中的ValueItems的Translate功能
- c#中关于华表cell的用法(急,在线等待)
- 用C#如何达到当关闭应用程序(窗体)时,弹出MessageBox,然后按取消按钮可以不关闭窗体,回到应用程序
- 22弄3号404室怎么翻译成英文?中文文秘英文怎么说?还有夜大?
- C# 窗体的位子
- javascirpt时间控制的问题
http://hi.baidu.com/winnyang/blog/item/6f30d7da80a893ddb7fd483a.html
这有一个介绍DEBUG和RELEASE的文章LZ看下吧.
可以处理domain的AssemblyResolve事件,这个事件在net不能load引用的assembly时调用。
To tell the .NET runtime where to look for an assembly, follow these steps:1. After adding the reference to the assembly within Visual Studio, set the "Copy Local" property of the Reference to False. This prevents the assembly DLL from being copied to the application’s bin directory.
2. In the Form’s load event, add an event handler for the AssemblyResolve event for the current AppDomain. (It’s easy and requires two lines of code — have a look at the code below.)
3. Create an AssemblyResolve event handler. You may use the implementation as-is from the source code below. private void Form1_Load(object sender, EventArgs e)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve); } Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
//This handler is called only when the common language runtime tries to bind to the assembly and fails.
//Retrieve the list of referenced assemblies in an array of AssemblyName.
Assembly MyAssembly, objExecutingAssemblies;
string strTempAssmbPath = "";
objExecutingAssemblies = Assembly.GetExecutingAssembly();
AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();
//Loop through the array of referenced assembly names.
foreach (AssemblyName strAssmbName in arrReferencedAssmbNames)
{
//Check for the assembly names that have raised the "AssemblyResolve" event.
if (strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
{
//Build the path of the assembly from where it has to be loaded.
//The following line is probably the only line of code in this method you may need to modify:
strTempAssmbPath = "Lib"; //assembly在exe执行目录的子目录Lib中
if (!strTempAssmbPath.EndsWith("\\")) strTempAssmbPath += "\\";
strTempAssmbPath += args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
break;
}
}
//Load the assembly from the specified path.
MyAssembly = Assembly.LoadFrom(strTempAssmbPath);
//Return the loaded assembly.
return MyAssembly;
} private void button1_Click(object sender, EventArgs e)
{
int res = Library.Class1.Add(2, 3); //调用assembly中的代码
MessageBox.Show(res.ToString());
}
我已经测试过了,没有问题。
private void button1_Click(object sender, EventArgs e)
{
int res = Library.Class1.Add(2, 3); //调用assembly中的代码
MessageBox.Show(res.ToString());
}你所指的Library是不是我加载到当前程序集中需要应用的类?
那这样的类怎么进行实例化?
Type tp = asm.GeType("MyType");
object o = Activator.CreateInstance(tp); //创建类实例
MethodInfo mi = tp.GetMethod(...); //取的方法描述
mi.Invoke(o, ...); //调用的对象,以及参数
int res = Library.Class1.Add(2, 3); //调用assembly中的代码
MessageBox.Show(res.ToString()); 我看不明白这个是程序集加载后使用,既然程序集是动态加载的,那么对一个类的实例化应该怎样进行呢?
我测试用的assembly是个很简单的dll,Add是static方法因此不需要实例化。将Assembly Load进去后,你可以随意使用Assembly中的类,当然也可以实例化了。如果Add不是static的话,我就需要先Library.Class1 ins = new Library.Class1(); ins.Add(2,3)了。namespace Library
{
public class Class1
{
public static int Add(int a, int b)
{
return a + b;
}
}
}
Assembly加载后,你对Assembly中的东西可以任意使用。就好像正常使用reference一样。