Assembly.LoadFromAssembly SampleAssembly; SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll"); // Obtain a reference to a method known to exist in assembly. MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("Method1"); // Obtain a reference to the parameters collection of the MethodInfo instance. ParameterInfo[] Params = Method.GetParameters(); // Display information about method parameters. // Param = sParam1 // Type = System.String // Position = 0 // Optional=False foreach (ParameterInfo Param in Params) { Console.WriteLine("Param=" + Param.Name.ToString()); Console.WriteLine(" Type=" + Param.ParameterType.ToString()); Console.WriteLine(" Position=" + Param.Position.ToString()); Console.WriteLine(" Optional=" + Param.IsOptional.ToString()); }
有三种方法解决这个问题 Method 1: Install the assembly in the global assembly cache (GAC) Method 2: Use an application configuration (.config) file with the <codeBase> tags <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="MyAssembly2" culture="neutral" publicKeyToken="307041694a995978"/> <codeBase version="1.0.1524.23149" href="FILE://C:/Myassemblies/MyAssembly2.dll"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration>Method 3: Use the AssemblyResolve event,net runtime在捆定assembly的时候,如果在本目录找不到,则调用这个函数,给开发者机会从其它地方载入 private Assembly MyResolveEventHandler(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. strTempAssmbPath="C:\\Myassemblies\\"+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; } 详细步骤见: How to load an assembly at runtime that is located in a folder that is not the bin folder of the application http://support.microsoft.com/kb/837908
SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
// Obtain a reference to a method known to exist in assembly.
MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("Method1");
// Obtain a reference to the parameters collection of the MethodInfo instance.
ParameterInfo[] Params = Method.GetParameters();
// Display information about method parameters.
// Param = sParam1
// Type = System.String
// Position = 0
// Optional=False
foreach (ParameterInfo Param in Params)
{
Console.WriteLine("Param=" + Param.Name.ToString());
Console.WriteLine(" Type=" + Param.ParameterType.ToString());
Console.WriteLine(" Position=" + Param.Position.ToString());
Console.WriteLine(" Optional=" + Param.IsOptional.ToString());
}
这样写行不行呢?
Method 1: Install the assembly in the global assembly cache (GAC)
Method 2: Use an application configuration (.config) file with the <codeBase> tags
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="MyAssembly2" culture="neutral" publicKeyToken="307041694a995978"/>
<codeBase version="1.0.1524.23149" href="FILE://C:/Myassemblies/MyAssembly2.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>Method 3: Use the AssemblyResolve event,net runtime在捆定assembly的时候,如果在本目录找不到,则调用这个函数,给开发者机会从其它地方载入
private Assembly MyResolveEventHandler(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.
strTempAssmbPath="C:\\Myassemblies\\"+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;
}
详细步骤见:
How to load an assembly at runtime that is located in a folder that is not the bin folder of the application
http://support.microsoft.com/kb/837908
把用的东西放一起反倒乱了?
我再说一次我8L的方法。
如果你的某一目录下有A,B两个文件夹,你目前在A文件夹下,那么你在地址栏键入“..\B”就可以进入同一级目录下的B目录了。
..\表示返回上一级目录
A项目是主程序,B项目是程序集。
A项目引用了B项目,编译生成后A.exe和 b.dll默认就同在Debug目录。
由于我有多个exe,多个dll,所以我想把exe放到自己建的一个bin目录。dll都放到自己建的lib目录。并保证exe运行时能访问到lib目录中的dll
A项目是主程序,B项目是程序集。
A项目引用了B项目,编译生成后A.exe和 b.dll默认就同在Debug目录。
由于我有多个exe,多个dll,所以我想把exe放到自己建的一个bin目录。dll都放到自己建的lib目录。并保证exe运行时能访问到lib目录中的dll
DLL都在?:\XXX\XX\LIB(PS:这些问号,XX都替换成同样的值)
如果EXE想调用DLL,那么用相对路径访问
"..\lib\xx.dll"
这样就从bin目录下进入了lib目录下访问到了需要的dll
添加了一个checkedListBox,捕获到路径文件之后显示到checkedListBox以下是部分代码
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(@"..\..\..\");
FileInfo[] fi = di.GetFiles("*.*");
checkedListBox1.Items.AddRange(fi);
}结果checkedListBox显示出来两个项“临时项目.sln”和“临时项目.suo”与实际相符。
(..\..\..\表示向上返回三层目录,即运行的时候是在VS项目文件夹下的“临时项目\临时项目\bin\debug”
那么执行“..\..\..\”之后,向上返回三层目录,即VS项目文件夹下的“临时项目\”,捕获到了该目录下的两个文件,“临时项目.sln”和“临时项目.suo”)
那么你的程序编写的时候用相对路径的DLL就行了。
挂载DLL的这一部分代码的DLL路径这么写“..\lib\a.dll”就行了,这样无论用户把程序安装到哪个目录都无所谓了,这个就是相对路径的好处
你这是运行时加载程序集,这样写那我每个工程都得拆开,全都在主程序运行时加载,否则我编译都通不过。vs2005 中是一个解决方案里面有多个工程,每个工程可以相互引用,默认引用,编译后他们都处于同一级目录。