当前位置: 首页>>代码示例>>C#>>正文


C# Func类代码示例

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


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


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