有关AppDomain.Load方法

antoniusguo 2006-06-05 04:16:40
System.AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = @"C:\MyAssemblies\";
setup.PrivateBinPath = System.AppDomain.CurrentDomain.BaseDirectory;
System.AppDomain domain = System.AppDomain.CreateDomain("MyDomian", null, setup);
domain.Load("Test.MyDll");

文件路径是
C:\MyAssemblies\MyDll.dll
类名为
Test.MyDll
运行到
domain.Load("Test.MyDll");
这里到这里抛出
System.IO.FileNotFoundException
具体这个怎么使用不是很清楚,以前用的是Assembly.LoadFile方法
...全文
204 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
kokubo_wing 2006-06-05
  • 打赏
  • 举报
回复
学习
linaren 2006-06-05
  • 打赏
  • 举报
回复
using System;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;

class Test {
public static void Main() {
AppDomain currentDomain = AppDomain.CurrentDomain;

InstantiateMyType(currentDomain); // Failed!

currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolver);

InstantiateMyType(currentDomain); // OK!
}

static void InstantiateMyType(AppDomain domain) {
try {
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}

// Loads the content of a file to a byte array.
static byte[] loadFile(string filename) {
FileStream fs = new FileStream(filename, FileMode.Open);
byte[] buffer = new byte[(int) fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();

return buffer;
}

static Assembly MyResolver(object sender, ResolveEventArgs args) {
AppDomain domain = (AppDomain) sender;

// Once the files are generated, this call is
// actually no longer necessary.
EmitAssembly(domain);

byte[] rawAssembly = loadFile("temp.dll");
byte[] rawSymbolStore = loadFile("temp.pdb");
Assembly assembly = domain.Load(rawAssembly, rawSymbolStore);

return assembly;
}

// Creates a dynamic assembly with symbol information
// and saves them to temp.dll and temp.pdb
static void EmitAssembly(AppDomain domain) {
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyAssembly";

AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule", "temp.dll", true);
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public);

ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("MyType instantiated!");
ilGenerator.Emit(OpCodes.Ret);

typeBuilder.CreateType();

assemblyBuilder.Save("temp.dll");
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 第三方公司插件 { using 记事本公司提供的插件标准; public class PlugOne:IPlug { public string PlugName { get { return "全部变成大写"; } } public string ProcessText(string strOri) { return strOri.ToUpper(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 第三方公司插件 { using 记事本公司提供的插件标准; public class PlugTwo:IPlug { public string PlugName { get { return "全部变成小写"; } } public string ProcessText(string strOri) { return strOri.ToLower(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 记事本公司提供的插件标准 { public interface IPlug { string ProcessText(string strOri); string PlugName { get; } } } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 记事本主程序 { using System.Reflection; using System.IO; using 记事本公司提供的插件标准; public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //获取正在运行的程序集 Assembly ass = Assembly.GetExecutingAssembly(); //获取程序集的4种方法 //Assembly ass1 = this.GetType().Assembly; //Assembly[] asses = AppDomain.CurrentDomain.GetAssemblies();

110,526

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧