C#学习教程:加载组件不工作所以我首先编写原文文件:privatebooltestAssemblies(StringsourceName){FileInfosourceFile=newFileInfo(sourceName);CodeDomProvider提供者=null;boolcompileOk=false;//根据输入的文件扩展名选择代码提供者。如果(sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture)==".CS"){provider=CodeDomProvider.CreateProvider("CSharp");}elseif(sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture)==".VB"){provider=CodeDomProvider.CreateProvider("VisualBasic");}else{Console.WriteLine("源文件的扩展名必须是.cs或.vb");}if(provider!=null){//格式化可执行文件名。//使用当前目录//和_cs.exe或_vb.exe构建输出程序集路径。StringexeName=String.Format(@"{0}{1}.exe",System.Environment.CurrentDirectory,sourceFile.Name.Replace(".","_"));编译器参数cp=new编译器参数();//生成可执行文件而不是//类库。cp.GenerateExecutable=true;//指定要生成的程序集文件名。cp.OutputAssembly=exeName;//将程序集保存为物理文件。cp.GenerateInMemory=false;//设置是否将所有警告都视为错误。cp.TreatWarningsAsErrors=false;//调用源文件的编译。CompilerResultscr=provider.CompileAssemblyFromFile(cp,sourceName);if(cr.Errors.Count>0){//显示编译错误。Console.WriteLine("将{0}构建到{1}时出错",sourceName,cr.PathToAssembly);foreach(CompilerErrorceincr.Errors){Console.WriteLine("{0}",ce.ToString());控制台.WriteLine();}}else{//显示编译成功的信息。Console.WriteLine("Source{0}builtinto{1}successfully.",sourceName,cr.PathToAssembly);}//返回编译结果。如果(cr.Errors.Count>0){compileOk=false;}else{compileOk=true;}}返回compileOk;这个很好用,但是如果我以后尝试加载程序集,我总是会遇到异常:System.Reflection.TargetInvocationException未处理Message=Exception已被调用目标抛出。Source=mscorlibStackTrace:在System.RuntimeMethodHandle._InvokeMethodFast(对象目标,Object[]参数,SignatureStruct&sig,MethodAttributesmethodAttributes,RuntimeTypeHandletypeOwner)在System.RuntimeMethodHandle.InvokeMethodFast(对象目标,Object[]参数,Signaturesig,MethodAttributesmethodAttributes,RuntimeTypeHandletypeOwner)在System.Reflection.RuntimeMethodInfo.Invoke(Objectobj,BindingFlagsinvokeAttr,Binderbinder,Object[]parameters,CultureInfoculture,BooleanskipVisibilityChecks)在System.Delegate.DynamicInvokeImpl(Object[]args)在Microsoft.Surface.Shell.ApiEventManager.EventSubscriberInfo.InvokeCallback(Objectarg)在System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegatecallback,Objectargs,BooleanisSingleParameter)在System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Ob项目源、委托回调、对象参数、布尔值isSingleParameter、委托catchHandler)在System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调、对象参数、布尔值isSingleParameter、委托catchHandler)在System.Windows.Threading.DispatcherOperation.InvokeImpl()在System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象状态)在System.Threading.ExecutionContext.runTryCode(对象userData).ExecutionContext.RunInternal(ExecutionContextexecutionContext,ContextCallbackcallback,Objectstate)在System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallbackcallback,Objectstate)在System.Windows.Threading.DispatcherOperation.Invoke()在System.Windows.ThreadingSystem.Windows.Threading中的.Dispatcher.ProcessQueue()。Dispatcher.WndProcHook(IntPtrhwnd,Int32msg,IntPtrwParam,IntPtrlParam,Boolean&handled)在MS.Win32.HwndWrapper.WndProc(IntPtrhwnd,Int32msg,IntPtrwParam,IntPtrlParam,Boolean&handled)在MS.Win32.HwndSubclassDispatcherCallbackOperation(对象o)在System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象参数,布尔值isSingleParameter)在System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源,委托回调,对象参数,布尔值isSingleParameter,委托catchHandler)在System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调,对象参数,布尔值isSingleParameter,委托catchHandler)在System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority优先级,委托方法,对象arg)在MS.Win32.HwndSubclass.SubclassWndProc(IntPtrhwnd,Int32msg,IntPtrwParam,IntPtrlParam)在MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&msg)在System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrameframe)在System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)在System.Windows.Threading.Dispatcher.Run()在System.Windows.Application.RunDispatcher(对象忽略)在System.Windows.Application.RunInternal(窗口窗口)在System.Windows.Application.Run(窗口窗口)在C:UsersRoflcoptrDocumentsVisualStudio2008ProjectsPrototype_Ver1Prototype_Ver1objDebugApp.g.cs中的Prototype_Ver1.App.Main()中的System.Windows.Application.Run():System.AppDomain中的第0行。_nExecuteAssembly(Assembly程序集,String[]args)在System.AppDomain.ExecuteAssembly(StringassemblyFile,EvidenceassemblySecurity,String[]args)在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()在System.Threading.ThreadHelper.ThreadStart_Context(Ob项目状态)在System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback回调,对象状态)在System.Threading.ThreadHelper.ThreadStart()InnerException:System.IO.FileLoadException消息=无法加载文件或程序集'C:\Users\Roflcoptr\Desktop\hello.cs'或其依赖项之一。给定的程序集名称或代码库无效。(HRESULT异常:0x80131047)Source=mscorlibFileName=C:\Users\Roflcoptr\Desktop\hello.csFusionLog=""StackTrace:atSystem.Reflection.AssemblyName.nInit(Assembly&assembly,BooleanforIntrospection,BooleanraiseResolveEvent)atSystem.Reflection.Assembly.InternalLoad(StringassemblyString,EvidenceassemblySecurity,StackCrawlMark&stackMark,BooleanforIntrospection)在System.Reflection.Assembly.Load(StringassemblyString)在Prototype_Ver1.SurfaceWindow1.OnApplicationActivated(Objectsender,EventArgse)在C:UsersRoflcoptrDocumentsVisualStudio2008ProjectsPrototype_Ver1Prototype_Ver1MainWindow.xaml.cs:line91InnerException:我的加载方法如下所示:if(testAssemblies("C:\Users\Roflcoptr\Desktop\hello.cs")){\Roflcoptr\Documents\VisualStudio2008\Projects\Prototype_Ver1\Prototype_Ver1\bin\Debug\hello_cs.exe");任何想法为什么它不起作用?我认为您的语法有误Assembly.load(string)需要程序集名称Assembly.Load("SampleAssembly,Version=1.0.2004.0,Culture=neutral,PublicKeyToken=8744b20f8da049e3");还要确保您使用正确的重载来满足您的需要(通常是Assembly.Load)http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx编辑使用这个:以上就是C#学习教程:加载程序集不能正常工作分享的所有内容,如果对你有用,需要进一步了解C#学习教程,希望大家多多关注—AssemblyNamean=AssemblyName.GetAssemblyName(文件路径);组装.加载(一个);本文来自网络收藏,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:
