使用扩展方法进行实例化和初始化的简写
使用扩展方法实例化和初始化对象是否有任何简写?
我的目标是抽象并封装实例化和初始化适合单元测试的 MyType 实例所需的代码。
例子:
//...
//convoluted client code - I'd like to avoid the null instance creation
MyType t = null;
t = t.GetTestInstance();
//...
//extension method
public static class MyTypeExtensions
{
public static MyType GetTestInstance(this MyType t)
{
var ctorInjectedDependency = blah;
return new MyType(ctorInjectedDependency);
}
}
Is there any shorthand for using an extension method to instantiate and initialise an object?
My aim is to abstract-away and encapsulate the code required to instantiate and initialise an instance of MyType suitable for unit testing.
Example:
//...
//convoluted client code - I'd like to avoid the null instance creation
MyType t = null;
t = t.GetTestInstance();
//...
//extension method
public static class MyTypeExtensions
{
public static MyType GetTestInstance(this MyType t)
{
var ctorInjectedDependency = blah;
return new MyType(ctorInjectedDependency);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这样的东西会满足您的需求:
用法:
Maybe something like this will suit your needs:
Usage: