c# - 如何使用来自 t4 模板的 app.config 中的自定义部分

标签 c# .net visual-studio-2010 .net-4.0 t4

我试图从 VS2010 中的 t4 模板访问 app.config 文件中的自定义部分,但无法加载定义自定义部分的程序集。

我正在使用 ConfigurationAccessor 获取该部分(引用 http://skysanders.net/subtext/archive/2010/01/23/accessing-app.configweb.config-from-t4-template.aspx)。

应用程序配置:

<configSections>
    <section name="MyProviders" type="System.Web.Security.MySection, MyAssembly" />
</configSections>

<MyProviders default="SQLMyProvider">
  <providers>
    <add name="SQLMyProvider" ... connectionStringName="MyConnectionString" />
  </providers>
</MyProviders>

在 .tt 文件中调用这一行:

MySection section = (MySection)config.Configuration.GetSection("MyProviders");

给出这个错误:

Running transformation: System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for MyProviders: Could not load file or assembly 'MyAssembly' or one of its dependencies. The system cannot find the file specified.

.tt 文件引用程序集,项目也是如此,但这似乎无助于加载配置部分。 我已尝试将 MySection 类实现为 tt 代码块,但也无法在 app.config 中使用该类。

有什么想法吗?

最佳答案

我认为<configSections>需要完整的程序集名称(版本、文化、公钥 token )

type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"

关于c# - 如何使用来自 t4 模板的 app.config 中的自定义部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4792388/

相关文章:

.net - 用于 Windows Server 事务/错误处理的服务总线 1.0

c# - 如何从 C# 调用 CORBA 接口(interface)?

c++ - DirectX 10 绘制 .PNG

c# - 如何识别 Page_Load 中的回发事件

c# - ASP.NET Mvc - System.Web.Compilation.CompilationLock

c# - 读取 Dictionary<string, object> 中的值

c# - 如何通过子属性选择对象

c# - 等待进程完成然后显示消息 (C#)

c# - 自定义控件上的单击事件未触发(从包含表单分配的事件)

.net - 在内部网络上发现 Microsoft SQL Server