0

我正在 ASP.Net MVC3 的 web api 项目中处理 T4 模板。我需要从业务层获取类和方法,并使用 .tt 文件创建 cs 文件。我也需要获取只有 [PrivateApi] 标签的方法。

这是我的 t4 模板类。

<# 

Assembly ab =  AppDomain.CurrentDomain.GetAssemblies()
    .Where(b=>b.GetName().Name.Trim().ToLower() == "Empite.Give360.Business".ToLower())
    .FirstOrDefault() as Assembly; 

foreach (var type in ab.GetTypes())
{  
    if (type.Name.EndsWith("Service") && type.IsInterface )
    {

        CreateAPI(type);
        SaveOutput(type.Name + "API.cs");

    }
}          

DeleteOldOutputs();    #>

<#+ public void CreateAPI(Type businessObjType) {#>

公共类 <#= businessObjType.Name.Substring(1) #>API : <#= businessObjType.Name #>API { } 公共接口 <#= businessObjType.Name #>API { } <#+ } #>

这是生成的 CS 文件

public class DonationServiceAPI : IDonationServiceAPI
{

}
public interface IDonationServiceAPI
{
}

这是我需要重现的课程

public class DonationService : IDonationService
{

 private readonly IDonationRepository _donationRepository;
        private readonly IDonationStatusTypeRepository _donationStatusTypeRepository;
        private readonly IPayrollDeductionRepository _payrollDeductionRepository;

        public DonationService() : this(new DonationRepository(),new DonationStatusTypeRepository(),new PayrollDeductionRepository())
        {

        }

        public DonationService(IDonationRepository donationRepository,IDonationStatusTypeRepository donationStatusTypeRepository,IPayrollDeductionRepository payrollDeductionRepository)
        {
            _donationRepository = donationRepository;
            _donationStatusTypeRepository = donationStatusTypeRepository;
            _payrollDeductionRepository = payrollDeductionRepository;
        }

 [PrivateApi]
  public ServiceResponse<Donation> GetDonationByDonationId(int donationId)
        {
            var donationObj = _donationRepository.Get(donationId);
            return new ServiceResponse<Donation>(donationObj);
        }
}

我是 T4 模板的新手,有人知道该怎么做吗?

4

1 回答 1

1

我不能给你完整的答案,但从ST4bby开始,获取一些示例代码来构建。它使用 RDB 作为要创建哪些类的基础,但会循环并向 VS 输入类。

于 2013-08-09T07:09:19.767 回答