本文整理汇总了C#中Chain.Execute方法的典型用法代码示例。如果您正苦于以下问题:C# Chain.Execute方法的具体用法?C# Chain.Execute怎么用?C# Chain.Execute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Chain
的用法示例。
在下文中一共展示了Chain.Execute方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public static void Run()
{
Chain<int, double> chain = new Chain<int, double>()
.Link<int>(x =>
{
// Generate random array of ints
int[] nums = new int[x];
Random rand = new Random();
for (int i = 0; i < x; i++)
{
nums[i] = rand.Next(100);
}
return nums;
})
.Link<int[]>(x =>
{
// order the numbers (pointless but why not)
return x.OrderBy(y => y).ToArray();
})
.Link<int[]>(x =>
{
// compute the average and return it
double average = 0.0;
for (int i = 0; i < x.Length; i++)
{
average += x[i];
}
return average / x.Length;
});
// Execute our chain, passing in 100 as the initial argument
double avg = chain.Execute(100);
Console.WriteLine("Average int chain finished, result: " + avg);
}
示例2: TestExecutionError
public void TestExecutionError()
{
Chain<int, int> chain = new Chain<int, int>()
.Link<int>(x => x)
.Link<int>(x =>
{
if (x == 0)
throw new NullReferenceException(x.ToString());
else
return x;
})
.Link<int>(x => 100);
ChainExecutionException chEE = null;
try
{
int outcome = chain.Execute(0);
}
catch (ChainExecutionException chee)
{
chEE = chee;
}
Assert.IsNotNull(chEE);
Assert.AreEqual<string>("0", chEE.InnerException.Message);
}
示例3: TestMethod1
public void TestMethod1()
{
var chain = new Chain<ICommand>()
.Add<Command1>()
.Add<Command2>(() => new Command2("Test"))
.Build();
chain.Execute();
chain = new Chain<ICommand>()
.Add<Command2>()
.Add<Command1>()
.Build();
chain.Execute();
}
示例4: Diag
private static void Diag(Chain<string, double> chain, int loops, int iters, string input)
{
for (int loop = 0; loop < loops; loop++)
{
Stopwatch swChain = Stopwatch.StartNew();
for (int i = 0; i < iters; i++)
{
chain.Execute(input);
}
swChain.Stop();
Stopwatch swLin = Stopwatch.StartNew();
for (int i = 0; i < iters; i++)
{
LinearImp(input);
}
swLin.Stop();
Console.WriteLine("Chain (" + loop + "): " + swChain.ElapsedMilliseconds);
Console.WriteLine("Linear (" + loop + "): " + swLin.ElapsedMilliseconds);
Console.WriteLine();
}
}
示例5: TestMultiTypeChain
public void TestMultiTypeChain()
{
Chain<int, string> chain = new Chain<int, string>()
.Link<int>(x => Enumerable.Range(1, x).ToArray())
.Link<int[]>(x => x.Sum())
.Link<int>(x => x.ToString());
string output = chain.Execute(5);
Assert.AreEqual<string>("15", output);
}
示例6: TestTypeMismatch
public void TestTypeMismatch()
{
Chain<int, int> chain = new Chain<int, int>();
chain.Link<int>(x => x + 1)
.Link<string>(x => x)
.Link<int>(x => x);
LinkArgumentException laErr = null;
try
{
int output = chain.Execute(0);
}
catch (LinkArgumentException lae)
{
laErr = lae;
}
Assert.IsNotNull(laErr);
}