传递内存流&字符串构建器作为C#类的参数 [英] Pass Memory stream & string builder as parameters to C# class
问题描述
改述我的问题:
当前课程:
  public someclass(stream Stream,stringbuilder builder)
  {
    stream = new stream;
    builder = new StringBuilder();
  } b
所以它不可模仿且可单元测试。我希望实现的是通过流和& stringbuilder作为此类的依赖项,以便构造函数看起来像这样:
$
  public someclass(IStreamProvider streamprovider,IStringbuilderProvider builderProvider)
  {
    stream = streamProvider.GetStream();
    builder = builderprovider.GetBuilder();
  } b
稍后我可以模拟IStreamProvider和IStringbuilderProvider并设置相同的Get方法。
是这种方法对吗?有什么建议 ?
Rephrasing my question :
current class :
public someclass(stream Stream, stringbuilder builder)
{
stream = new stream;
builder = new StringBuilder ();
}
So it is not mockable and unit testable. What i wish to achieve is that pass both stream & stringbuilder as dependencies to this class so that the constructor looks something like this :
public someclass(IStreamProvider streamprovider, IStringbuilderProvider builderProvider)
{
stream = streamProvider.GetStream();
builder = builderprovider.GetBuilder();
}
Later on I can mock IStreamProvider and IStringbuilderProvider and setup Get methods on the same.
Is this approach correct ? Any suggestions ?
推荐答案
你可以尝试这个方法:
You may try this method:
using System;
using System.Text;
class Program
{
static void Main()
{
string[] items = { "Cat", "Dog", "Celebrity" };
StringBuilderbuilder2 = new StringBuilder(
"These items are required:").AppendLine();
foreach(string item in items)
{
builder2.Append(item).AppendLine();
}
Console.WriteLine(builder2.ToString());
Console.ReadLine();
}
}
Output
These items are required:
Cat
Dog
Celebrity
这篇关于传递内存流&字符串构建器作为C#类的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!