1

我想在运行时使用实例化一个类

_syncHelper = (ISyncHelper) Activator.CreateInstance("SBD.Syrius.Synchronisation", "SyncHelper");


A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

System.IO.FileNotFoundException: Could not load file or assembly 

'SBD.Syrius.Synchronisation' or one of its dependencies. The system cannot find the file specified.

File name: 'SBD.Syrius.Synchronisation'
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase,    
Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr   pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)

at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)

at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)

at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(String assemblyName, String typeName)

at SBD.Syrius.DataLayer.DataHelper.get_SyncHelper() in e:\EShared\Syrius6\syrius_syrius\SBD.Syrius.DataLayer\DataHelper.cs:line 35

at SBD.Syrius.DataLayer.Context.SaveChanges() in e:\EShared\Syrius6\syrius_syrius\SBD.Syrius.DataLayer\Context.cs:line 99

at SBD.Syrius.UI.MDIHelper.AttemptToSave(Context Db) in  e:\EShared\Syrius6\syrius_syrius\SBD.Syrius.UI\MDIHelper.cs:line 51

=== Pre-bind state information ===
LOG: User = saturn\kirsten
LOG: DisplayName = SBD.Syrius.Synchronisation
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: SBD.Syrius.Synchronisation | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information 
and common solutions to this issue.
LOG: Appbase = file:///E:/EShared/Syrius6/syrius_syrius/SBD.Syrius.UI/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : SBD.Syrius.DataLayer, Version=1.0.0.0, Culture=neutral,  
PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file:    
E:\EShared\Syrius6\syrius_syrius\SBD.Syrius.UI\bin\Debug\SBD.Syrius.UI.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from    
C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or    
location-based assembly bind).

LOG: Attempting download of new URL          file:///E:/EShared/Syrius6/syrius_syrius/SBD.Syrius.UI/bin/Debug/SBD.Syrius.Synchronisation.DLL.

LOG: Attempting download of new URL   file:///E:/EShared/Syrius6/syrius_syrius/SBD.Syrius.UI/bin/Debug/SBD.Syrius.Synchronisation/SBD.Syrius.Synchronisation.DLL.

LOG: Attempting download of new URL file:///E:/EShared/Syrius6/syrius_syrius/SBD.Syrius.UI/bin/Debug/SBD.Syrius.Synchronisation.EXE.

LOG: Attempting download of new URL file:///E:/EShared/Syrius6/syrius_syrius/SBD.Syrius.UI/bin/Debug/SBD.Syrius.Synchronisation/ SBD.Syrius.Synchronisation.EXE.

我收到的错误包含指向 MSDN here的链接,但我找不到如何为程序集指定文本标识的示例。

我注意到完整的错误表明它正在寻找错误的文件夹以及带有大写 .DLL 的文件。我的文件是用小写的 .dll 创建的

是否有示例说明如何为程序集提供完全指定的文本标识?我应该尝试更改 .dll 扩展名的大小写吗?

如果我实际上将 .dll 复制到 UI\bin\debug 文件夹,那么我会得到一个不同的错误

A first chance exception of type 'System.TypeLoadException' occurred in mscorlib.dll
System.TypeLoadException: Could not load type 'SyncHelper' 
from assembly  

'SBD.Syrius.Synchronisation,版本 = 1.0.0.0,文化 = 中性,PublicKeyToken = null'。

 at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name,    
 Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)


at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)

 at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture,   Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)

at System.Activator.CreateInstance(String assemblyName, String typeName)
at SBD.Syrius.DataLayer.DataHelper.get_SyncHelper() in   

e:\EShared\Syrius6\syrius_syrius\SBD.Syrius.DataLayer\DataHelper.cs:line 35
at SBD.Syrius.DataLayer.Context.SaveChanges() in 

e:\EShared\Syrius6\syrius_syrius\SBD.Syrius.DataLayer\Context.cs:line 99

at SBD.Syrius.UI.MDIHelper.AttemptToSave(Context Db) in   
e:\EShared\Syrius6\syrius_syrius\SBD.Syrius.UI\MDIHelper.cs:line 51
4

1 回答 1

2

发现三个错误:

  1. Activator.CreateInstance需要类型的完整名称来加载它,这意味着您必须将命名空间添加到类型名称。
    您可能在不同的命名空间中有多个具有该名称的类,那么它如何确定加载哪个类。

  2. 您想创建一个不可能的接口实例。
    ISyncHelper是一个界面,你不能说:
    var variable = new ISynchHelper()

    这会导致编译错误,因为接口只是空的主体,不包含单行实现。
    因此,每当您使用反射做某事时,请小心,因为编译器无法帮助您,如果出现问题,您将在运行时而不是在编译时面对它。

  3. 最后一个错误是由于 UI 中没有对 Synchronization 项目的引用,这意味着它不会复制到 UI 项目的 Bin 文件夹。

    您可以通过将同步项目添加到 UI 或只是将复制命令添加到同步项目以在构建后复制来做到这一点。

祝你好运 :-)

于 2013-01-30T10:15:31.717 回答