使用扩展方法进行实例化和初始化的简写

发布于 2024-09-12 07:43:51 字数 453 浏览 5 评论 0原文

使用扩展方法实例化和初始化对象是否有任何简写?

我的目标是抽象并封装实例化和初始化适合单元测试的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所谓喜欢 2024-09-19 07:43:51

也许这样的东西会满足您的需求:

public class MyType
{
  public MyType(string s)
  {
    Property = s;
  }

  public string Property { get; set; }
}

public static class MyTypeExtensions
{
  public static object GetTestInstance(this Type t)
  {
    var ctorInjectedDependency = "blah";
    var ctorInfo = t.GetConstructor(new[]{typeof(string)});

    return ctorInfo.Invoke(new object[] {ctorInjectedDependency});
  }

  public static T GetTestInstance<T>(this Type t)
  {
    var ctorInjectedDependency = "blah";
    var ctorInfo = t.GetConstructor(new[] { typeof(string) });

    return (T)ctorInfo.Invoke(new object[] { ctorInjectedDependency });
  }
}

用法:

var my = typeof(MyType).GetTestInstance(); // maybe just object is enough
var my2 = typeof(MyType).GetTestInstance<MyType>();


Console.WriteLine((my as MyType).Property);
Console.WriteLine(my2.Property);

Maybe something like this will suit your needs:

public class MyType
{
  public MyType(string s)
  {
    Property = s;
  }

  public string Property { get; set; }
}

public static class MyTypeExtensions
{
  public static object GetTestInstance(this Type t)
  {
    var ctorInjectedDependency = "blah";
    var ctorInfo = t.GetConstructor(new[]{typeof(string)});

    return ctorInfo.Invoke(new object[] {ctorInjectedDependency});
  }

  public static T GetTestInstance<T>(this Type t)
  {
    var ctorInjectedDependency = "blah";
    var ctorInfo = t.GetConstructor(new[] { typeof(string) });

    return (T)ctorInfo.Invoke(new object[] { ctorInjectedDependency });
  }
}

Usage:

var my = typeof(MyType).GetTestInstance(); // maybe just object is enough
var my2 = typeof(MyType).GetTestInstance<MyType>();


Console.WriteLine((my as MyType).Property);
Console.WriteLine(my2.Property);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文