使用Resharper模板,我想根据上下文类型数据动态调整结果代码,这些数据可在完成模板时从静态分析中获得。
例如,如果我的模板参数之一是方法名称,则在输入方法名称并跳出替换项之后,我想评估方法的返回类型,并相应地调整模板的另一部分。类似于switch语句,其中每种情况都是一种类型,默认情况下会掉线。
这是我要实现的目标的(详细)示例:
/* template */
return _service.MakeCall(
client => client.$methodName$(),
returnValue => returnValue.$transformMethod$());
/* class being written with the assistance of the template */
class FooDataAccess
{
ServiceHelper<FooServiceClient> _service;
public Foo GetFoo(int id)
{
/* template usage, the type returned by GetFoo() does not result is any
additional transformation */
return _service.MakeCall(
client => client.GetFoo(),
returnValue => returnValue.ToDomain());
}
public IEnumerable<Foo> GetFoos()
{
/* template usage, $transformMethod$ is wrapped in a Select method since
GetFoo() returns an IEnumerable */
return _service.MakeCall(
client => client.GetFoo(),
returnValue => returnValue.Select(p => p.ToDomain()));
}
}
/* supporting classes */
class ServiceHelper<TClient>
{
public TOutput MakeCall<TResult, TOutput>(Func<TClient, TResult> call, Func<TResult, TOutput> transform)
{ ... }
}
class FooServiceClient
{
public FooData GetFoo(int id)
{ /* make service call */ }
public IEnumerable<FooData> GetFoos()
{ /* make service call */ }
}
static class FooExtensions
{
public static Foo ToDomain(this FooData data)
{ /* hydrate domain from data */ }
public static FooData ToData(this Foo domain)
{ /* hydrate data from domain */ }
}