面向方面的编程是一个非常诱人的概念,用于简化代码库,生成干净的代码并最大程度地减少复制粘贴错误。
如今,在大多数情况下,方面是在字节码级别实现的,即编译后,某些工具在所需逻辑的支持下“交织”附加字节代码。
我们的方法(以及某些其他工具的方法)是修改源代码以实现方面逻辑。随着向Roslyn技术的过渡,很容易做到这一点,并且其结果比字节码本身的修改具有某些优势。
如果您对详细信息感兴趣,请参见cat。
您可能会认为面向方面的编程与您无关,并不是特别关心您,只是一堆难以理解的词,但实际上它比看起来容易得多,这是关于实际产品开发的问题,如果您从事工业开发,那么您肯定可以从中受益。
特别是在公司级别的中型项目中,对产品功能的要求已正式化。例如,可能有一个要求-设置配置标志时,记录所有公共方法的所有输入参数。或者,对于所有项目方法,都有一个通知系统,当超过该方法的执行时间的特定阈值时,该系统将发送一条消息。
没有AOP怎么办?要么只是锤击并完成最重要的部分,要么是在编写新方法时,从相邻方法以及所有随附方法中复制粘贴类似代码。
使用AOP时,一旦编写建议,该建议就会应用于项目并完成工作。当您需要稍微更新逻辑时,将再次更新建议一次,它将在下一个构建中应用。如果没有AOP,则整个项目代码中的更新数为100,500。
优点是您的代码不再看起来像患有天花的人,因为它散布着这种功能,并且在阅读代码时看起来像令人讨厌的噪音。
在将AOP引入项目后,如果没有它,您将开始实现您梦never以求的事情,因为它们看起来像是一个相对较小的优势,而且成本很高。使用AOP,一切都是相反的,相对较低的成本和巨大的收益(对于您类似的工作成本水平)。
我的感觉是,与Java生态系统相比,面向方面的编程在.Net生态系统中的普及率明显较低。我认为主要原因是缺乏可与Java的功能和质量相媲美的免费和开源工具。
PostSharp提供了类似的功能和便利性,但是没有多少人愿意花数百美元在其项目中使用它,并且社区版本的功能非常有限。当然,还有其他选择,但不幸的是,它们还没有达到PostSharp的水平。
您可以比较工具的功能(必须牢记,比较是由PostSharp的所有者进行的,但是给出了一些图片)。
我们面向方面的编程之路
我们是一家小型咨询公司(可容纳12人),我们的工作最终结果是源代码。那些。我们获得报酬以创建源代码,质量代码。我们仅在一个行业中工作,我们的许多项目都有非常相似的要求,因此,这些项目之间的源代码也非常相似。
而且由于我们资源有限,所以对我们来说,最重要的任务之一是能够重用代码和使用使开发人员从例行任务中省下来的工具。
为此,一种方法是我们大量使用自动代码生成功能,并且还为Visual Studio创建了一些自定义插件和分析器,这些插件和分析器特定于我们的项目和任务。这使得可以显着提高程序员的生产率,同时保持代码的高质量(甚至有人说质量已经提高了)。
下一个合乎逻辑的步骤是实现面向方面编程的使用的想法。我们尝试了几种方法和工具,但结果远没有达到我们的预期。这与Roslyn技术的发布恰逢其时,并且在某个时刻,我们有了将自动代码生成和Roslyn的功能相结合的想法。
在短短的几周内,就创建了该仪器的原型,并且根据我们的感觉,这种方法似乎更有希望。在反复使用和更新此工具的方式之后,我们可以说达到了我们的期望,甚至超出了我们的期望。我们已经开发了一个有用的模板库,并在我们的大多数项目中使用了这种方法,并且我们的一些客户也使用了它,甚至根据自己的需要订购模板。
不幸的是,我们的工具还远远不够理想,因此我想将描述分为两部分,第一部分是我如何在理想的世界中看到此功能的实现,第二部分是如何在此实现。
在开始详细介绍之前,我想作一个小小的解释-本文中的所有示例都已简化为一个层次,可以让您展示出这个想法,而又不会过多地涉及无关的细节。
在完美的世界中将如何做到
在使用我们的工具几年后,我对如果我们生活在理想的世界中,我希望它如何工作感到vision憬。
在理想世界的视野中,语言规范允许使用源代码转换,并提供编译器和IDE支持。
这个想法是受C#语言规范中包含“部分”修饰符启发的。这个相当简单的概念(在多个文件中定义类,结构或接口的能力)极大地改善和简化了自动源代码生成工具的支持。那些。它是一种类的源代码在几个文件之间的水平拆分。对于那些不懂C#语言的人,举一个小例子。
假设我们在文件Example1.aspx中描述了一个简单的形式
<%@ Page Language="C#" AutoEventWireup="True" %>
// . . .
<asp:Button id="btnSubmit"
Text="Submit"
OnClick=" btnSubmit_Click"
runat="server"/>
// . . .
以及Example1.aspx.cs文件中的自定义逻辑(例如,单击按钮时将其颜色更改为红色)
public partial class ExamplePage1 : System.Web.UI.Page, IMyInterface
{
protected void btnSubmit_Click(Object sender, EventArgs e)
{
btnSubmit.Color = Color.Red;
}
}
语言中“部分”提供的功能的存在使该工具包能够解析Example1.aspx文件并自动生成Example1.aspx.designer.cs文件。
public partial class ExamplePage1 : System.Web.UI.Page
{
protected global::System.Web.UI.WebControls.Button btnSubmit;
}
那些。我们可以由可更新的程序员(Example1.aspx.cs)将ExamplePage1类的一部分代码存储在一个文件中,而由自动生成的工具包将Example1.aspx.designer.cs文件中的一部分存储在一个文件中。最后,对于编译器,它看起来像一个通用类
public class ExamplePage1 : System.Web.UI.Page, IMyInterface
{
protected global::System.Web.UI.WebControls.Button btnSubmit;
protected void btnSubmit_Click(Object sender, EventArgs e)
{
btnSubmit.Color = Color.Red;
}
}
使用带有IMyInterface接口的继承定义的示例,您可以看到最终结果是来自不同文件的类定义的组合。
如果我们没有部分功能,并且编译器只需要将所有类代码存储在一个文件中,那么我们就可以假设不便之处以及支持自动生成所需的其他手势。
因此,我的想法是在语言规范中包括两个附加的修饰符,这将使将方面嵌入源代码更加容易。
第一个修饰符是原始的,我们将其添加到应该可以转换的类定义中。
第二个被处理,它象征着这是源转换工具获得的最终类定义,编译器必须接受它才能生成字节码。
顺序是这样的
- 用户使用包含.cs文件中原始修饰符的类的源代码(例如Example1.cs)
- 编译时,编译器检查源代码的正确性,如果该类已成功编译,则检查原始代码是否存在。
- 如果存在原始文件,则编译器会将此文件的源代码提供给转换过程(这是编译器的黑匣子)。
- .processed.cs .processed.cs.map ( .cs .processed.cs, IDE)
- .processed.cs ( Example1.processed.cs) .
- ,
a. original processed
b. .cs .processed.cs - , .processed.cs .
那些。通过添加这两个修饰符,我们能够在语言级别上组织对源代码转换工具的支持,就像部分允许我们简化对源代码生成的支持一样。那些。 parial是水平代码拆分,原始/已处理是垂直。
如我所见,对于Microsoft的两名实习生而言,在编译器中实现原始/已处理的支持是一周的工作(当然是在开玩笑,但这与事实相差不远)。总体而言,此任务没有根本的困难,从编译器的角度来看,这是文件操作和过程调用。
.NET 5中添加了新功能-源代码生成器这已经允许您在编译过程中生成新的源代码文件,这是朝着正确方向发展的一步。不幸的是,它仅允许您生成新的源代码,而不能修改现有的源代码。所以我们还在等。
类似过程的一个例子。用户创建文件Example2.cs
public original class ExamplePage2 : System.Web.UI.Page, IMyInterface
{
protected global::System.Web.UI.WebControls.Button btnSubmit;
protected void btnSubmit_Click(Object sender, EventArgs e)
{
btnSubmit.Color = Color.Red;
}
}
运行编译,如果一切正常,并且编译器看到了原始修饰符,则它将源代码提供给转换过程,该转换过程将生成Example2.processed.cs文件(在最简单的情况下,它可能只是Example2.cs的精确副本,而原始文件已被处理文件替换) ...
在我们的例子中,我们假设转换过程添加了一个日志记录方面,结果如下所示:
public processed class ExamplePage2 : System.Web.UI.Page, IMyInterface
{
protected global::System.Web.UI.WebControls.Button btnSubmit;
protected void btnSubmit_Click(Object sender, EventArgs e)
{
try
{
btnSubmit.Color = Color.Red;
}
catch(Exception ex)
{
ErrorLog(ex);
throw;
}
SuccessLog();
}
private static processed ErrorLog(Exception ex)
{
// some error logic here
}
private static processed SuccessLog([System.Runtime.CompilerServices.CallerMemberName] string memberName = "")
{
// some success logic here
}
}
下一步是验证签名。 _main_签名相同,并且满足以下条件:原始和处理后的定义必须完全相同。
在此示例中,我特别添加了一个小句子,这是方法,属性和字段的经过处理的修饰符。
它将方法,属性和字段标记为仅适用于具有已处理修饰符的类,并且在比较签名时将被忽略。这样做是为了方便方面开发人员,并允许您将常规逻辑移至单独的方法中,以免造成不必要的代码冗余。
编译器编译了此代码,如果一切正常,则使用字节码继续该过程。
显然,在此示例中有一些简化,实际上,逻辑可能更复杂(例如,当我们为同一类同时包含原始和部分逻辑时),但这并不是无法克服的复杂性。
完美世界中的基本IDE功能
在IDE中使用.processed.cs文件的源代码的支持主要在于逐步调试过程中原始/已处理类与转换之间的正确导航。
从我的角度来看,IDE的第二个最重要的功能是帮助读取已处理类的代码。一个Processed类可以包含许多由多个方面添加的代码。在我们看来,与图形编辑器中的图层概念相似的显示实现似乎是实现此目标的最方便的选择。我们当前的插件实现了类似的功能,并且其用户的反馈是非常积极的。
重构功能是有助于将AOP引入日常生活的另一个功能。用户突出显示代码的一部分,可以说“提取到AOP模板”,IDE将创建正确的文件,生成初始代码,并在分析了项目代码后,建议使用其他类的模板作为候选对象。
好吧,锦上添花的是将支持编写方面模板,例如,将方面以交互方式应用于您选择的类/方法,以便您可以动态评估最终结果,而无需进行明确的编译周期。
我敢肯定,如果转售者的创造者接管了业务,那么魔术就可以保证。
在完美的世界中编写方面代码
用TRIZ来解释,实现方面的理想代码编写是没有编写仅用于支持检测过程的附加代码。
在理想的世界中,我们希望对方面本身进行编码,而无需编写辅助逻辑来实现该目标。而且此代码将成为项目本身不可或缺的一部分。
第二个需求是具有交互式即插即用的能力,即编写完模板后,我们无需采取其他步骤即可将其用于转换。无需重新编译该工具,捕获其错误等。并在项目中配置选项以进行后期编译。
创建了模板并编写了几行代码后,我将立即看到结果,如果其中包含错误,则它们的检测和调试将被集成到应用模板的过程中,而不是一个单独的部分,不需要程序员付出额外的努力。
好吧,这样模板的语法就尽可能接近C#语言的语法,理想情况下是次要的附加组件,以及一些关键字和占位符。
我们目前的实施
不幸的是,我们没有生活在一个完美的世界中,因此我们必须重新发明自行车并骑自行车。
代码注入,编译和调试
我们当前的模型是创建项目的两个副本。一个是原始代码,一个是程序员用来工作的代码,第二个是转换后的代码,用于编译和执行。
这种情况是这样的
- , , ..
- , , , .
- , , , WPF , ..
为了进行调试,将启动IDE的第二个副本,打开该项目的国家/地区格式的副本,并且该副本可与应用了转换的副本一起使用。
该过程需要一定的纪律,但是不时地成为一种习惯,并且在某些情况下,这种方法具有一些优势(例如,可以将构建启动并部署到远程服务器,而不是与本地计算机一起使用)。加上VisualStudio中的插件帮助,使该过程更加容易。
集成开发环境
我们使用为我们的特定任务和流程量身定制的插件,并且对源代码实现的支持只是其功能的很小一部分。
例如,以类似于图形编辑器的样式显示图层的功能允许,例如,按范围(例如,仅可见公共方法)隐藏/显示注释图层。嵌入的代码周围带有特殊格式的注释,也可以将它们隐藏为单独的图层。
另一种可能性是显示原始文件与转换后的文件之间的差异。由于IDE知道项目中文件副本的相对位置,因此它可以显示原始文件和国家/地区生成的文件之间的差异。
另外,该插件在尝试更改国家/地区生成的副本时会发出警告(以免在随后的重新转换期间丢失它们)
组态
单独的任务是设置转换规则,即 我们将转换应用于哪些类和方法。
我们使用几个级别。
第一层是顶层配置文件。我们可以根据文件系统上的路径,文件名中的模式,类或方法,类的范围,方法或属性来设置规则。
第二级表示在类,方法或字段的属性级别上转换规则的应用。
第三个是代码块级别,第四个是显式指示,包括源代码中特定位置的模板转换结果。
范本
从历史上看,出于自动生成的目的,我们使用T4格式的模板,因此使用与模板相同的方法进行转换是很合逻辑的。T4模板具有执行任意C#代码的能力,具有最小的开销和良好的表达能力。
对于从未使用过T4的人来说,最简单的类似方法是呈现ASPX格式,该格式代替HTML在C#中生成源代码,并且不在IIS上执行,而是作为单独的实用程序将结果输出到控制台(或文件)。
示例
要了解其实际工作原理,最简单的方法是演示转换前后的代码以及转换过程中使用的模板的源代码。我将展示最简单的选项,但潜力仅受您的想象力限制。
转换前的样本源代码
// ##aspect=AutoComment
using AOP.Common;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Aspectimum.Demo.Lib
{
[AopTemplate("ClassLevelTemplateForMethods", NameFilter = "First")]
[AopTemplate("StaticAnalyzer", Action = AopTemplateAction.Classes)]
[AopTemplate("DependencyInjection", AdvicePriority = 500, Action = AopTemplateAction.PostProcessingClasses)]
[AopTemplate("ResourceReplacer", AdvicePriority = 1000, ExtraTag = "ResourceFile=Demo.resx,ResourceClass=Demo", Action = AopTemplateAction.PostProcessingClasses)]
public class ConsoleDemo
{
public virtual Person FirstDemo(string firstName, string lastName, int age)
{
Console.Out.WriteLine("FirstDemo: 1");
// ##aspect="FirstDemoComment" extra data here
return new Person()
{
FirstName = firstName,
LastName = lastName,
Age = age,
};
}
private static IConfigurationRoot _configuration = inject;
private IDataService _service { get; } = inject;
private Person _somePerson = inject;
[AopTemplate("LogExceptionMethod")]
[AopTemplate("StopWatchMethod")]
[AopTemplate("MethodFinallyDemo", AdvicePriority = 100)]
public Customer[] SecondDemo(Person[] people)
{
IEnumerable<Customer> Customers;
Console.Out.WriteLine("SecondDemo: 1");
Console.Out.WriteLine(i18("SecondDemo: i18"));
int configDelayMS = inject;
string configServerName = inject;
using (new AopTemplate("SecondDemoUsing", extraTag: "test extra"))
{
Customers = people.Select(s => new Customer()
{
FirstName = s.FirstName,
LastName = s.LastName,
Age = s.Age,
Id = s.Id
});
_service.Init(Customers);
foreach (var customer in Customers)
{
Console.Out.WriteLine(i18($"First Name {customer.FirstName} Last Name {customer.LastName}"));
Console.Out.WriteLine("SecondDemo: 2 " + i18("First Name ") + customer.FirstName + i18(" Last Name ") + customer.LastName);
}
}
Console.Out.WriteLine(i18(String.Format("SecondDemo: {0}", "3")));
Console.Out.WriteLine($"Server {configServerName} default delay {configDelayMS}");
Console.Out.WriteLine($"Customer for ID=5 is {_service.GetCustomerName(5)}");
return Customers.ToArray();
}
protected static string i18(string s) => s;
protected static dynamic inject;
[AopTemplate("NotifyPropertyChangedClass", Action = AopTemplateAction.Classes)]
[AopTemplate("NotifyPropertyChanged", Action = AopTemplateAction.Properties)]
public class Person
{
[AopTemplate("CacheProperty", extraTag: "{ \"CacheKey\": \"name_of_cache_key\", \"ExpiresInMinutes\": 10 }")]
public string FullName
{
get
{
// ##aspect="FullNameComment" extra data here
return $"{FirstName} {LastName}";
}
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
[AopTemplate("NotifyPropertyChanged", Action = AopTemplateAction.Properties)]
public class Customer : Person
{
public double CreditScore { get; set; }
}
public interface IDataService
{
void Init(IEnumerable<Customer> customers);
string GetCustomerName(int customerId);
}
public class DataService: IDataService
{
private IEnumerable<Customer> _customers;
public void Init(IEnumerable<Customer> customers)
{
_customers = customers;
}
public string GetCustomerName(int customerId)
{
return _customers.FirstOrDefault(w => w.Id == customerId)?.FullName;
}
}
public class MockDataService : IDataService
{
private IEnumerable<Customer> _customers;
public void Init(IEnumerable<Customer> customers)
{
if(customers == null)
throw (new Exception("IDataService.Init(customers == null)"));
}
public string GetCustomerName(int customerId)
{
if (customerId < 0)
throw (new Exception("IDataService.GetCustomerName: customerId cannot be negative"));
if (customerId == 0)
throw (new Exception("IDataService.GetCustomerName: customerId cannot be zero"));
return $"FirstName{customerId} LastName{customerId}";
}
}
}
}
转换后的完整版源代码
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
//
// Generated base on file: ConsoleDemo.cs
// ##sha256: ekmmxFSeH5ev8Epvl7QvDL+D77DHwq1gHDnCxzeBWcw
// Created By: JohnSmith
// Created Machine: 127.0.0.1
// Created At: 2020-09-19T23:18:07.2061273-04:00
//
// </auto-generated>
//------------------------------------------------------------------------------
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
namespace Aspectimum.Demo.Lib
{
public class ConsoleDemo
{
public virtual Person FirstDemo(string firstName, string lastName, int age)
{
Console.Out.WriteLine("FirstDemo: 1");
// FirstDemoComment replacement extra data here
return new Person()
{FirstName = firstName, LastName = lastName, Age = age, };
}
private static IConfigurationRoot _configuration = new ConfigurationBuilder()
.SetBasePath(System.IO.Path.Combine(AppContext.BaseDirectory))
.AddJsonFile("appsettings.json", optional: true)
.Build();
private IDataService _service { get; } = new DataService();
#error Cannot find injection rule for Person _somePerson
private Person _somePerson = inject;
public Customer[] SecondDemo(Person[] people)
{
try
{
#error variable "Customers" doesn't match code standard rules
IEnumerable<Customer> Customers;
Console.Out.WriteLine("SecondDemo: 1");
#error Cannot find resource for a string "SecondDemo: i18", please add it to resources
Console.Out.WriteLine(i18("SecondDemo: i18"));
int configDelayMS = Int32.Parse(_configuration["delay_ms"]);
string configServerName = _configuration["server_name"];
{
// second demo test extra
{
Customers = people.Select(s => new Customer()
{FirstName = s.FirstName, LastName = s.LastName, Age = s.Age, Id = s.Id});
_service.Init(Customers);
foreach (var customer in Customers)
{
Console.Out.WriteLine(String.Format(Demo.First_Last_Names_Formatted, customer.FirstName, customer.LastName));
Console.Out.WriteLine("SecondDemo: 2 " + (Demo.First_Name + " ") + customer.FirstName + (" " + Demo.Last_Name + " ") + customer.LastName);
}
}
}
#error Argument for i18 method must be either string literal or interpolated string, but instead got Microsoft.CodeAnalysis.CSharp.Syntax.InvocationExpressionSyntax
#warning Please replace String.Format with string interpolation format.
Console.Out.WriteLine(i18(String.Format("SecondDemo: {0}", "3")));
Console.Out.WriteLine($"Server {configServerName} default delay {configDelayMS}");
Console.Out.WriteLine($"Customer for ID=5 is {_service.GetCustomerName(5)}");
return Customers.ToArray();
}
catch (Exception logExpn)
{
Console.Error.WriteLine($"Exception in SecondDemo\r\n{logExpn.Message}\r\n{logExpn.StackTrace}");
throw;
}
}
protected static string i18(string s) => s;
protected static dynamic inject;
public class Person : System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
public string FullName
{
get
{
System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default;
string cachedData = cache["name_of_cache_key"] as string;
if (cachedData == null)
{
cachedData = GetPropertyData();
if (cachedData != null)
{
cache.Set("name_of_cache_key", cachedData, System.DateTimeOffset.Now.AddMinutes(10));
}
}
return cachedData;
string GetPropertyData()
{
// FullNameComment FullName
return $"{FirstName} {LastName}";
}
}
}
private int _id;
public int Id
{
get
{
return _id;
}
set
{
if (_id != value)
{
_id = value;
NotifyPropertyChanged();
}
}
}
private string _firstName;
public string FirstName
{
get
{
return _firstName;
}
set
{
if (_firstName != value)
{
_firstName = value;
NotifyPropertyChanged();
}
}
}
private string _lastName;
public string LastName
{
get
{
return _lastName;
}
set
{
if (_lastName != value)
{
_lastName = value;
NotifyPropertyChanged();
}
}
}
private int _age;
public int Age
{
get
{
return _age;
}
set
{
if (_age != value)
{
_age = value;
NotifyPropertyChanged();
}
}
}
}
public class Customer : Person
{
private double _creditScore;
public double CreditScore
{
get
{
return _creditScore;
}
set
{
if (_creditScore != value)
{
_creditScore = value;
NotifyPropertyChanged();
}
}
}
}
public interface IDataService
{
void Init(IEnumerable<Customer> customers);
string GetCustomerName(int customerId);
}
public class DataService : IDataService
{
private IEnumerable<Customer> _customers;
public void Init(IEnumerable<Customer> customers)
{
_customers = customers;
}
public string GetCustomerName(int customerId)
{
return _customers.FirstOrDefault(w => w.Id == customerId)?.FullName;
}
}
public class MockDataService : IDataService
{
private IEnumerable<Customer> _customers;
public void Init(IEnumerable<Customer> customers)
{
if (customers == null)
throw (new Exception("IDataService.Init(customers == null)"));
}
public string GetCustomerName(int customerId)
{
if (customerId < 0)
throw (new Exception("IDataService.GetCustomerName: customerId cannot be negative"));
if (customerId == 0)
throw (new Exception("IDataService.GetCustomerName: customerId cannot be zero"));
return $"FirstName{customerId} LastName{customerId}";
}
}
}
}
// ##template=AutoComment sha256=Qz6vshTZl2/u+NgtcV4u5W5RZMb9JPkJ2Zj0yvQBH9w
// ##template=AopCsharp.ttinclude sha256=2QR7LE4yvfWYNl+JVKQzvEBwcWvReeupVpslWTSWQ0c
// ##template=FirstDemoComment sha256=eIleHCim5r9F/33Mv9B7pcNQ/dlfEhDVXJVhA7+3OgY
// ##template=FullNameComment sha256=2/Ipn8fk2y+o/FVQHAWnrOlhqS5ka204YctZkwl/CUs
// ##template=NotifyPropertyChangedClass sha256=sxRrSjUSrynQSPjo85tmQywQ7K4fXFR7nN2mX87fCnk
// ##template=StaticAnalyzer sha256=zmJsj/FWmjqDDnpZXhoAxQB61nYujd41ILaQ4whcHyY
// ##template=LogExceptionMethod sha256=+zTre3r3LR9dm+bLPEEXg6u2OtjFg+/V6aCnJKijfcg
// ##template=NotifyPropertyChanged sha256=PMgorLSwEChpIPnEWXfEuUzUm4GO/6pMmoJdF7qcgn8
// ##template=CacheProperty sha256=oktDGTfC2hHoqpbKkeNABQaPdq6SrVLRFEQdNMoY4zE
// ##template=DependencyInjection sha256=nPq/ZxVBpgrDzyH+uLtJvD1aKbajKinX/DUBQ4BGG9g
// ##template=ResourceReplacer sha256=ZyUljjKKj0jLlM2nUIr1oJc1L7otYUI8WqWN7um6NxI
说明和模板代码
自动评论模板
// ##aspect=AutoComment
如果在源代码中遇到特殊格式的注释,则我们执行指定的模板(在本例中为AutoComment),然后插入转换结果而不是此注释。在此示例中,自动插入一个特殊的免责声明是有意义的,该免责声明会警告程序员该文件中的代码是转换的结果,而直接修改此文件则没有任何意义。
模板代码AutoComment.t4
<#@ include file="AopCsharp.ttinclude" #>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
//
// Generated base on file: <#= FileName #>
// ##sha256: <#= FileSha256 #>
// Created By: <#= User #>
// Created Machine: <#= MachineName #>
// Created At: <#= Now #>
//
// </auto-generated>
//------------------------------------------------------------------------------
变量FileName,FileSha256,User,MachineName和Now从转换过程中导出到模板。
转换结果
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
//
// Generated base on file: ConsoleDemo.cs
// ##sha256: PV3lHNDftTzVYnzNCZbKvtHCbscT0uIcHGRR/NJFx20
// Created By: EuGenie
// Created Machine: 192.168.0.1
// Created At: 2017-12-09T14:49:26.7173975-05:00
//
// </auto-generated>
//------------------------------------------------------------------------------
以下转换指定为类
[AopTemplate(“ ClassLevelTemplateForMethods”,NameFilter =“ First”)]的
属性。此属性表示应将模板应用于包含单词“ First”的所有类方法。NameFilter参数是一个正则表达式模式,用于确定要在转换中包括哪些方法。
模板代码ClassLevelTemplateForMethods.t4
<#@ include file="AopCsharp.ttinclude" #>
// class level template
<#= MethodStart() #><#= MethodBody() #><#= MethodEnd() #>
这是最简单的示例,
// class level template
在方法代码
转换结果之前添加注释
// class level template
public virtual Person FirstDemo(string firstName, string lastName, int age)
{
Console.Out.WriteLine("FirstDemo: 1");
// ##aspect="FirstDemoComment" extra data here
return new Person()
{
FirstName = firstName,
LastName = lastName,
Age = age,
};
}
将以下转换指定为方法属性,以演示应用于同一方法的多个转换。 LogExceptionMethod.t4模板
[AopTemplate("LogExceptionMethod")]
[AopTemplate("StopWatchMethod")]
[AopTemplate("MethodFinallyDemo", AdvicePriority = 100)]
<#@ include file="AopCsharp.ttinclude" #>
<# EnsureUsing("System"); #>
<#= MethodStart() #>
try
{
<#= MethodBody() #>
}
catch(Exception logExpn)
{
Console.Error.WriteLine($"Exception in <#= MethodName #>\r\n{logExpn.Message}\r\n{logExpn.StackTrace}");
throw;
}
<#= MethodEnd() #>
StopWatchMethod.t4模板
<#@ include file="AopCsharp.ttinclude" #>
<# EnsureUsing("System.Diagnostics"); #>
<#= MethodStart() #>
var stopwatch = Stopwatch.StartNew();
try
{
<#= MethodBody() #>
}
finally
{
stopwatch.Stop();
Console.Out.WriteLine($"Method <#= MethodName #>: {stopwatch.ElapsedMilliseconds}");
}
<#= MethodEnd() #>
MethodFinallyDemo.t4模板
<#@ include file="AopCsharp.ttinclude" #>
<#= MethodStart() #>
try
{
<#= MethodBody() #>
}
finally
{
// whatever logic you need to include for a method
}
<#= MethodEnd() #>
转换结果
public Customer[] SecondDemo(Person[] people)
{
try
{
var stopwatch = Stopwatch.StartNew();
try
{
try
{
IEnumerable<Customer> customers;
Console.Out.WriteLine("SecondDemo: 1");
{
// second demo test extra
{
customers = people.Select(s => new Customer()
{FirstName = s.FirstName, LastName = s.LastName, Age = s.Age, });
foreach (var customer in customers)
{
Console.Out.WriteLine($"SecondDemo: 2 {customer.FirstName} {customer.LastName}");
}
}
}
Console.Out.WriteLine("SecondDemo: 3");
return customers.ToArray();
}
catch (Exception logExpn)
{
Console.Error.WriteLine($"Exception in SecondDemo\r\n{logExpn.Message}\r\n{logExpn.StackTrace}");
throw;
}
}
finally
{
stopwatch.Stop();
Console.Out.WriteLine($"Method SecondDemo: {stopwatch.ElapsedMilliseconds}");
}
}
finally
{
// whatever logic you need to include for a method
}
}
对于限于使用构造的块,给出以下转换
using (new AopTemplate("SecondDemoUsing", extraTag: "test extra"))
{
customers = people.Select(s => new Customer()
{
FirstName = s.FirstName,
LastName = s.LastName,
Age = s.Age,
});
foreach (var customer in customers)
{
Console.Out.WriteLine($"SecondDemo: 2 {customer.FirstName} {customer.LastName}");
}
}
SecondDemoUsing.t4模板
<#@ include file="AopCsharp.ttinclude" #>
// second demo <#= ExtraTag #>
<#= StatementBody() #>
ExtraTag是作为参数传递的字符串。这对于根据输入参数可能具有稍微不同的行为的泛型很有用。
转换结果
{
// second demo test extra
{
customers = people.Select(s => new Customer()
{FirstName = s.FirstName, LastName = s.LastName, Age = s.Age, });
foreach (var customer in customers)
{
Console.Out.WriteLine($"SecondDemo: 2 {customer.FirstName} {customer.LastName}");
}
}
}
以下转换是由 NotifyPropertyChanged类的属性指定的,这是一个经典示例,在面向方面编程的大多数示例中都提供了日志记录示例。
[AopTemplate("NotifyPropertyChangedClass", Action = AopTemplaceAction.Classes)]
[AopTemplate("NotifyPropertyChanged", Action = AopTemplaceAction.Properties)]
NotifyPropertyChangedClass.t4模板应用于类代码
.
, AOP .Net
<#@ include file="AopCsharp.ttinclude" #>
<#
// the class already implements INotifyPropertyChanged, nothing to do here
if(ImplementsBaseType(ClassNode, "INotifyPropertyChanged", "System.ComponentModel.INotifyPropertyChanged"))
return null;
var classNode = AddBaseTypes<ClassDeclarationSyntax>(ClassNode, "System.ComponentModel.INotifyPropertyChanged");
#>
<#= ClassStart(classNode) #>
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
<#= ClassBody(classNode) #>
<#= ClassEnd(classNode) #>
.
Fogy
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
public partial class ModuleWeaver
{
public void InjectINotifyPropertyChangedInterface(TypeDefinition targetType)
{
targetType.Interfaces.Add(new InterfaceImplementation(PropChangedInterfaceReference));
WeaveEvent(targetType);
}
void WeaveEvent(TypeDefinition type)
{
var propertyChangedFieldDef = new FieldDefinition("PropertyChanged", FieldAttributes.Private | FieldAttributes.NotSerialized, PropChangedHandlerReference);
type.Fields.Add(propertyChangedFieldDef);
var propertyChangedField = propertyChangedFieldDef.GetGeneric();
var eventDefinition = new EventDefinition("PropertyChanged", EventAttributes.None, PropChangedHandlerReference)
{
AddMethod = CreateEventMethod("add_PropertyChanged", DelegateCombineMethodRef, propertyChangedField),
RemoveMethod = CreateEventMethod("remove_PropertyChanged", DelegateRemoveMethodRef, propertyChangedField)
};
type.Methods.Add(eventDefinition.AddMethod);
type.Methods.Add(eventDefinition.RemoveMethod);
type.Events.Add(eventDefinition);
}
MethodDefinition CreateEventMethod(string methodName, MethodReference delegateMethodReference, FieldReference propertyChangedField)
{
const MethodAttributes Attributes = MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.Final |
MethodAttributes.SpecialName |
MethodAttributes.NewSlot |
MethodAttributes.Virtual;
var method = new MethodDefinition(methodName, Attributes, TypeSystem.VoidReference);
method.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, PropChangedHandlerReference));
var handlerVariable0 = new VariableDefinition(PropChangedHandlerReference);
method.Body.Variables.Add(handlerVariable0);
var handlerVariable1 = new VariableDefinition(PropChangedHandlerReference);
method.Body.Variables.Add(handlerVariable1);
var handlerVariable2 = new VariableDefinition(PropChangedHandlerReference);
method.Body.Variables.Add(handlerVariable2);
var loopBegin = Instruction.Create(OpCodes.Ldloc, handlerVariable0);
method.Body.Instructions.Append(
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldfld, propertyChangedField),
Instruction.Create(OpCodes.Stloc, handlerVariable0),
loopBegin,
Instruction.Create(OpCodes.Stloc, handlerVariable1),
Instruction.Create(OpCodes.Ldloc, handlerVariable1),
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Call, delegateMethodReference),
Instruction.Create(OpCodes.Castclass, PropChangedHandlerReference),
Instruction.Create(OpCodes.Stloc, handlerVariable2),
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldflda, propertyChangedField),
Instruction.Create(OpCodes.Ldloc, handlerVariable2),
Instruction.Create(OpCodes.Ldloc, handlerVariable1),
Instruction.Create(OpCodes.Call, InterlockedCompareExchangeForPropChangedHandler),
Instruction.Create(OpCodes.Stloc, handlerVariable0),
Instruction.Create(OpCodes.Ldloc, handlerVariable0),
Instruction.Create(OpCodes.Ldloc, handlerVariable1),
Instruction.Create(OpCodes.Bne_Un_S, loopBegin), // go to begin of loop
Instruction.Create(OpCodes.Ret));
method.Body.InitLocals = true;
method.Body.OptimizeMacros();
return method;
}
}
, AOP .Net
NotifyPropertyChanged.t4模板应用于类属性
<#@ include file="AopCsharp.ttinclude" #>
<#
if(!(PropertyHasEmptyGetBlock() && PropertyHasEmptySetBlock()))
return null;
string privateUnqiueName = GetUniquePrivatePropertyName(ClassNode, PropertyNode.Identifier.ToString());
#>
private <#= PropertyNode.Type.ToFullString() #> <#= privateUnqiueName #><#= PropertyNode.Initializer != null ? " = " + PropertyNode.Initializer.ToFullString() : "" #>;
<#= PropertyNode.AttributeLists.ToFullString() + PropertyNode.Modifiers.ToFullString() + PropertyNode.Type.ToFullString() + PropertyNode.Identifier.ToFullString() #>
{
get { return <#= privateUnqiueName #>; }
set
{
if(<#= privateUnqiueName #> != value)
{
<#= privateUnqiueName #> = value;
NotifyPropertyChanged();
}
}
}
类和属性的原始代码
public class Person
{
public int Id { get; set; }
// ...
}
转换结果
public class Person : System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
private int _id;
public int Id
{
get
{
return _id;
}
set
{
if (_id != value)
{
_id = value;
NotifyPropertyChanged();
}
}
}
// ...
}
用于缓存属性结果的模板示例,它由
[AopTemplate("CacheProperty", extraTag: "{ \"CacheKey\": \"name_of_cache_key\", \"ExpiresInMinutes\": 10 }")]
Template参数指定为JSON attribute 。如果没有显式参数,则使用默认参数。
CacheProperty.t4模板
<#@ include file="AopCsharp.ttinclude" #>
<#
// The template accepts a configuration value from extraTag in two ways
// 1. as a number of minutes to use for expiration (example: 8)
// 2. as a string in JSON in format { CacheKey: "name_of_cache_key", CacheKeyVariable: "name_of_variable", ExpiresInMinutes: 10, ExpiresVariable: "name_of_variable" }
//
// CacheKey (optional) name of the cache key, the name will be used as a literal string (example: my_key)
// CacheKeyVariable (optional) name of variable that holds the cache key (example: GlobalConsts.MyKeyName)
//
// ExpiresInMinutes (optional) number minutes that the cache value will expires (example: 12)
// ExpiresVariable (optional) name of a variable that the expiration value will be get from (example: AppConfig.EXPIRE_CACHE)
//
// if any of expiration values are not specified, 5 minutes default expiration will be used
if(!PropertyHasAnyGetBlock())
return null;
const int DEFAULT_EXPIRES_IN_MINUTES = 5;
string propertyName = PropertyNode.Identifier.ToFullString().Trim();
string propertyType = PropertyNode.Type.ToFullString().Trim();
string expiresInMinutes = DEFAULT_EXPIRES_IN_MINUTES.ToString();
string cacheKey = "\"" + ClassNode.Identifier.ToFullString() + ":" + propertyName + "\"";
if(!String.IsNullOrEmpty(ExtraTag))
{
if(Int32.TryParse(ExtraTag, out int exp))
{
expiresInMinutes = exp.ToString();
}
else
{
JsonDocument json = ExtraTagAsJson();
if(json != null && json.RootElement.ValueKind == JsonValueKind.Object)
{
if(json.RootElement.TryGetProperty("CacheKey", out JsonElement cacheKeyElement))
{
string s = cacheKeyElement.GetString();
if(!String.IsNullOrEmpty(s))
cacheKey = "\"" + s + "\"";
}
else if(json.RootElement.TryGetProperty("CacheKeyVariable", out JsonElement cacheVariableElement))
{
string s = cacheVariableElement.GetString();
if(!String.IsNullOrEmpty(s))
cacheKey = s;
}
if(json.RootElement.TryGetProperty("ExpiresInMinutes", out JsonElement expiresInMinutesElement))
{
if(expiresInMinutesElement.TryGetInt32(out int v) && v > 0)
expiresInMinutes = "" + v;
}
else if(json.RootElement.TryGetProperty("ExpiresVariable", out JsonElement expiresVariableElement))
{
string s = expiresVariableElement.GetString();
if(!String.IsNullOrEmpty(s))
expiresInMinutes = s;
}
}
}
}
#>
<#= PropertyDefinition() #>
{
get
{
System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default;
<#= propertyType #> cachedData = cache[<#= cacheKey #>] as <#= propertyType #>;
if(cachedData == null)
{
cachedData = GetPropertyData();
if(cachedData != null)
{
cache.Set(<#= cacheKey #>, cachedData, System.DateTimeOffset.Now.AddMinutes(<#= expiresInMinutes #>));
}
}
return cachedData;
<#= propertyType #> GetPropertyData()
{
<# if(PropertyNode.ExpressionBody != null ) { #>
return (<#= PropertyNode.ExpressionBody.Expression.ToFullString() #>);
<# } else if(PropertyNode?.AccessorList?.Accessors.FirstOrDefault(w => w.ExpressionBody != null && w.Keyword.ToString() == "get") != null) { #>
return (<#= PropertyNode?.AccessorList?.Accessors.FirstOrDefault(w => w.ExpressionBody != null && w.Keyword.ToString() == "get").ExpressionBody.Expression.ToFullString() #>);
<# } else { #>
<#= PropertyGetBlock() #>
<# } #>
}
}
<#
if(PropertyHasAnySetBlock()) { #>
set
{
System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default;
cache.Remove(<#= cacheKey #>); // invalidate cache for the property
<#= PropertySetBlock() #>
}
<# } #>
}
资源
[AopTemplate("CacheProperty", extraTag: "{ \"CacheKey\": \"name_of_cache_key\", \"ExpiresInMinutes\": 10 }")]
public string FullName
{
get
{
return $"{FirstName} {LastName}";
}
}
CacheProperty.t4的转换结果
public string FullName
{
get
{
System.Runtime.Caching.ObjectCache cache = System.Runtime.Caching.MemoryCache.Default;
string cachedData = cache["name_of_cache_key"] as string;
if (cachedData == null)
{
cachedData = GetPropertyData();
if (cachedData != null)
{
cache.Set("name_of_cache_key", cachedData, System.DateTimeOffset.Now.AddMinutes(10));
}
}
return cachedData;
string GetPropertyData()
{
// FullNameComment FullName
return $"{FirstName} {LastName}";
}
}
}
下次再次从评论中调用模板
// ##aspect="FullNameComment" extra data here
FullNameComment.t4模板
<#@ include file="AopCsharp.ttinclude" #>
// FullNameComment <#= PropertyNode.Identifier #>
与AutoComment.t4模板非常相似,但是在这里我们演示PropertyNode的用法。另外,“ Extra data here”数据可通过ExtraTag参数提供给FullNameComment.t4模板(但在此示例中,我们不使用它们,因此将其忽略)。
转换结果
// FullNameComment FullName
由
[AopTemplate("NotifyPropertyChanged", Action = AopTemplaceAction.Properties)]
AND属性指定的文件中以下转换与Person类的转换相同。上面已经包含了NotifyPropertyChanged.t4模板的源代码。
转换结果
public class Customer : Person
{
private double _creditScore;
public double CreditScore
{
get
{
return _creditScore;
}
set
{
if (_creditScore != value)
{
_creditScore = value;
NotifyPropertyChanged();
}
}
}
}
最后部分
尽管本文着重于面向方面的编程,但是源代码转换技术是通用的,并且原则上可以用于与AOP不相关的任务。
例如,它可以用于依赖注入,即 我们根据构建参数更改资源创建代码。
DependencyInjection.t4模板
<#@ include file="AopCsharp.ttinclude" #>
<#
var syntaxNode = FieldsInjection(SyntaxNode);
syntaxNode = VariablesInjection(syntaxNode);
syntaxNode = PropertiesInjection(syntaxNode);
if(syntaxNode == SyntaxNode)
return null;
#>
<#= syntaxNode.ToFullString() #>
<#+
private SyntaxNode VariablesInjection(SyntaxNode syntaxNode)
{
return RewriteNodes<LocalDeclarationStatementSyntax >(syntaxNode, OnLocalVariablesInjection);
SyntaxNode OnLocalVariablesInjection(LocalDeclarationStatementSyntax node)
{
var errorMsgs = new System.Text.StringBuilder();
SyntaxNode syntaxNode = RewriteNodes<VariableDeclaratorSyntax>(node, (n) => OnVariableDeclaratorVisit(n, node.Declaration.Type, errorMsgs));
if(errorMsgs.Length > 0)
return AddErrorMessageTrivia(syntaxNode, errorMsgs.ToString());
return syntaxNode;
}
}
private SyntaxNode PropertiesInjection(SyntaxNode syntaxNode)
{
return RewriteNodes<PropertyDeclarationSyntax>(syntaxNode, OnPropertyInjection);
SyntaxNode OnPropertyInjection(PropertyDeclarationSyntax node)
{
if(node.Initializer?.Value?.ToString() != "inject")
return node;
var errorMsgs = new System.Text.StringBuilder();
SyntaxNode syntaxNode = DoInjection(node, node.Identifier.ToString().Trim(), node.Initializer.Value, node.Type, errorMsgs);
if(errorMsgs.Length > 0)
return AddErrorMessageTrivia(syntaxNode, errorMsgs.ToString());
return syntaxNode;
}
}
private SyntaxNode FieldsInjection(SyntaxNode syntaxNode)
{
return RewriteNodes<BaseFieldDeclarationSyntax>(syntaxNode, OnFieldsInjection);
SyntaxNode OnFieldsInjection(BaseFieldDeclarationSyntax node)
{
var errorMsgs = new System.Text.StringBuilder();
SyntaxNode syntaxNode = RewriteNodes<VariableDeclaratorSyntax>(node, (n) => OnVariableDeclaratorVisit(n, node.Declaration.Type, errorMsgs));
if(errorMsgs.Length > 0)
return AddErrorMessageTrivia(syntaxNode, errorMsgs.ToString());
return syntaxNode;
}
}
private SyntaxNode OnVariableDeclaratorVisit(VariableDeclaratorSyntax node, TypeSyntax typeSyntax, System.Text.StringBuilder errorMsgs)
{
if(node.Initializer?.Value?.ToString() != "inject")
return node;
return DoInjection(node, node.Identifier.ToString().Trim(), node.Initializer.Value, typeSyntax, errorMsgs);
}
private SyntaxNode DoInjection(SyntaxNode node, string varName, ExpressionSyntax initializerNode, TypeSyntax typeSyntax, System.Text.StringBuilder errorMsgs)
{
string varType = typeSyntax.ToString().Trim();
Log($"{varName} {varType} {initializerNode.ToString()}");
if(varName.StartsWith("config"))
{
string configName = Regex.Replace(Regex.Replace(varName, "^config", ""), "([a-z])([A-Z])", (m) => m.Groups[1].Value + "_" + m.Groups[2].Value).ToLower();
ExpressionSyntax configNode = CreateElementAccess("_configuration", CreateStringLiteral(configName));
if(varType == "int")
{
configNode = CreateMemberAccessInvocation("Int32", "Parse", configNode);
}
return node.ReplaceNode(initializerNode, configNode);
}
switch(varType)
{
case "Microsoft.Extensions.Configuration.IConfigurationRoot":
case "IConfigurationRoot":
EnsureUsing("Microsoft.Extensions.Configuration");
ExpressionSyntax pathCombineArg = CreateMemberAccessInvocation("System.IO.Path", "Combine", CreateMemberAccess("AppContext", "BaseDirectory"));
ExpressionSyntax builderNode = CreateNewType("ConfigurationBuilder").WithTrailingTrivia(SyntaxFactory.EndOfLine("\r\n"));
builderNode = CreateMemberAccessInvocation(builderNode, "SetBasePath", pathCombineArg).WithTrailingTrivia(SyntaxFactory.EndOfLine("\r\n"));
ExpressionSyntax addJsonFileArg = CreateMemberAccessInvocation("System.IO.Path", "Combine", CreateMemberAccess("AppContext", "BaseDirectory"));
builderNode = CreateMemberAccessInvocationNamedArgs(builderNode, "AddJsonFile",
(null, CreateStringLiteral("appsettings.json")),
("optional", SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression))).WithTrailingTrivia(SyntaxFactory.EndOfLine("\r\n"));
if(GetGlobalSetting("env")?.ToLower() == "test")
{
builderNode = CreateMemberAccessInvocationNamedArgs(builderNode, "AddJsonFile",
(null, CreateStringLiteral("appsettings.test.json")),
("optional", SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression)));
}
builderNode = CreateMemberAccessInvocation(builderNode, "Build");
return node.ReplaceNode(initializerNode, builderNode);
case "IDataService":
{
string className = (GetGlobalSetting("env")?.ToLower() == "test" ? "MockDataService" : "DataService");
return node.ReplaceNode(initializerNode, CreateNewType(className));
}
}
errorMsgs.AppendLine($"Cannot find injection rule for {varType} {varName}");
return node;
}
#>
在源代码中(此处使用了动态变量功能,可将其分配给任何类型),即 为了表现力,我们想出了一个新关键字。
private static IConfigurationRoot _configuration = inject;
private IDataService _service { get; } = inject;
// ...
public Customer[] SecondDemo(Person[] people)
{
int configDelayMS = inject; // we are going to inject dependency to local variables
string configServerName = inject;
}
// ...
protected static dynamic inject;
在转换期间,将使用比较GetGlobalSetting(“ env”)==“ test”,并根据此条件,将注入新的DataService()或新的MockDataService()。
转换结果
private static IConfigurationRoot _configuration = new ConfigurationBuilder()
.SetBasePath(System.IO.Path.Combine(AppContext.BaseDirectory))
.AddJsonFile("appsettings.json", optional: true)
.Build();
private IDataService _service { get; } = new DataService();
// ...
public Customer[] SecondDemo(Person[] people)
{
int configDelayMS = Int32.Parse(_configuration["delay_ms"]);
string configServerName = _configuration["server_name"];
}
// ...
或者您可以将此工具用作“穷人”静态分析(但是使用本机Roslyn功能实现分析器要正确得多),我们分析规则代码并将其插入源代码中,
#error our error message here
这将导致编译时错误。
#warning our warning message here
这将在IDE或编译时作为警告。
StaticAnalyzer.t4模板
<#@ include file="AopCsharp.ttinclude" #>
<#
var syntaxNode = AnalyzeLocalVariables(SyntaxNode);
syntaxNode = AnalyzeStringFormat(syntaxNode);
if(syntaxNode == SyntaxNode)
return null;
#>
<#= syntaxNode.ToFullString() #>
<#+
private SyntaxNode AnalyzeLocalVariables(SyntaxNode syntaxNode)
{
return RewriteNodes<LocalDeclarationStatementSyntax>(syntaxNode, OnAnalyzeLocalVariablesNodeVisit);
SyntaxNode OnAnalyzeLocalVariablesNodeVisit(LocalDeclarationStatementSyntax node)
{
var errorMsgs = new System.Text.StringBuilder();
string d = "";
foreach(VariableDeclaratorSyntax variableNode in node.DescendantNodes().OfType<VariableDeclaratorSyntax>().Where(w => Regex.IsMatch(w.Identifier.ToString(), "^[A-Z]")))
{
LogDebug($"variable: {variableNode.Identifier.ToString()}");
errorMsgs.Append(d + $"variable \"{variableNode.Identifier.ToString()}\" doesn't match code standard rules");
d = ", ";
}
if(errorMsgs.Length > 0)
return AddErrorMessageTrivia(node, errorMsgs.ToString());
return node;
}
}
private SyntaxNode AnalyzeStringFormat(SyntaxNode syntaxNode)
{
return RewriteLeafStatementNodes(syntaxNode, OnAnalyzeStringFormat);
SyntaxNode OnAnalyzeStringFormat(StatementSyntax node)
{
bool hasStringFormat = false;
foreach(MemberAccessExpressionSyntax memberAccessNode in node.DescendantNodes().OfType<MemberAccessExpressionSyntax>())
{
if(memberAccessNode.Name.ToString().Trim() != "Format")
continue;
string expr = memberAccessNode.Expression.ToString().Trim().ToLower();
if(expr != "string" && expr != "system.string")
continue;
hasStringFormat = true;
break;
}
if(hasStringFormat)
return AddWarningMessageTrivia(node, "Please replace String.Format with string interpolation format.");
return node;
}
}
#>
转换结果
#error variable "Customers" doesn't match code standard rules
IEnumerable<Customer> Customers;
// ...
#warning Please replace String.Format with string interpolation format.
Console.Out.WriteLine(i18(String.Format("SecondDemo: {0}", "3")));
或作为用于本地化应用程序的自动工具,即 在类中找到所有字符串,并使用适当的资源替换它们。
ResourceReplacer.t4模板
<#@ include file="AopCsharp.ttinclude" #>
<#
Dictionary<string, string> options = ExtraTagAsDictionary();
_resources = LoadResources(options["ResourceFile"]);
_resourceClass = options["ResourceClass"];
var syntaxNode = RewriteLeafStatementNodes(SyntaxNode, OnStatementNodeVisit);
#>
<#= syntaxNode.ToFullString() #>
<#+
private SyntaxNode OnStatementNodeVisit(StatementSyntax node)
{
if(!node.DescendantNodes().OfType<InvocationExpressionSyntax>().Any(w => (w.Expression is IdentifierNameSyntax) && ((IdentifierNameSyntax)w.Expression).Identifier.ToString() == "i18" ))
return node;
var errorMsgs = new System.Text.StringBuilder();
SyntaxNode syntaxNode = RewriteNodes<InvocationExpressionSyntax>(node, (n) => OnInvocationExpressionVisit(n, errorMsgs));
if(errorMsgs.Length > 0)
return AddErrorMessageTrivia(syntaxNode, errorMsgs.ToString());
return syntaxNode;
}
private SyntaxNode OnInvocationExpressionVisit(InvocationExpressionSyntax node, System.Text.StringBuilder errorMsgs)
{
if(!(node.Expression is IdentifierNameSyntax && ((IdentifierNameSyntax)node.Expression).Identifier.ToString() == "i18" ))
return node;
ArgumentSyntax arg = node.ArgumentList.Arguments.Single(); // We know that i18 method accepts only one argument. Keep in mind that it is just a demo and in real life you could be more inventive
var expr = arg.Expression;
if(!(expr is LiteralExpressionSyntax || expr is InterpolatedStringExpressionSyntax))
{
errorMsgs.AppendLine($"Argument for i18 method must be either string literal or interpolated string, but instead got {arg.Expression.GetType().ToString()}");
return node;
}
string s = expr.ToString();
if(s.StartsWith("$"))
{
(string format, List<ExpressionSyntax> expressions) = ConvertInterpolatedStringToFormat((InterpolatedStringExpressionSyntax)expr);
ExpressionSyntax stringNode = ReplaceStringWithResource("\"" + format + "\"", errorMsgs);
if(stringNode != null)
{
var memberAccess = CreateMemberAccess("String", "Format");
var arguments = new List<ArgumentSyntax>();
arguments.Add(SyntaxFactory.Argument(stringNode));
expressions.ForEach(item => arguments.Add(SyntaxFactory.Argument(item)));
var argumentList = SyntaxFactory.SeparatedList(arguments);
return SyntaxFactory.InvocationExpression(memberAccess, SyntaxFactory.ArgumentList(argumentList));
}
}
else
{
SyntaxNode stringNode = ReplaceStringWithResource(s, errorMsgs);
if(stringNode != null)
return stringNode;
}
return node;
}
private ExpressionSyntax ReplaceStringWithResource(string s, System.Text.StringBuilder errorMsgs)
{
Match m = System.Text.RegularExpressions.Regex.Match(s, "^\"(\\s*)(.*?)(\\s*)\"$");
if(!m.Success)
{
errorMsgs.AppendLine($"String doesn't match search criteria");
return null;
}
if(!_resources.TryGetValue(m.Groups[2].Value, out string resourceName))
{
errorMsgs.AppendLine($"Cannot find resource for a string {s}, please add it to resources");
return null;
}
string csharpName = Regex.Replace(resourceName, "[^A-Za-z0-9]", "_");
ExpressionSyntax stringNode = CreateMemberAccess(_resourceClass, csharpName);
if(!String.IsNullOrEmpty(m.Groups[1].Value) || !String.IsNullOrEmpty(m.Groups[3].Value))
{
if(!String.IsNullOrEmpty(m.Groups[1].Value))
{
stringNode = SyntaxFactory.BinaryExpression(SyntaxKind.AddExpression,
CreateStringLiteral(m.Groups[1].Value),
stringNode);
}
if(!String.IsNullOrEmpty(m.Groups[3].Value))
{
stringNode = SyntaxFactory.BinaryExpression(SyntaxKind.AddExpression,
stringNode,
CreateStringLiteral(m.Groups[3].Value));
}
stringNode = SyntaxFactory.ParenthesizedExpression(stringNode);
}
return stringNode;
}
private string _resourceClass;
private Dictionary<string,string> _resources;
#>
资源
Console.Out.WriteLine(i18("SecondDemo: i18"));
// ...
Console.Out.WriteLine(i18($"First Name {customer.FirstName} Last Name {customer.LastName}"));
Console.Out.WriteLine("SecondDemo: 2 " + i18("First Name ") + customer.FirstName + i18(" Last Name ") + customer.LastName);
// ...
Console.Out.WriteLine(i18(String.Format("SecondDemo: {0}", "3")));
// ...
protected static string i18(string s) => s;
在资源文件Demo.resx中,我们为示例创建了以下几行
<data name="First Last Names Formatted" xml:space="preserve">
<value>First Name {0} Last Name {1}</value>
</data>
<data name="First Name" xml:space="preserve">
<value>First Name</value>
</data>
<data name="Last Name" xml:space="preserve">
<value>Last Name</value>
</data>
以及Demo.Designer.cs文件的自动生成的代码
public class Demo
{
// ...
public static string First_Last_Names_Formatted
{
get
{
return ResourceManager.GetString("First Last Names Formatted", resourceCulture);
}
}
public static string First_Name
{
get
{
return ResourceManager.GetString("First Name", resourceCulture);
}
}
public static string Last_Name
{
get
{
return ResourceManager.GetString("Last Name", resourceCulture);
}
}
}
转换结果(请注意,插入的字符串已替换为String.Format,并使用了“名字{0}姓{1}”资源)。对于资源文件中不存在或与我们的格式不匹配的行,将添加错误消息
//#error Cannot find resource for a string "SecondDemo: i18", please add it to resources
Console.Out.WriteLine(i18("SecondDemo: i18"));
// ...
Console.Out.WriteLine(String.Format(Demo.First_Last_Names_Formatted, customer.FirstName, customer.LastName));
Console.Out.WriteLine("SecondDemo: 2 " + (Demo.First_Name + " ") + customer.FirstName + (" " + Demo.Last_Name + " ") + customer.LastName);
// ...
//#error Argument for i18 method must be either string literal or interpolated string, but instead got Microsoft.CodeAnalysis.CSharp.Syntax.InvocationExpressionSyntax
Console.Out.WriteLine(i18(String.Format("SecondDemo: {0}", "3")));
此外,转换工具不仅可以处理C#文件,还可以处理任何文件类型(当然,有一定的限制)。如果您有一个可以为您的语言构建AST的解析器,则可以用该解析器替换Roslyn,调整代码处理程序的实现,它将起作用。不幸的是,功能非常接近Roslyn的库非常有限,使用它们需要付出更多的努力。除了C#外,我们还对JavaScript和TypeScript项目使用了转换,但肯定不像C#那样全面。
我再次重申,示例代码和模板是为了说明这种方法的可能性而提供的,正如他们所说的那样-天空是极限。
谢谢你的时间。
本文的主要部分是在几年前撰写的,但不幸的是,由于某种原因,它只能在现在发布。
我们最初的工具是在.Net Framework上开发的,但是我们已经根据.Net Core的MIT许可开始开发简化的开源版本。目前,结果已经完全可用,并且90%可以使用,还有一些小改进,代码的发型,文档和示例的创建,但是如果没有所有这些,将很难进入项目,这个想法本身将受到损害,而DX将是负面的。
从事创作工作的人在搬到另一家公司之前无法完成创作,因此在分配资源以继续工作之前,我们要研究一下社区的反应,因为我们知道适合我们的情况并不一定是有需求的,而且很有可能,某些替代工具或开发方法正在填补这一利基。
该工具的想法非常简单,开发人员花了大约一个月的时间来实施一个可行的版本,因此我认为具有良好资格和经验的Roslyn程序员将能够在几天内创建自己的特定版本。目前,项目源代码(包括示例和模板)的大小仅为150KB。
我将很高兴收到建设性的批评(非建设性的批评也不会让我不安,所以不要犹豫)。
感谢Phil Rangin(fillpackart)以鼓励撰写文章。频道“我们注定要失败”规则!