本文整理汇总了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;
}
示例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);
}
}
示例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]));
}
}
}
}
}
示例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));
}
示例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]);
}
}
}
示例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));
};
}
示例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]));
}
}
}
}
示例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));
}
示例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));
}
}
}
示例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));
}
}
}
示例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);
}
}
示例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;
}
示例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();
}
示例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");
}
}
示例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));
}
}
}