T4 引用的组装块构建 [英] T4 referenced assembly blocks build

查看:19
本文介绍了T4 引用的组装块构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Visual Studio 2010 中,我有以下项目布局:

In Visual Studio 2010 I have the following project layout:

  • 解决方案
    • 项目 A
      • C 类
      • D级
      • T4 模板

      T4 模板包含这样的程序集引用:

      The T4 template contains a assembly reference like this:

      <#@ assembly name="$(SolutionDir)\A\bin\Debug\A.dll" #>
      

      模板实例化类 C 的一个实例.当我运行 T4 模板时,处理器加载项目 A 的 dll 并正确创建输出.当我想更改项目 A 中的某些内容时出现错误,例如修改类 C 或 D.

      The template instantiates an instance of class C. When I run the T4 template the processor loads the project A's dll and correctly creates the output. The error arises when I want to change something in project A, say modify either class C or D.

      无法复制文件obj\Debug\A.dll"到bin\Debug\A.dll".过程无法访问文件'bin\Debug\A.dll' 因为它正在被另一个进程使用.

      Unable to copy file "obj\Debug\A.dll" to "bin\Debug\A.dll". The process cannot access the file 'bin\Debug\A.dll' because it is being used by another process.

      我发现摆脱此错误的唯一方法是重新启动 Visual Studio.有没有其他方法可以强制从 VS 卸载 A.dll 程序集?

      The only way I found to get rid of this error is to restart Visual Studio. Is there any other way to force the unloading of the A.dll assembly from VS?

      推荐答案

      我使用 VS2010 SP1,在 POST-BUILD 事件期间运行自定义 T4 模板时,在第一次构建后仍然被阻止同一个项目.

      Im using VS2010 SP1 and was still getting blocked during build after first build when running a custom T4 template during the POST-BUILD events which accessed instances of classes of the same project.

      我的工作原理是使用反射从 Project dll 访问类.

      How I got it to work was to use Reflection to access classes from the Project dll.

      直接从文件加载 dll 时,我仍然遇到阻塞问题.

      I still got the blocking issue when loading the dll directly from the file.

      注意:诀窍是将 .dll 作为字节数组加载到内存中,然后从原始字节数组加载程序集.不要使用 Assembly.LoadFrom

      NOTE: The trick was to load the .dll into memory as a byte array and then load the assembly from the raw byte array. DONT load from the file using the Assembly.LoadFrom

      此代码来自我的 T4 模板文件,正在访问静态类信息"并调用静态方法版本"以返回字符串值.

      This code is from my T4 template file and is accessing a static class "Information" and calling a static Method "Version" to return a string value.

      string assemblyPath = Path.Combine(projectPath, @"bin\SampleProject.dll");
      byte[] data;
      
      using (var fs = File.OpenRead(assemblyPath))
      {
          data = new byte[fs.Length];
          fs.Read(data, 0, Convert.ToInt32(fs.Length));
      }
      
      if (data == null || data.Length == 0)
      {
          throw new ApplicationException("Failed to load " + assemblyPath);
      }
      
      var asm = Assembly.Load(data);
      appVersion = (string) asm.GetType("SampleProject.Information").GetField("Version").GetValue(null);
      

      这篇关于T4 引用的组装块构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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