예제 #1
0
        public void ConvertStringToInt(string enteredString, int number)
        {
            IProcessString processString = new ProcessString();

            var resultNumber = processString.ConvertStringToInt(enteredString);

            Assert.AreEqual(number, resultNumber);
        }
예제 #2
0
        // since C# added generics, we've been able to specify delegates in a more flexible
        // but less self-descriptive way
        // that is the types Func<...> and Action<...>

        // for example:
        // Func<string, int> = any method with 1 string param, returning int.
        // Func<string, int, bool> = any method with 1 string param and 1 int param, returning bool.
        // Action<string, int> = any method with 1 string param, and 1 int param, returning nothing (void).

        //static void ForEach(List<string> list, ProcessString action)
        static void ForEach(List <string> list, ProcessString action, Func <string, string> transform)
        {
            foreach (string item in list)
            {
                string transformed = transform(item);
                action(transformed);
            }
        }
예제 #3
0
        public static void ProcessFile(string filename, ProcessString processor)
        {
            var content = File.ReadAllLines(filename);

            var firstline = content.First(l => l.Length > 0);

            var line = content[0];

            processor(line);
        }
        public void GivenAString_WhenAllCharUnique_ThenReturnTrue_()
        {
            // Arrange
            ProcessString processString = new ProcessString();
            var           input         = string.Empty;

            // Act
            var result = processString.HasDuplicates(input);

            // Assert
            Assert.IsTrue(result);
        }
예제 #5
0
 protected void GenerateLineLoadString(int j, string xLoadStr, string yLoadStr, string zLoadStr, string oritationStr = null)
 {
     if (oritationStr == null)
     {
         ProcessString.Add(Template.Wires[j - 1] + " Fx= " + xLoadStr);
         ProcessString.Add(Template.Wires[j - 1] + " Fy= " + yLoadStr);
         ProcessString.Add(Template.Wires[j - 1] + " Fz= " + zLoadStr);
     }
     else
     {
         ProcessString.Add(Template.Wires[j - 1] + oritationStr + " Fx= " + xLoadStr);
         ProcessString.Add(Template.Wires[j - 1] + oritationStr + " Fy= " + yLoadStr);
         ProcessString.Add(Template.Wires[j - 1] + oritationStr + " Fz= " + zLoadStr);
     }
 }
예제 #6
0
        public void ExceptionShouldArise()
        {
            try
            {
                string         enteredString = "fdgdg";
                IProcessString processString = new ProcessString();

                var resultNumber = processString.ConvertStringToInt(enteredString);

                Assert.Fail("no exception thrown");
            }
            catch (StringCanNotBeConvertedToInt ex)
            {
                Assert.IsTrue(ex is StringCanNotBeConvertedToInt);
            }
        }
예제 #7
0
        private void btnDelegateTest_Click(object sender, EventArgs e)
        {
            List <string> foxRiver8 = new List <string>
            {
                "Michael",
                "Lincoln",
                "Sucre",
                "Abruzzi",
                "T-Bag",
                "C-Note",
                "Tweener",
                "Charles"
            };

            // 传统委托方式
            ProcessString p1 = new ProcessString(LowerIt);

            foreach (string name in foxRiver8)
            {
                listBox1.Items.Add(p1(name));
            }
            listBox1.Items.Add("----------------");

            // 匿名方法方式
            ProcessString p2 = delegate(string input)
            {
                return(input.ToLower());
            };

            foreach (string name in foxRiver8)
            {
                listBox1.Items.Add(p2(name));
            }
            listBox1.Items.Add("----------------");

            // Lambda 表达式方式
            //ProcessString p = (string input) => { return input.ToLower(); };
            //ProcessString p = input => { return input.ToLower(); };
            //ProcessString p = (string input) => input.ToLower();

            ProcessString p3 = abc => abc.ToUpper();

            foreach (string name in foxRiver8)
            {
                listBox1.Items.Add(p3(name));
            }
        }
        public void TestCheckPalindrome()
        {
            // Act
            bool expected1 = false;
            bool expected2 = false;
            bool expected3 = true;

            // Arrange
            bool actual1 = ProcessString.CheckPalindrome("newyork");
            bool actual2 = ProcessString.CheckPalindrome("someotherplace");
            bool actual3 = ProcessString.CheckPalindrome("neveroddoreven");

            // Assert
            Assert.AreEqual(expected1, actual1);
            Assert.AreEqual(expected2, actual2);
            Assert.AreEqual(expected3, actual3);
        }
        public void TestRemoveSpecialCharacters()
        {
            // Act
            string expected1 = "NewYork";
            string expected2 = "Someotherplace";
            string expected3 = "neverOddorEven";

            // Arrange
            string actual1 = ProcessString.RemoveSpecialCharacters("..!!New   York;[]");
            string actual2 = ProcessString.RemoveSpecialCharacters("Some, !other ..place");
            string actual3 = ProcessString.RemoveSpecialCharacters("never Odd, or Even.");

            // Assert
            Assert.AreEqual(expected1, actual1);
            Assert.AreEqual(expected2, actual2);
            Assert.AreEqual(expected3, actual3);
        }
        public void TestLowerCase()
        {
            // Act
            string expected1 = "newyork";
            string expected2 = "someotherplace";
            string expected3 = "neveroddoreven";

            // Arrange
            string actual1 = ProcessString.LowerCase("NewYork");
            string actual2 = ProcessString.LowerCase("Someotherplace");
            string actual3 = ProcessString.LowerCase("neverOddorEven");

            // Assert
            Assert.AreEqual(expected1, actual1);
            Assert.AreEqual(expected2, actual2);
            Assert.AreEqual(expected3, actual3);
        }
예제 #11
0
        /// <summary>
        /// 计算转向挂点
        /// </summary>
        /// <param name="i"></param>
        /// <param name="j"></param>
        protected void CalsTurningPointsLoad(int i, int j)
        {
            float angf = HPSettingParas.TurningPoints.Where(item => item.WireType == Template.Wires[j - 1]).First().Angle;

            //XXT[i, j - 1] = (float)(-ZZ[i, j - 1] * Math.Sin(angf * Math.PI / 180));
            //YYT[i, j - 1] = YY[i, j - 1];
            //ZZT[i, j - 1] = (float)(ZZ[i, j - 1] + ZZ[i, j - 1] * Math.Cos(angf * Math.PI / 180));

            //ProcessString.Add(Template.Wires[j - 1] + "转向处 Fx= " + ZZ[i, j - 1].ToString("0.00") + " x sin(" + angf + ") = " + XXT[i, j - 1].ToString("0.00"));
            //ProcessString.Add(Template.Wires[j - 1] + "转向处 Fy= " + YY[i, j - 1].ToString("0.00"));
            //ProcessString.Add(Template.Wires[j - 1] + "转向处 Fz= " + ZZ[i, j - 1].ToString("0.00") + " + " + ZZ[i, j - 1].ToString("0.00") + " x cos(" + angf + ") = " + ZZ[i, j - 1].ToString("0.00"));

            XXT[i, j - 1] = formula.TPTuringX(ZZ[i, j - 1], angf, out string strTX);
            YYT[i, j - 1] = formula.TPTuringY(YY[i, j - 1], out string strTY);
            ZZT[i, j - 1] = formula.TPTuringZ(ZZ[i, j - 1], angf, out string strTZ);

            ProcessString.Add(Template.Wires[j - 1] + "转向处 Fx= " + strTX);
            ProcessString.Add(Template.Wires[j - 1] + "转向处 Fy= " + strTY);
            ProcessString.Add(Template.Wires[j - 1] + "转向处 Fz= " + strTZ);

            //float fzz = XX[i, j - 1];
            //float fzz1 = ZZ[i, j - 1];
            //XX[i, j - 1] = XX[i, j - 1] + ZZ[i, j - 1] * (float)Math.Sin(angf * Math.PI / 180);
            //YY[i, j - 1] = YY[i, j - 1];
            //ZZ[i, j - 1] = ZZ[i, j - 1] - ZZ[i, j - 1] * (float)Math.Cos(angf * Math.PI / 180);

            //ProcessString.Add(Template.Wires[j - 1] + "导线处 Fx= " + fzz.ToString("0.00") + " + " + fzz1.ToString("0.00") + " x sin(" + angf + ") = " + XX[i, j - 1].ToString("0.00"));
            //ProcessString.Add(Template.Wires[j - 1] + "导线处 Fy= " + YY[i, j - 1].ToString("0.00"));
            //ProcessString.Add(Template.Wires[j - 1] + "导线处 Fz= " + fzz1.ToString("0.00") + " - " + fzz1.ToString("0.00") + " x cos(" + angf + ") = " + ZZ[i, j - 1].ToString("0.00"));


            XX[i, j - 1] = formula.TPWireX(XX[i, j - 1], ZZ[i, j - 1], angf, out string strWX);
            YY[i, j - 1] = formula.TPWireY(YY[i, j - 1], out string strWY);
            ZZ[i, j - 1] = formula.TPWireZ(ZZ[i, j - 1], angf, out string strWZ);

            ProcessString.Add(Template.Wires[j - 1] + "导线处 Fx= " + strWX);
            ProcessString.Add(Template.Wires[j - 1] + "导线处 Fy= " + strWY);
            ProcessString.Add(Template.Wires[j - 1] + "导线处 Fz= " + strWZ);
        }
예제 #12
0
 public static string ReadLinesUntilConditionIsMet(TextReader sr, List<string> list, ProcessString proc, ConditionOnString cond)
 {
     string line = sr.ReadLine();
     while (!cond(line))
     {
         list.Add(proc(line));
         line = sr.ReadLine();
     }
     return line;
 }
        public override void CalculateLoadDistribute(string path)
        {
            int calNums = Template.WorkConditionCombos.Count;

            XX = new float[calNums, Template.Wires.Count];
            YY = new float[calNums, Template.Wires.Count];
            ZZ = new float[calNums, Template.Wires.Count];

            XXT = new float[calNums, Template.Wires.Count];
            YYT = new float[calNums, Template.Wires.Count];
            ZZT = new float[calNums, Template.Wires.Count];

            int i = -1, j = 1;
            int count = 0;

            ProcessString.Add(Template.Name + " " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));

            foreach (var wd in Template.WorkConditionCombos)
            {
                i++;
                if (!wd.IsCalculate)
                {
                    continue;
                }

                count++;
                ProcessString.Add("");
                ProcessString.Add("No." + count.ToString() + " " + wd.WorkComment);

                j = 1;
                foreach (int wic in wd.WireIndexCodes)
                {
                    LineParas = LineParasArr[j - 1];
                    formula.SetStrLineParas(LineParas);

                    int zhs = wic;

                    switch (wd.WorkConditionCode)
                    {
                    case "N1":
                    case "N2":
                    case "D1":
                    case "D2":
                        DistributeInWindAndLowTemperature(i, j);
                        break;

                    case "I1":
                    case "I2":
                    case "U1":
                    case "U2":
                    case "B1":
                    case "B2":
                    case "Y1":
                    case "Y2":
                        DistributeInIce(i, j);
                        break;

                    case "T":
                        DistributeInJump(i, j);
                        break;

                    case "L":
                    case "La":
                    case "Lb":
                    case "Lc":
                    case "Ld":
                    case "Le":
                    case "Lf":
                    case "Lg":
                    case "Lh":
                        DistributeInLift(i, j);
                        break;

                    case "G":
                    case "Ga":
                    case "Gb":
                    case "Gc":
                    case "Gd":
                    case "Ge":
                    case "Gf":
                    case "Gg":
                    case "Gh":
                    case "GLa":
                    case "GLb":
                    case "GLc":
                    case "GLd":
                    case "GLe":
                    case "GLf":
                    case "GLg":
                    case "GLh":
                        DistributeInHuache(i, j);
                        break;

                    case "C":
                    case "Ca":
                    case "Cb":
                    case "Cc":
                    case "Cd":
                    case "Ce":
                    case "Cf":
                    case "Cg":
                    case "Ch":
                    case "CLa":
                    case "CLb":
                    case "CLc":
                    case "CLd":
                    case "CLe":
                    case "CLf":
                    case "CLg":
                    case "CLh":
                        DistributeInTixian(i, j);
                        break;

                    default:
                        throw new Exception("直线塔工况代号超出范围" + "0 + 16" + "错误:1-230");
                        //break;
                    }
                    j++;
                }
            }

            FileUtils.TextSaveByLine(path, ProcessString);
        }
예제 #14
0
        private List <string> BuildListFromNode(XmlNode itemDefGroupNode, String xPath, ProcessString process = null)
        {
            var node = itemDefGroupNode.SelectSingleNode(xPath, _nsMan);

            if (node != null)
            {
                var list = new List <string>(node.InnerText.Split(';'));
                if (process != null)
                {
                    list = list.Select(i => process(i)).ToList();
                }
                return(list);
            }
            else
            {
                return(null);
            }
        }
예제 #15
0
        public void Delegate_constructor()
        {
            ProcessString allAs = new ProcessString(AllAs);

            Assert.That(allAs("abc"), Is.EqualTo("aaa"));
        }
        /// <summary>
        /// 针对脱冰跳跃工况
        /// 工况代码"T"
        /// </summary>
        /// <param name="i"></param>
        /// <param name="j"></param>
        protected void DistributeInJump(int i, int j)
        {
            float x1, y1, y2, z1, z2, z3, z4;
            float zjiao;

            WorkConditionCombo wd = Template.WorkConditionCombos[i];

            int    zhs = wd.WireIndexCodes[j - 1], zhsAM;
            int    angle             = wd.WindDirectionCode;
            string workConditionCode = wd.WorkConditionCode;

            if (Math.Abs(zhs) <= workConditionNum && Math.Abs(zhs) > 0)
            {
                //正常覆冰相
                if (zhs > 0)
                {
                    if (wd.TensionAngleCode == "D")
                    {
                        x1    = Wind[j, zhs];
                        zjiao = LineParas.AngleMax;
                    }
                    //else if( wd.TensionAngleCode == "X" )
                    else
                    {
                        x1    = Windx[j, zhs];
                        zjiao = LineParas.AngleMin;
                    }
                    y1 = TensionMax[j, zhs];
                    y2 = TensionMin[j, zhs];
                    z1 = GMax[j, zhs];
                    z2 = GMax[j, 1];
                    z3 = GMin[j, zhs];
                    z4 = GMin[j, 1];
                }
                else
                {
                    if (wd.TensionAngleCode == "D")
                    {
                        x1    = Wind[j, Math.Abs(zhs)];
                        zjiao = LineParas.AngleMax;
                    }
                    //else if( wd.TensionAngleCode == "X" )
                    else
                    {
                        x1    = Windx[j, Math.Abs(zhs)];
                        zjiao = LineParas.AngleMin;
                    }
                    y1 = TensionMin[j, Math.Abs(zhs)];
                    y2 = TensionMax[j, Math.Abs(zhs)];
                    z1 = GMax[j, Math.Abs(zhs)];
                    z2 = GMax[j, 1];
                    z3 = GMin[j, Math.Abs(zhs)];
                    z4 = GMin[j, 1];
                }


                //j从1开始计数,但是XX YY ZZ 从0开始
                XX[i, j - 1] = ((FormulaLineLineCornerTower)formula).ZZTX(angle, x1, y1, y2, zjiao, out string strX);
                YY[i, j - 1] = ((FormulaLineLineCornerTower)formula).ZZTY(angle, x1, y1, y2, zjiao, out string strY);
                ZZ[i, j - 1] = ((FormulaLineLineCornerTower)formula).ZZTZ2(z2, z1, out string strZ);

                GenerateLineLoadString(j, strX, strY, strZ);
            }
            else if (Math.Abs(zhs) > 1000)
            {
                zhsAM = Math.Abs(zhs) % 1000;

                //脱冰跳跃相

                if (zhs > 1000)
                {
                    if (zhsAM > workConditionNum)
                    {
                        throw new Exception("第 " + i + " 工况,第 " + j + " 线条组合参数错误" + "0 + 16 " + "错误:1-220");
                    }

                    if (wd.TensionAngleCode == "D")
                    {
                        x1    = Wind[j, zhsAM];
                        zjiao = LineParas.AngleMax;
                    }
                    //else if( wd.TensionAngleCode == "X" )
                    else
                    {
                        x1    = Windx[j, zhsAM];
                        zjiao = LineParas.AngleMin;
                    }
                    y1 = TensionMax[j, zhsAM];
                    y2 = TensionMin[j, zhsAM];
                    z1 = GMax[j, zhsAM];
                    z2 = GMax[j, 1];
                    z3 = GMin[j, zhsAM];
                    z4 = GMin[j, 1];
                }
                else
                {
                    if (zhsAM > workConditionNum)
                    {
                        throw new Exception("第 " + i + " 工况,第 " + j + " 线条组合参数错误" + "0 + 16 " + "错误:1-221");
                    }

                    if (wd.TensionAngleCode == "D")
                    {
                        x1    = Wind[j, zhsAM];
                        zjiao = LineParas.AngleMax;
                    }
                    //else if( wd.TensionAngleCode == "X" )
                    else
                    {
                        x1    = Windx[j, zhsAM];
                        zjiao = LineParas.AngleMin;
                    }

                    y1 = TensionMin[j, zhsAM];
                    y2 = TensionMax[j, zhsAM];
                    z1 = GMax[j, zhsAM];
                    z2 = GMax[j, 1];
                    z3 = GMin[j, zhsAM];
                    z4 = GMin[j, 1];
                }

                XX[i, j - 1] = ((FormulaLineLineCornerTower)formula).ZZTX(angle, x1, y1, y2, zjiao, out string strX);
                YY[i, j - 1] = ((FormulaLineLineCornerTower)formula).ZZTY(angle, x1, y1, y2, zjiao, out string strY);
                ZZ[i, j - 1] = ((FormulaLineLineCornerTower)formula).ZZTZ1(z4, z3, out string strZ);

                ProcessString.Add(Template.Wires[j - 1] + " Fx= " + strX);
                ProcessString.Add(Template.Wires[j - 1] + " Fy= " + strY);
                ProcessString.Add(Template.Wires[j - 1] + " Fz= " + strZ);
            }
            else if (zhs == 0)
            {
                //j从1开始计数,但是XX YY ZZ 从0开始
                XX[i, j - 1] = 0;
                YY[i, j - 1] = 0;
                ZZ[i, j - 1] = 0;

                GenerateLineLoadString(j, "0.00", "0.00", "0.00");
            }
        }
예제 #17
0
        public void Named_method()
        {
            ProcessString reverse = Reverse;

            Assert.That(reverse("abc"), Is.EqualTo("cba"));
        }
 public void SetUp()
 {
     _squareBracketsProcessor = new SquareBracketProcessor();
     _roundBracketsProcessor  = new RoundBracketsProcessor(_squareBracketsProcessor);
     _processString           = new ProcessString(_roundBracketsProcessor);
 }
        /// <summary>
        /// 针对施工提线工况,只考虑最大垂荷,多用于直流输电塔
        /// 工况代码"C", "Ca", "Cb", "Cc", "Cd", "Ce", "Cf", "Cg", "Ch", "CLa", "CLb", "CLc", "CLd", "CLe", "CLf", "CLg", "CLh"
        /// </summary>
        /// <param name="i"></param>
        /// <param name="j"></param>
        protected void DistributeInTixian(int i, int j)
        {
            float x1, y1, y2, z1, z2;
            int   fuhao;
            float fhn;
            float deta1, deta2, deta3;

            WorkConditionCombo wd = Template.WorkConditionCombos[i];

            int    zhs = wd.WireIndexCodes[j - 1], zhsAM;
            int    angle             = wd.WindDirectionCode;
            string workConditionCode = wd.WorkConditionCode;
            float  fh = LineParas.WireExtraLoad / Paras.LoadRatio;

            if (Math.Abs(zhs) < workConditionNum && Math.Abs(zhs) > 0)
            {
                //已安装
                if (zhs <= workConditionNum && zhs > 0)
                {
                    y1 = TensionMax[j, Math.Abs(zhs)];
                    y2 = TensionMin[j, Math.Abs(zhs)];
                }
                else
                {
                    y1 = TensionMin[j, Math.Abs(zhs)];
                    y2 = TensionMax[j, Math.Abs(zhs)];
                }

                x1 = Wind[j, Math.Abs(zhs)];
                z1 = GMax[j, Math.Abs(zhs)];
                z2 = GMin[j, Math.Abs(zhs)];

                //j从1开始计数,但是XX YY ZZ 从0开始
                XX[i, j - 1] = ((FormulaLineTower)formula).ZXLX(angle, x1, out string strX);
                YY[i, j - 1] = ((FormulaLineTower)formula).ZXLY(angle, x1, y1, y2, out string strY);
                ZZ[i, j - 1] = ((FormulaLineTower)formula).ZXLZ1(z1, out string strZ);

                GenerateLineLoadString(j, strX, strY, strZ);
            }
            else if (Math.Abs(zhs) > 100 && Math.Abs(zhs) < 1000)
            {
                //已锚相
                if (zhs > 0)
                {
                    fuhao = 1;
                }
                else
                {
                    fuhao = -1;
                }

                fhn   = Math.Abs(zhs) / 100;
                zhsAM = Math.Abs(zhs) % 100;

                if (zhsAM > workConditionNum)
                {
                    throw new Exception("第 " + i + " 工况,第 " + j + " 线条组合参数错误" + "0 + 16 " + "错误:1-215");
                }

                y1 = TensionMax[j, zhsAM];
                y2 = TensionMin[j, zhsAM];

                if (j <= earthWireNum && y1 >= LineParas.AnchorTension)
                {
                    //地线有开段时
                    deta1 = y1;
                    deta2 = 90;
                    deta3 = 0;
                }
                else
                {
                    //地线不开段和导线
                    deta1 = LineParas.AnchorTension;
                    deta2 = Paras.AnchorAngle;
                    deta3 = Paras.AnchorAngle;
                }

                x1 = Wind[j, zhsAM];
                z1 = GMax[j, zhsAM];
                z2 = GMin[j, zhsAM];

                //j从1开始计数,但是XX YY ZZ 从0开始
                XX[i, j - 1] = ((FormulaLineTower)formula).ZXMX2(angle, x1, out string strX);
                YY[i, j - 1] = ((FormulaLineTower)formula).ZXMY2(angle, x1, deta1 * fuhao, deta2, out string strY);
                ZZ[i, j - 1] = ((FormulaLineTower)formula).ZXMZ2(z1, fh, fhn, deta1, deta3, out string strZ);

                GenerateLineLoadString(j, strX, strY, strZ);
            }
            else if (Math.Abs(zhs) > 1000)
            {
                //施工提线相
                if (Math.Abs(zhs) < 10000)
                {
                    fhn = Math.Abs(zhs) / 1000;
                }
                else
                {
                    fhn = (Math.Abs(zhs) - 10000) / 1000;
                }

                zhsAM = Math.Abs(zhs) % 1000;

                if (zhsAM > workConditionNum)
                {
                    throw new Exception("第 " + i + " 工况,第 " + j + " 线条组合参数错误" + "0 + 16 " + "错误:1-216");
                }

                x1 = Wind[j, zhsAM];
                z1 = GMax[j, zhsAM];
                z2 = GMin[j, zhsAM];

                //j从1开始计数,但是XX YY ZZ 从0开始
                XX[i, j - 1] = ((FormulaLineTower)formula).ZXCX(angle, x1, out string strX);
                YY[i, j - 1] = ((FormulaLineTower)formula).ZXCY(angle, x1, LineParas.HoistingCoef, z1, out string strY);
                ZZ[i, j - 1] = ((FormulaLineTower)formula).ZXCZ(fh, fhn, LineParas.HoistingCoef, z1, out string strZ);

                GenerateLineLoadString(j, strX, strY, strZ);

                //工况代号大于10000,并且选择了转向挂点
                if (Math.Abs(zhs) > 10000 && HPSettingParas.IsTuringPointSeleced == true)
                {
                    CalsTurningPointsLoad(i, j);
                }
            }
            else if (zhs == 0)
            {
                //j从1开始计数,但是XX YY ZZ 从0开始
                XX[i, j - 1] = 0;
                YY[i, j - 1] = 0;
                ZZ[i, j - 1] = 0;

                ProcessString.Add(Template.Wires[j - 1] + " Fx= " + "0.00");
                ProcessString.Add(Template.Wires[j - 1] + " Fy= " + "0.00");
                ProcessString.Add(Template.Wires[j - 1] + " Fz= " + "0.00");
            }
        }
예제 #20
0
        public void Lambda()
        {
            ProcessString minusculise = input => input.ToLowerInvariant();

            Assert.That(minusculise("ABC"), Is.EqualTo("abc"));
        }
예제 #21
0
        public void Anonymous_function()
        {
            ProcessString capitalise = delegate(string input) { return(input.ToUpperInvariant()); };

            Assert.That(capitalise("abc"), Is.EqualTo("ABC"));
        }
예제 #22
0
        static void Main(string[] args)
        {
            // Print() calls the method, Print refers to the method

            ProcessString action2 = Console.Write;

            var list = new List <string> {
                "one", "two"
            };

            ForEach(list, Print, (string str) => { return(str.ToUpper()); });
            ForEach(list, action2, str => str[0].ToString());

            // lambda expression is a shorthand syntax to write a "anonymous method"
            // this is for quick "disposable" functions

            // the best/most useful application of lambda expression / delegate types etc
            // is a part of the base class library called LINQ - stands for Language Integrated Query
            // - there's two ways to write it, one is weird and looks like SQL, called "query syntax"
            // - the other is called method syntax

            // calculate the average length of the strings in the list
            double average = list.Average(s => s.Length);

            // c# has a feature called "extension methods"
            // basically, you can write a static method in a static class, and tell C# to pretend that it's actually
            //   defined on the class of one of its first parameter.
            // it's "syntactic sugar" but does LOOK like you get to modify any class in C#
            // and add your own methods.

            char a = "abc".FirstCharacter();

            // LINQ is just a big pile of overloaded extension methods defined for the IEnumerable<T> interface

            // three types of LINQ methods
            // 1. the ones that return a new IEnumerable collection (they never modify the original collection)
            //    they don't execute "yet" - they use deferred execution.
            // 2. the ones that return any concrete value - like Average, First- do not use deferred execution
            // 3. things like ToArray, ToList. these return collections that need to be "all there"
            //     so they also don't use deferred execution.
            //    "ToList" lets you effectively force execution of type-1 methods whenever you want.

            // - Select: maps each element to something new
            // - Where: filters the collection according to some condition

            // - Distinct: filters out duplicates
            // - Skip: skips n elements
            // - Take: skips AFTER n elements

            // - Count: counts how many items (match a condition)
            // - First: returns the first item (matching a condition)

            Action <string> print = (s =>
            {
                Console.WriteLine(s);
            }); // you can use "block" syntax with lambda expressions as well as the "expression" syntax

            IEnumerable <string> noNulls = list.Where(x => x != null);
            var lastChar = noNulls.Select(x => x[x.Length - 1]);
            var upper    = lastChar.Select(c => c.ToString().ToUpper());

            // no processing of the list has happened yet

            string first = upper.First(); // we get the first element of that new collection...
            // it processed the first item but hasn't looked at the others yet.

            List <string> list2 = upper.ToList(); // now it has processed all of the items.
            // because you have to be able to access any part of a List (not true of generic IEnumerable)
            //.... it has to process all the elements
        }
예제 #23
0
        public void Delegate_constructor()
        {
            ProcessString allAs = new ProcessString(AllAs);

            Assert.That(allAs("abc"), Is.EqualTo("aaa"));
        }
예제 #24
0
        public static string ReadLinesUntilConditionIsMet(TextReader sr, List <string> list, ProcessString proc, ConditionOnString cond)
        {
            string line = sr.ReadLine();

            while (!cond(line))
            {
                list.Add(proc(line));
                line = sr.ReadLine();
            }
            return(line);
        }
예제 #25
0
        private void OnExit(object state)
        {
            if (restart)
            {
                Reset();
                ProcessCallback(this);
                return;
            }

            bool isError;

            try
            {
                // An error occurred!
                if (gitCommand != null && gitCommand.ExitCode != 0)
                {
                    isError = true;

                    // TODO: This Plink stuff here seems misplaced. Is there a better
                    // home for all of this stuff? For example, if I had a label called pull,
                    // we could end up in this code incorrectly.
                    if (Plink)
                    {
                        if (ProcessArguments.ToLower().Contains("pull") || ProcessArguments.ToLower().Contains("push") || ProcessArguments.ToLower().Contains("plink") || ProcessArguments.ToLower().Contains("tortoiseplink") || ProcessArguments.ToLower().Contains("remote") || ProcessString.ToLower().Contains("clone") || ProcessArguments.ToLower().Contains("clone"))
                        {
                            if (OutputString.ToString().Contains("successfully authenticated"))
                            {
                                isError = false;
                            }

                            if (OutputString.ToString().Contains("FATAL ERROR") && OutputString.ToString().Contains("authentication"))
                            {
                                var puttyError = new FormPuttyError();
                                puttyError.ShowDialog();
                                if (puttyError.RetryProcess)
                                {
                                    Reset();
                                    ProcessCallback(this);
                                    return;
                                }
                            }
                            if (OutputString.ToString().ToLower().Contains("the server's host key is not cached in the registry") && !string.IsNullOrEmpty(UrlTryingToConnect))
                            {
                                if (MessageBox.Show("The server's host key is not cached in the registry.\n\nDo you want to trust this host key and then try again?", "SSH", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                                {
                                    GitCommandHelpers.RunRealCmdDetached(
                                        "cmd.exe",
                                        string.Format("/k \"\"{0}\" -T \"{1}\"\"", Settings.Plink, UrlTryingToConnect));

                                    Reset();
                                    ProcessCallback(this);
                                    return;
                                }
                            }
                        }
                    }
                }
                else
                {
                    isError = false;
                }
            }
            catch
            {
                isError = true;
            }

            Done(!isError);
        }