我怎样才能得到一个扩展方法来改变原来的对象? [英] How can I get an extension method to change the original object?

查看:171
本文介绍了我怎样才能得到一个扩展方法来改变原来的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够写的扩展方法,这样我可以说:

  lines.ForceSpaceGroupsToBeTabs();

而不是:

  =行lines.ForceSpaceGroupsToBeTabs();

但是,下面的code当前输出:

 ··1
........二

而不是:

 
TTtwo

什么我有以下code键更改,使其输出:

 
TTtwo

(请注意,知名度, =空间,T = \\ t ):

 使用系统;
使用System.Collections.Generic;命名空间TestExtended82343
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            清单<串GT;行=新的List<串GT;();
            lines.Add(... 1);
            lines.Add(........二);            lines.ForceSpaceGroupsToBeTabs();            lines.ForEach(L => Console.WriteLine(1));
            到Console.ReadLine();
        }
    }    公共静态类助手
    {
        公共静态无效ForceSpaceGroupsToBeTabs(名单<串GT; originalLines)
        {
            串空间群=新的String(4'。');
            清单<串GT;行=新的List<串GT;();
            的foreach(在originalLines VAR originalLine)
            {
                lines.Add(originalLine.Replace(空间群,T));
            }
            originalLines =线;
        }
    }
}


解决方案

您必须修改的内容列表<串> 传递给扩展方法,而不是变量持有参考到列表:

 公共静态无效ForceSpaceGroupsToBeTabs(名单<串GT;线条)
{
    串空间群=新的String(4'。');
    的for(int i = 0; I< lines.Count;我++)
    {
        线[I] =行[I] .Replace(空间群,T);
    }
}

I want to be able to write extension methods so that I can say:

lines.ForceSpaceGroupsToBeTabs();

instead of:

lines = lines.ForceSpaceGroupsToBeTabs();

However, the following code currently outputs:

....one
........two

instead of:

Tone
TTtwo

What do I have to change in the following code to make it output:

Tone
TTtwo

(note that for visibility, . = space, T = \t):

using System;
using System.Collections.Generic;

namespace TestExtended82343
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> lines = new List<string>();
            lines.Add("....one");
            lines.Add("........two");

            lines.ForceSpaceGroupsToBeTabs();

            lines.ForEach(l => Console.WriteLine(l));
            Console.ReadLine();
        }
    }

    public static class Helpers
    {
        public static void ForceSpaceGroupsToBeTabs(this List<string> originalLines)
        {
            string spaceGroup = new String('.', 4);
            List<string> lines = new List<string>();
            foreach (var originalLine in originalLines)
            {
                lines.Add(originalLine.Replace(spaceGroup, "T"));
            }
            originalLines = lines;
        }
    }
}

解决方案

You have to modify the contents of the List<string> passed to the extension method, not the variable that holds the reference to the list:

public static void ForceSpaceGroupsToBeTabs(this List<string> lines)
{
    string spaceGroup = new String('.', 4);
    for (int i = 0; i < lines.Count; i++)
    {
        lines[i] = lines[i].Replace(spaceGroup, "T");
    }
}

这篇关于我怎样才能得到一个扩展方法来改变原来的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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