我正在研究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(); #>
&lt;#+ public void CreateAPI(输入businessObjType){#&gt;
public class&lt;#= businessObjType.Name.Substring(1)#&gt; API:&lt;#= businessObjType.Name#&gt; API { } public interface&lt;#= businessObjType.Name#&gt; API {}&lt;#+}#&gt;
这是生成的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模板的新手,有谁知道怎么做?