假设我有很多这样的样板代码:
class MyClass
{
private readonly IDependencyA dependencyA;
private readonly IDependencyB dependencyB;
public MyClass(
IDependencyA dependencyA,
IDependencyB dependencyB)
{
if(dependencyA == null) throw ArgumentNullException("dependencyA");
if(dependencyB == null) throw ArgumentNullException("dependencyB");
this.dependencyA = dependencyA;
this.dependencyB = dependencyB;
...
}
...
public void SomeMethod()
{
this.dependencyA.DoSomething(this.dependencyB);
}
}
有没有办法使用 PostSharp 之类的东西来删除样板代码并使其看起来像这样:
class MyClass
{
[ConstructorParametersAreClassMembers]
public MyClass(
IDependencyA dependencyA,
IDependencyB dependencyB)
{
...
}
...
public void SomeMethod()
{
this.dependencyA.DoSomething(this.dependencyB);
}
}
这甚至可能吗?
另外:这实际上是它在 F# 中默认的工作方式。