传递内存流&字符串构建器作为C#类的参数 [英] Pass Memory stream & string builder as parameters to C# class

查看:48
本文介绍了传递内存流&字符串构建器作为C#类的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

改述我的问题:



当前课程:



    public someclass(s​​tream 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆