diff --git a/.gitignore b/.gitignore index ad3046a..42f9d16 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,9 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk +# ---> Visual Studio Code +.vscode/solution-explorer/ + # ---> VisualStudio ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. diff --git a/Study.DelegateSeries.Core/StudyDelegate.cs b/Study.DelegateSeries.Core/StudyDelegate.cs index 2d3f0eb..4a1fef3 100644 --- a/Study.DelegateSeries.Core/StudyDelegate.cs +++ b/Study.DelegateSeries.Core/StudyDelegate.cs @@ -23,13 +23,7 @@ namespace Study.DelegateSeries.Core * 10、总结 */ - #region 定义委托 - //基本语法 - public delegate decimal OperationDelegate(decimal a,decimal b); - - public delegate string ShowTypeNameDelegate(); - - public delegate string ShowNameDelegate(); + #region 定义委托,DeclareDelegate类 #endregion #region 实例化委托 @@ -38,9 +32,9 @@ namespace Study.DelegateSeries.Core /// 基础语法 /// (c#1.0 开始提供) /// - public OperationDelegate BaseInstance() + public CalculatorDelegate BaseInstance() { - OperationDelegate operation = new OperationDelegate(new Addition().Add); + CalculatorDelegate operation = new CalculatorDelegate(new Addition().Add); return operation; } @@ -48,9 +42,9 @@ namespace Study.DelegateSeries.Core /// c#2.0 简写方法 /// (基于类型推断) /// - public OperationDelegate AutoTypeInstance() + public CalculatorDelegate AutoTypeInstance() { - OperationDelegate operation = new Subtraction().Difference; + CalculatorDelegate operation = new Subtraction().Difference; return operation; } @@ -58,10 +52,10 @@ namespace Study.DelegateSeries.Core /// c#2.0 匿名委托 /// (使用匿名方法实例化的委托) /// - public OperationDelegate AnonymousInstance() + public CalculatorDelegate AnonymousInstance() { //c# 2.0基础语法 - OperationDelegate operation = delegate (decimal a,decimal b) { return (a * b) * (a * b); }; + CalculatorDelegate operation = delegate (decimal a,decimal b) { return (a * b) * (a * b); }; return operation; } @@ -69,14 +63,14 @@ namespace Study.DelegateSeries.Core /// c#3.0 Lambda表达式 /// (实质:"Lambda表达式"是一个匿名函数) /// - public OperationDelegate LambdaInstance() + public CalculatorDelegate LambdaInstance() { - OperationDelegate operation = (decimal a,decimal b)=> { return a * a + b * b + 2 * a * b; }; + CalculatorDelegate operation = (decimal a,decimal b)=> { return a * a + b * b + 2 * a * b; }; //自动推断 - OperationDelegate operation2 = (a,b) => { return a * a + b * b + 2 * a * b; }; + CalculatorDelegate operation2 = (a,b) => { return a * a + b * b + 2 * a * b; }; //{}只有一句时的简写 - OperationDelegate operation3 = (a, b) => a * a + b * b + 2 * a * b ; + CalculatorDelegate operation3 = (a, b) => a * a + b * b + 2 * a * b ; return operation; } diff --git a/Study.DelegateSeries.Test/StudyDelegateTest.cs b/Study.DelegateSeries.Test/StudyDelegateTest.cs new file mode 100644 index 0000000..600aea9 --- /dev/null +++ b/Study.DelegateSeries.Test/StudyDelegateTest.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Text; + +using Xunit; + +using Study.DelegateSeries.Core; +using Study.DelegateSeries.Core.Calculator; + +namespace Study.DelegateSeries.Test +{ + public class StudyDelegateTest : IDisposable, IClassFixture + { + #region 准备 + private StudyDelegate studyDelegate; + + public StudyDelegateTest(StudyDelegate _studyDelegate) + { + this.studyDelegate = _studyDelegate; + } + #endregion + + #region xUnit框架可能性测试 + [Fact] + public void FixtureTest() + { + Assert.NotNull(studyDelegate); + } + #endregion + + #region 实例化委托测试 + [Fact] + public void BaseInstanceTest() + { + var baseInstance = this.studyDelegate.BaseInstance(); + + Assert.NotNull(baseInstance); + Assert.True(baseInstance.Method.IsPublic); + } + + [Fact] + public void AutoTypeInstanceTest() + { + var baseInstance = this.studyDelegate.AutoTypeInstance(); + + Assert.NotNull(baseInstance); + Assert.Equal("Difference", baseInstance.Method.Name); + } + + [Fact] + public void AnonymousInstanceTest() + { + var baseInstance = this.studyDelegate.AnonymousInstance(); + + Assert.NotNull(baseInstance); + Assert.False(baseInstance.Method.IsSpecialName); + } + + [Fact] + public void LambdaInstanceTest() + { + var baseInstance = this.studyDelegate.LambdaInstance(); + + Assert.NotNull(baseInstance); + Assert.False(baseInstance.Method.IsSpecialName); + } + #endregion + + #region 清理 + void IDisposable.Dispose() + { + + } + #endregion + + } +} diff --git a/Study.DelegateSeries.Test/XUnitTest.cs b/Study.DelegateSeries.Test/XUnitTest.cs index 10c2699..0e6623d 100644 --- a/Study.DelegateSeries.Test/XUnitTest.cs +++ b/Study.DelegateSeries.Test/XUnitTest.cs @@ -9,12 +9,6 @@ namespace Study.DelegateSeries.Test [Fact] public void UseXunit() { - List array = new List(); - for (int i = 0; i < 10; i++) - { - array.Add(i.ToString()); - } - Assert.True(true, "ͨ"); } }