程序集未加载C# [英] Assembly not loaded c#

查看:127
本文介绍了程序集未加载C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目Common,其中包含一个log4net CustomAppender.我在所有其他项目中都引用了该项目,并在app.config中配置了log4net附加程序.除了一个项目在尝试实例化Appender时失败之外,其他所有事情都运行顺利.

I have a project Common which contains a log4net CustomAppender. I reference the project in all my other projects and configure log4net appender in app.config. Everything works smooth except for one project which fails when trying to instantiate the Appender.

输出显示以下错误:


System.TypeLoadException: Could not load type [Common.Appenders.MyCustomAppender].
Tried assembly [log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a]
and all loaded assemblies
at log4net.Util.SystemInfo.GetTypeFromString(Assembly relativeAssembly, String typeName, Boolean throwOnError, Boolean ignoreCase)
at log4net.Util.SystemInfo.GetTypeFromString(String typeName, Boolean throwOnError, Boolean ignoreCase)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement)
log4net:ERROR Appender named [MyCustomAppender] not found.

所有项目的log4net配置均相同. app.config包含:

The log4net configuration is the same for all project. app.config contains:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="MyCustomAppender" type="Common.Appenders.MyCustomAppender">
      <file value="log.txt" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="MyCustomAppender" />
    </root>
  </log4net>
</configuration>

从代码中我调用log4net.Config.XmlConfigurator.Configure().如果我在调用log4net配置非工作项目之前使用Assembly.Load("Common")手动加载程序集,那么一切都很好.

From code I call log4net.Config.XmlConfigurator.Configure(). If I manually load the assembly using Assembly.Load("Common") before calling log4net configure for the non-working project everything is fine.

有人知道为什么通过反射实例化MyCustomAppender时不加载程序集吗?我该如何解决?

Any idea why the assembly is not loaded when MyCustomAppender is instantiated via reflection? How can I solve this?

推荐答案

尝试将程序集的名称放在类型名称之后

Try putting the name of the assembly after the name of the type

<appender name="MyCustomAppender" type="Common.Appenders.MyCustomAppender,Common">

请记住,程序集的名称不必与它的默认名称空间相同,也不必与Visual Studio中的项目名称相同. 此处的相关名称是输出名称(即生成的dll的文件名)

Keep in mind that the name of an assembly is not necessarily the same as it's default namespace nor the project name in Visual Studio. The relevant name here is the output name (which is the file name of the resulting dll)

这篇关于程序集未加载C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆