• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Func类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中Func的典型用法代码示例。如果您正苦于以下问题:C# Func类的具体用法?C# Func怎么用?C# Func使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



Func类属于命名空间,在下文中一共展示了Func类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: BuildFactoryForService

		private Func<object> BuildFactoryForService(Type type)
		{
			if (type.IsClass)
			{
				foreach (var ctor in type.GetConstructors().Where(it => it.IsPublic))
				{
					var ctorParams = ctor.GetParameters();
					if (ctorParams.Length == 0)
						return () => Activator.CreateInstance(type);
					if (ctorParams.Length == 1)
					{
						if (ctorParams[0].ParameterType == typeof(IServiceProvider)
							|| ctorParams[0].ParameterType == typeof(IObjectFactory))
							return () => Activator.CreateInstance(type, this);
					}
					var argFactories = new Func<object>[ctorParams.Length];
					for (int i = 0; i < ctorParams.Length; i++)
					{
						var arg = GetFactory(ctorParams[i].ParameterType);
						if (arg == null)
							return null;
						argFactories[i] = arg;
					}
					return () =>
					{
						var args = new object[argFactories.Length];
						for (int i = 0; i < argFactories.Length; i++)
							args[i] = argFactories[i]();
						return Activator.CreateInstance(type, args);
					};
				}
			}
			return null;
		}
开发者ID:dstimac,项目名称:revenj,代码行数:34,代码来源:DryIocObjectFactory.cs


示例2: DepthFirstSearchTest

        public void DepthFirstSearchTest()
        {
            Func<Vertex[], int, bool[]>[] functions = new Func<Vertex[], int, bool[]>[]
            {
                DepthFirstSearchTestClass.RunDepthFirstSearch,
                DepthFirstSearchTestClass.RunBreadthFirstSearch,
                DepthFirstSearchTestClass.RunBellmanFord,
                DepthFirstSearchTestClass.RunDjikstra,
                DepthFirstSearchTestClass.RunFloydWarshall,
            };

            Vertex[] vertices = new Vertex[10];
            for (int i = 0; i < vertices.Length; i++)
                vertices[i] = new Vertex();

            for (int i = 0; i <= vertices.Length * (vertices.Length - 1); i++)
            {
                for (int j = 0; j < vertices.Length; j++)
                {
                    bool[][] results = new bool[vertices.Length][];

                    for (int k = 0; k < functions.Length; k++)
                    {
                        foreach (Vertex vertex in vertices)
                            vertex.Reset();

                        results[k] = functions[k](vertices, j);
                        Assert.IsTrue(ArrayUtilities.AreEqual(results[0], results[k]));
                    }
                }

                GraphUtilities.SetRandomEdge(vertices);
            }
        }
开发者ID:mmoroney,项目名称:Algorithms,代码行数:34,代码来源:DepthFirstSearchTest.cs


示例3: ReverseSingleSublistTest

        public void ReverseSingleSublistTest()
        {
            Func<ListNode<int>, int, int, ListNode<int>>[] functions = new Func<ListNode<int>, int, int, ListNode<int>>[]
            {
                ReverseSingleSublist.BruteForce,
                ReverseSingleSublist.SinglePass
            };

            for(int i = 0; i < 10; i++)
            {
                int[] data = ArrayUtilities.CreateRandomArray(10, 0, 10);
                for (int start = 1; start <= 10; start++)
                {
                    for(int end = start; end <= 10; end++)
                    {
                        ListNode<int>[] results = new ListNode<int>[functions.Length];

                        for (int j = 0; j < results.Length; j++)
                        {
                            ListNode<int> head = LinkedListUtilities.Initialize(data);
                            results[j] = functions[j](head, start, end);
                            Assert.IsTrue(LinkedListUtilities.AreEqual(results[0], results[j]));
                        }
                    }
                }
            }
        }
开发者ID:mmoroney,项目名称:Algorithms,代码行数:27,代码来源:ReverseSingleSublist.cs


示例4: Type

        public static ParameterSpecification Type(this ParameterSpecification @this, Func<ParameterInfo, TypeSpecification, TypeSpecification> typeSpecification)
        {
            Guard.NotNull(@this, "this");
            Guard.NotNull(typeSpecification, "typeSpecification");

            return @this.Combine(p => typeSpecification(p, TypeSpecificationBuilder.Any)(p.ParameterType));
        }
开发者ID:iSynaptic,项目名称:iSynaptic.Commons,代码行数:7,代码来源:ParameterSpecificationBuilder.cs


示例5: FirstMissingElementTest

        public void FirstMissingElementTest()
        {
            Func<int[], int>[] functions = new Func<int[], int>[]
            {
                FirstMissingElement.BruteForce,
                FirstMissingElement.Iterative
            };

            for(int i = 0; i < 10; i++)
            {
                int[][] data = new int[functions.Length][];
                int[] original = ArrayUtilities.CreateRandomArray(20, -10, 10);

                for (int j = 0; j < functions.Length; j++)
                {
                    data[j] = new int[original.Length];
                    Array.Copy(original, data[j], original.Length);
                }

                int[] results = new int[functions.Length];

                for(int j = 0; j < functions.Length; j++)
                {
                    results[j] = functions[j](data[j]);
                    Assert.AreEqual(results[j], results[0]);
                }
            }
        }
开发者ID:mmoroney,项目名称:Algorithms,代码行数:28,代码来源:FirstMissingElement.cs


示例6: Bicubic

        /// <summary>
        /// A bicubic spline interpolation.
        /// </summary>
        /// <param name="points">The known data points.</param>
        /// <param name="x0">The first X.</param>
        /// <param name="y0">The first Y.</param>
        /// <param name="xDelta">The delta X.</param>
        /// <param name="yDelta">The delta Y.</param>
        /// <returns>An interpolation function.</returns>
        public static Func<double, Func<double, double>> Bicubic(double[,] points, double x0, double y0, double xDelta, double yDelta)
        {
            if (points.GetLength(0) < 2 || points.GetLength(1) < 2) return null;

            double[][] pointsAlt = new double[points.GetLength(1)][];
            for (int x = 0; x < points.GetLength(1); x++)
            {
                pointsAlt[x] = new double[points.GetLength(0)];
                for (int y = 0; y < points.GetLength(0); y++)
                {
                    pointsAlt[x][y] = points[y, x];
                }
            }

            Func<double, double>[] splines = new Func<double, double>[points.GetLength(1)];
            for (int x = 0; x < pointsAlt.Length; x++)
            {
                splines[x] = Interpolation.Cubic(Interpolation.ConvertToPoints(pointsAlt[x], x0, xDelta));
            }

            return x =>
            {
                double[] interpolated = new double[splines.Length];
                for (int i = 0; i < splines.Length; i++)
                {
                    interpolated[i] = splines[i](x);
                }

                return Interpolation.Cubic(Interpolation.ConvertToPoints(interpolated, y0, yDelta));
            };
        }
开发者ID:jamiees2,项目名称:Pie,代码行数:40,代码来源:Interpolation.cs


示例7: IntersectSortedArraysTest

        public void IntersectSortedArraysTest()
        {
            Func<int[], int[], int[]>[] functions = new Func<int[], int[], int[]>[]
            {
                IntersectSortedArrays.BruteForce,
                IntersectSortedArrays.Search,
                IntersectSortedArrays.Iterate
            };

            for(int i = 0; i < 10; i++)
            {
                for(int j = 0; j < 10; j++)
                {
                    int[] a = ArrayUtilities.CreateRandomArray(i, 0, 8);
                    int[] b = ArrayUtilities.CreateRandomArray(j, 0, 8);

                    Array.Sort(a);
                    Array.Sort(b);

                    int[][] results = new int[functions.Length][];

                    for (int k = 0; k < functions.Length; k++)
                    {
                        results[k] = functions[k](a, b);
                        Assert.IsTrue(ArrayUtilities.AreEqual(results[0], results[k]));
                    }
                }
            }
        }
开发者ID:mmoroney,项目名称:Algorithms,代码行数:29,代码来源:IntersectSortedArrays.cs


示例8: Type

        public static TypeArgumentSpecification Type(this TypeArgumentSpecification @this, Func<Type, TypeSpecification, TypeSpecification> typeSpecification)
        {
            Guard.NotNull(@this, "this");
            Guard.NotNull(typeSpecification, "typeSpecification");

            return @this.Combine(ta => typeSpecification(ta, TypeSpecificationBuilder.Any)(ta));
        }
开发者ID:shchimisyaotsel,项目名称:iSynaptic.Commons,代码行数:7,代码来源:TypeArgumentSpecification.cs


示例9: GenerateStringTypeTests

        public void GenerateStringTypeTests()
        {
            FastRandom fr = new FastRandom();
            CharacterType[] types = new CharacterType[] { CharacterType.LowerCase, CharacterType.UpperCase, CharacterType.Numeric };
            Func<char, bool>[] actions = new Func<char, bool>[] { Char.IsLower, Char.IsUpper, Char.IsNumber };
            for(int i = 0; i < types.Length; i++)
            {
                for(int j = 0; j < 1000; j++)
                {
                    var s = fr.NextString(5, types[i]);
                    Assert.Equal(5, s.Length);
                    foreach(var c in s)
                    {
                        Assert.True(actions[i](c));
                    }
                }
            }
            {
                var s = fr.NextString(5, CharacterType.Special);
                Assert.Equal(5, s.Length);
                foreach(var c in s)
                {
                    Assert.True(FastRandom.SpecialCharacters.Contains(c));
                }
            }

        }
开发者ID:glorylee,项目名称:Aoite,代码行数:27,代码来源:FastRandomTests.cs


示例10: MergeTwoSortedListsTest

        public void MergeTwoSortedListsTest()
        {
            Func<ListNode<int>, ListNode<int>, ListNode<int>>[] functions = new Func<ListNode<int>, ListNode<int>, ListNode<int>>[]
            {
                MergeTwoSortedLists.AppendAndSort,
                MergeTwoSortedLists.SinglePass
            };

            for(int i = 0; i < 10; i++)
            {
                int[] data1 = ArrayUtilities.CreateRandomArray(10, 0, 25);
                int[] data2 = ArrayUtilities.CreateRandomArray(10, 0, 25);
                Array.Sort(data1);
                Array.Sort(data2);

                int[] expected = new int[data1.Length + data2.Length];
                Array.Copy(data1, expected, data1.Length);
                Array.Copy(data2, 0, expected, data1.Length, data2.Length);
                Array.Sort(expected);

                for (int j = 0; j < functions.Length; j++)
                {
                    ListNode<int> result = functions[j](LinkedListUtilities.Initialize(data1), LinkedListUtilities.Initialize(data2));
                    int[] actual = LinkedListUtilities.ToArray(result);

                    Assert.IsTrue(ArrayUtilities.AreEqual(expected, actual));
                }
            }
        }
开发者ID:mmoroney,项目名称:Algorithms,代码行数:29,代码来源:MergeTwoSortedLists.cs


示例11: ShortestPathTest

        public void ShortestPathTest()
        {
            Func<Vertex[], int[,]>[] functions = new Func<Vertex[], int[,]>[]
            {
                ShortestPathTestClass.RunBellmanFord,
                ShortestPathTestClass.RunDjikstra,
                ShortestPathTestClass.RunFloydWarshall,
            };

            Vertex[] vertices = new Vertex[10];
            for (int i = 0; i < vertices.Length; i++)
                vertices[i] = new Vertex();

            for (int i = 0; i <= vertices.Length * (vertices.Length - 1); i++)
            {
                for (int j = 0; j < vertices.Length; j++)
                {
                    int[][,] results = new int[vertices.Length][,];

                    for (int k = 0; k < functions.Length; k++)
                    {
                        foreach (Vertex vertex in vertices)
                            vertex.Reset();

                        results[k] = functions[k](vertices);
                        Assert.IsTrue(ArrayUtilities.AreEqual(results[0], results[k]));
                    }
                }

                GraphUtilities.SetRandomEdge(vertices);
            }
        }
开发者ID:mmoroney,项目名称:Algorithms,代码行数:32,代码来源:ShortestPathTest.cs


示例12: GetPublicIp

        public string GetPublicIp()
        {
            var ipServices = new Func<string>[]
            {
                () => new IpInfoIpService().GetPublicIp(),
                () => new DynDnsIpService().GetPublicIp(),
                () => new IpifyIpService().GetPublicIp(),
                () => new ICanHazIpService().GetPublicIp(),
                () => new FreeGeoIpService().GetPublicIp()
            };

            for (int i = 0; i < ipServices.Count(); i++)
            {
                if (lastUsedIndex >= ipServices.Count()) lastUsedIndex = 0;
                else lastUsedIndex++;

                try
                {
                    return ipServices[lastUsedIndex]();
                }
                catch { }
            }

            return null;

        }
开发者ID:c0d3m0nky,项目名称:mty,代码行数:26,代码来源:IpService.cs


示例13: Stack

        private static int Stack(string rpn)
        {
            string operators = "+-*/";
            Func<int, int, int>[] functions = new Func<int, int, int>[]
            {
                (x, y) => x + y,
                (x, y) => x - y,
                (x, y) => x * y,
                (x, y) => x / y,
            };

            string[] tokens = rpn.Split(',');
            Stack<int> values = new Stack<int>();

            foreach(string token in tokens)
            {
                int index = operators.IndexOf(token);
                if (index == -1)
                {
                    values.Push(int.Parse(token));
                    continue;
                }

                int y = values.Pop();
                int x = values.Pop();

                values.Push(functions[index](x, y));
            }

            return values.Pop();
        }
开发者ID:mmoroney,项目名称:Algorithms,代码行数:31,代码来源:EvaluateRPN.cs


示例14: Transfer

        public static void Transfer(SupermarketsEntities sqlserver)
        {
            using (var mysql = new MySqlSupermarket())
            {
                // SET IDENTITY_INSERT (Transact-SQL) http://msdn.microsoft.com/en-us/library/ms188059.aspx
                sqlserver.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Vendors ON");
                sqlserver.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Measures ON");
                sqlserver.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Products ON");

                var mysqlTables = new IEnumerable[] { mysql.Vendors, mysql.Measures, mysql.Products };
                var sqlserverEntityFactories = new Func<object>[] { () => new Vendor(), () => new Measure(), () => new Product() };
                var sqlserverTables = new DbSet[] { sqlserver.Vendors, sqlserver.Measures, sqlserver.Products };

                for (int ii = 0; ii < mysqlTables.Length; ii++)
                {
                    foreach (var mysqlObject in mysqlTables[ii])
                    {
                        var sqlserverObject = sqlserverEntityFactories[ii]();
                        sqlserverObject.LoadPropertiesFrom(mysqlObject);
                        sqlserverTables[ii].Add(sqlserverObject);
                    }
                }

                sqlserver.SaveChanges();

                sqlserver.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Vendors OFF");
                sqlserver.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Measures OFF");
                sqlserver.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Products OFF");
            }
        }
开发者ID:VelizarIT,项目名称:db-project-2013,代码行数:30,代码来源:MySqlTransfer.cs


示例15: ExecuteTests

		public void ExecuteTests(TestTraceOutput output, Component originalComponent, Component transformedComponent,
								 MethodInfo originalMethod, MethodInfo transformedMethod)
		{
			var parameters = originalMethod.GetParameters();
			var originalArguments = new object[parameters.Length];
			var transformedArguments = new object[parameters.Length];
			var valueFactories = new Func<object>[parameters.Length];

			for (var i = 0; i < parameters.Length; ++i)
			{
				if (parameters[i].ParameterType == typeof(int) || parameters[i].ParameterType == typeof(int).MakeByRefType())
					valueFactories[i] = RandomInt32;
				else if (parameters[i].ParameterType == typeof(double) || parameters[i].ParameterType == typeof(double).MakeByRefType())
					valueFactories[i] = RandomDouble;
				else if (parameters[i].ParameterType == typeof(bool) || parameters[i].ParameterType == typeof(bool).MakeByRefType())
					valueFactories[i] = RandomBoolean;
				else
					Assert.NotReached("Unknown parameter type '{0}'.", parameters[i].ParameterType);
			}

			output.Log("Testing '{0}'", originalMethod);
			for (var i = 0; i < _testCount * 2; ++i)
			{
				output.Trace("----- Inputs -----");
				for (var j = 0; j < originalArguments.Length; ++j)
				{
					originalArguments[j] = valueFactories[j]();
					transformedArguments[j] = originalArguments[j];

					output.Trace("{0} = {1}", parameters[j].Name, originalArguments[j]);
				}

				var originalResult = originalMethod.Invoke(originalComponent, originalArguments);
				var transformedResult = transformedMethod.Invoke(transformedComponent, transformedArguments);

				if (originalResult != null && originalResult.GetType().IsEnum)
				{
					originalResult = ((IConvertible)originalResult).ToInt32(CultureInfo.InvariantCulture);
					transformedResult = ((IConvertible)transformedResult).ToInt32(CultureInfo.InvariantCulture);
				}

				transformedResult.ShouldBe(originalResult);
				transformedArguments.ShouldBe(originalArguments);

				for (var j = 0; j < originalComponent.Metadata.Fields.Length; ++j)
				{
					var originalField = originalComponent.Metadata.Fields[j];
					output.Trace("Comparing field '{0}'", originalField.FieldInfo);

					var transformedField = transformedComponent.Metadata.Fields.Single(f => f.Name == originalField.Name);
					transformedField.FieldInfo.GetValue(transformedComponent).ShouldBe(originalField.FieldInfo.GetValue(originalComponent));
				}
			}
		}
开发者ID:cubeme,项目名称:safety-sharp,代码行数:54,代码来源:ExecutionTests.Helpers.cs


示例16: MakeFromCoeffs

        /// <summary>
        /// 
        /// </summary>
        /// <param name="coeffs"></param>
        /// <returns></returns>
        private static Func<double, double> MakeFromCoeffs(double y0, double y1, double[] coeffs)
        {
            var n = coeffs.Length;
            var haar2 = new Func<double, double>[n];
            for (int k = 0; k < n; k++)
            {
                haar2[k] = MixedHaar2(2, k + 1 + 2);
            }

            return t =>
                y0 + y1 * t + coeffs.Select((yk, k) => yk * haar2[k](t)).Sum();
        }
开发者ID:rasuldev,项目名称:mixhaar,代码行数:17,代码来源:DiffEqSolver.cs


示例17: DesignMatrix

 /// <summary>
 /// 引数に指定された基底とデータ長に対応したデザイン行列を作る。<br />
 /// データは 0 ~ 1 の範囲をデータ長で等分した位置で取る。
 /// </summary>
 /// <param name="dataLength">デザイン行列のデータ長。</param>
 /// <param name="basis">デザイン行列に用いる基底。</param>
 public static double[,] DesignMatrix(int dataLength, Func<double, double>[] basis)
 {
     int dimension = basis.Length;
     double[,] matrix = new double[dataLength, dimension];
     for (int m = 0; m < dimension; m++)
     {
         for (int n = 0; n < dataLength; n++)
         {
             matrix[n, m] = basis[m]((double)n / dataLength);
         }
     }
     return matrix;
 }
开发者ID:y-takashina,项目名称:RegressionDemo,代码行数:19,代码来源:Regression.cs


示例18: max

        //calculates the f(x) value from each lamda expression int array
        //@param x a value for the func
        //@param maxValue the greatest calculated f(x) value
        //@param currentValue is the current calculated value to check for max
        public void max(Func<float, float>[] l, float x)
        {
            float maxValue = 0;
            for (int i = 0; i < l.Length; i++)
            {
                currentValue = l[i](x);

                if (currentValue > maxValue)
                {
                    maxValue = currentValue;
                }
            }
            Console.WriteLine("the max is --> "+maxValue);
        }
开发者ID:KlajdiIsmailaj,项目名称:Informatik3,代码行数:18,代码来源:MathsFunctions.cs


示例19: TestDifferentIterateVariables

        public void TestDifferentIterateVariables()
        {
            const int Length = 3;

            Func<int>[] diffVarFuncs = new Func<int>[Length];
            for (int i = 0; i < Length; ++i)
            {
                int temp = i;
                diffVarFuncs[i] = () => { return temp; };
            }

            for (int i = 0; i < Length; ++i)
                Assert.AreEqual(i, diffVarFuncs[i]());
        }
开发者ID:stasi009,项目名称:TestDrivenLearn,代码行数:14,代码来源:LambdaExpressionTest.cs


示例20: svdfit

        internal static void svdfit(double[][] x, double[][] y, double[][] a, out double chisq, Func<double[], double>[] funcs)
        {
            int i, j, k;
            double wmax, tmp, thresh, sum, TOL = 1e-13;

            //Allocated memory for svd matrices
            double[,] u = new double[x.Length, funcs.Length];
            double[,] v = new double[funcs.Length, funcs.Length];
            double[] w = new double[funcs.Length];

            //Fill input matrix with values based on fitting functions and input coordinates 
            for (i = 0; i < x.Length; i++)
            {
                for (j = 0; j < funcs.Length; j++)
                    u[i, j] = funcs[j](x[i]);
            }

            //Perform decomposition
            svdcmp(u, w, v);

            //Check for w values that are close to zero and replace them with zeros such that they are ignored in backsub
            wmax = 0;
            for (j = 0; j < funcs.Length; j++)
                if (w[j] > wmax) wmax = w[j];

            thresh = TOL * wmax;

            for (j = 0; j < funcs.Length; j++)
                if (w[j] < thresh) w[j] = 0;

            //Perform back substitution to get result
            svdbksb(u, w, v, y, a);

            //Calculate chi squared for the fit
            chisq = 0;
            for (k = 0; k < y[0].Length; k++)
            {
                for (i = 0; i < y.Length; i++)
                {
                    sum = 0.0;
                    for (j = 0; j < funcs.Length; j++) sum += a[j][k] * funcs[j](x[i]);
                    tmp = (y[i][k] - sum);
                    chisq += tmp * tmp;
                }
            }

            chisq = Math.Sqrt(chisq / (y.Length * y[0].Length)); 
        }
开发者ID:OperatorOverload,项目名称:encog-cs,代码行数:48,代码来源:SVD.cs



注:本文中的Func类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# FuncDef类代码示例发布时间:2022-05-24
下一篇:
C# FullPath类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap