/// <summary> /// Executes a function on each item in a <see cref="ICollection" /> /// but does not accumulate the result. /// </summary> /// <param name="coll"></param> /// <param name="func"></param> public static void Apply(ICollection coll, GenericMethod <object> func) { foreach (object obj in coll) { func(obj); } }
public void Run() { int a, b; char c, d; a = 10; b = 20; c = 'I'; d = 'V'; //display values before swap: Console.WriteLine("Int values before calling swap:"); Console.WriteLine("a = {0}, b = {1}", a, b); Console.WriteLine("Char values before calling swap:"); Console.WriteLine("c = {0}, d = {1}", c, d); //call swap - why nsGenericMethod needed ? GenericMethod.Swap <int>(ref a, ref b); GenericMethod.Swap <char>(ref c, ref d); //display values after swap: Console.WriteLine("Int values after calling swap:"); Console.WriteLine("a = {0}, b = {1}", a, b); Console.WriteLine("Char values after calling swap:"); Console.WriteLine("c = {0}, d = {1}", c, d); Console.WriteLine("\n"); }
public bool Run() { InferenceStart(); if (GenericMethod.AcceptVarArgs) { if (Arguments.Length < GenericMethod.GetParameters().Length) { return(InferenceComplete(false)); } } else if (Arguments.Length != GenericMethod.GetParameters().Length) { return(InferenceComplete(false)); } InferExplicits(); while (HasUnfixedTypes()) { bool wasFixed = FixAll(HasNoDependencies) || FixAll(HasDependantsAndBounds); if (!wasFixed) { return(InferenceComplete(false)); } InferCallables(); } ; return(InferenceComplete(true)); }
/// <summary> /// Executes a function on each item in a <see cref="ICollection" /> /// and returns the results in a new <see cref="IList" />. /// </summary> /// <param name="coll"></param> /// <param name="func"></param> /// <returns></returns> public static IList Transform(ICollection coll, GenericMethod<object> func) { IList result = new ArrayList(); foreach (object obj in coll) result.Add(func(obj)); return result; }
private static CqlQuery <T> AddSort <T>(CqlQuery <T> query, Sort sort) { var propertyType = typeof(T).GetProperty(sort.Path).PropertyType; return(GenericMethod.Invoke(() => AddSort <T, object>(query, sort), new[] { typeof(T), typeof(object) }, new[] { typeof(T), propertyType })); }
/// <summary> /// Executes a function on each item in a <see cref="ICollection" /> /// and collects all the entries for which the result /// of the function is equal to <c>true</c>. /// </summary> /// <param name="coll"></param> /// <param name="func"></param> /// <returns></returns> public static IList Select(ICollection coll, GenericMethod<object> func) { IList result = new ArrayList(); foreach (object obj in coll) if (true.Equals(func(obj))) result.Add(obj); return result; }
public void SutIsIMethod() { Action dummy = delegate { }; var anonymousFactory = new DelegatingMethodFactory(); var sut = new GenericMethod(dummy.Method, anonymousFactory); Assert.IsAssignableFrom <IMethod>(sut); }
public static IEnumerator WaitClausuleAndCallCo(GenericMethod5 callbackClausule, GenericMethod callBack) { while (!callbackClausule()) { yield return(new WaitForFixedUpdate()); } callBack(); }
protected void ECButton_Click(object sender, EventArgs e) { double x = 25, y = 34; Label.Text = "x = " + x + ", y = " + y; GenericMethod.Swap(ref x, ref y); Label.Text += "<br>x = " + x + ", y = " + y; Label.Text += "<br><br>" + GenericMethod.Compare(x, y); }
public void MethodIsCorrect() { var expectedMethod = ((Action) delegate { }).GetMethodInfo(); var anonymousFactory = new DelegatingMethodFactory(); var sut = new GenericMethod(expectedMethod, anonymousFactory); var result = sut.Method; Assert.Equal(expectedMethod, result); }
public override int GetHashCode() { int hash = Hash.Start; hash = Hash.Combine(hash, DeclaringType.GetHashCode()); hash = Hash.Combine(hash, (GenericMethod == null ? Hash.Start : GenericMethod.GetHashCode())); hash = Hash.Combine(hash, GenericArguments.GetHashCode()); hash = Hash.Combine(hash, Parameters.GetHashCode()); return(this.Parameters.GetHashCode()); }
/// <summary> /// Executes a function on each item in a <see cref="ICollection" /> /// and returns the results in a new <see cref="IList" />. /// </summary> /// <param name="coll"></param> /// <param name="func"></param> /// <returns></returns> public static IList Transform(ICollection coll, GenericMethod <object> func) { IList result = new ArrayList(); foreach (object obj in coll) { result.Add(func(obj)); } return(result); }
public void FactoryIsCorrect() { var anonymousMethod = ((Action) delegate { }).Method; var expectedFactory = new DelegatingMethodFactory(); var sut = new GenericMethod(anonymousMethod, expectedFactory); var result = sut.Factory; Assert.Equal(expectedFactory, result); }
private static void GenericMethodExampleOutput() { // creating object of class GFG GenericMethod p = new GenericMethod(); // calling Generics method p.Display <int>("Integer", 122); p.Display <char>("Character", 'H'); p.Display <double>("Decimal", 255.67); }
/// Test the generic stack public static void TestGenericStack() { // Create stack int size = 10; GenericStack <double> stack = new GenericStack <double>(size); GenericStack <string> stack2 = new GenericStack <string>(size); // Push elements on the stack try { for (int i = 0; i <= size; i++) { stack.Push(i); Console.WriteLine("Push: {0}", i); } } catch (ApplicationException ex) { Console.WriteLine("Error while pushing values on the stack: {0}", ex.Message); } // Pop elements from the stack double total = 0.0; try { for (int i = 0; i <= size + 5; i++) { // Note, no casting needed. double value = stack.Pop(); total += value; Console.WriteLine("Pop: {0}", value); } } catch (ApplicationException ex) { Console.WriteLine("Error while poping values from the stack: {0}", ex.Message); } Console.WriteLine("Total: {0}", total); // Using Generic methods int sz1 = 10; int sz2 = 6; GenericStack <double> stackA = new GenericStack <double>(sz1); GenericStack <double> stackB = new GenericStack <double>(sz2); GenericMethod.Swap <GenericStack <double> >(ref stackA, ref stackB); Console.WriteLine("Sizes of stacks: {0} {1}", stackA.Size(), stackB.Size()); // Swap 2 doubles double d1 = 1.2; double d2 = 3.0; GenericMethod.Swap <double>(ref d1, ref d2); Console.WriteLine("Sizes of stacks: {0} {1}", d1, d2); }
public static void Main() { // Create object with the generic swap method GenericMethod gm = new GenericMethod(); // Value type double d1 = 2.79; double d2 = 3.14; // Struct (value type) Point p1 = new Point(1, 2); Point p2 = new Point(3, 4); // Class (reference type) Exception e1 = new Exception("Exception 1"); Exception e2 = new Exception("Exception 2"); // Print values Console.WriteLine("Original values"); Console.WriteLine("Doubles: {0}, {1}", d1, d2); Console.WriteLine("Points: {0}, {1}", p1, p2); Console.WriteLine("Exceptions: {0}, {1}", e1, e2); // Swap the values gm.Swap <double>(ref d1, ref d2); gm.Swap <Point>(ref p1, ref p2); gm.Swap <Exception>(ref e1, ref e2); // Print values again Console.WriteLine("\nSwapped values"); Console.WriteLine("Doubles: {0}, {1}", d1, d2); Console.WriteLine("Points: {0}, {1}", p1, p2); Console.WriteLine("Exceptions: {0}, {1}", e1, e2); // Swap the values gm.Swap(ref d1, ref d2); gm.Swap(ref p1, ref p2); gm.Swap(ref e1, ref e2); // Print values again Console.WriteLine("\nSwapped again values"); Console.WriteLine("Doubles: {0}, {1}", d1, d2); Console.WriteLine("Points: {0}, {1}", p1, p2); Console.WriteLine("Exceptions: {0}, {1}", e1, e2); // Call with specifying the type Console.WriteLine("\n\nCall generic print with type"); gm.Print <double>(3.14); gm.Print <Exception>(new Exception("Exception")); // Call without specifying the type Console.WriteLine("\nCall generic print without type"); gm.Print(3.14); gm.Print(new Exception("Exception")); }
public void FactoryIsCorrect() { Action action = () => { }; var anonymousMethod = action.GetMethodInfo(); var expectedFactory = new DelegatingMethodFactory(); var sut = new GenericMethod(anonymousMethod, expectedFactory); var result = sut.Factory; Assert.Equal(expectedFactory, result); }
public void ParametersIsCorrect() { Action <int, double> dummy = delegate { }; var anonymousFactory = new DelegatingMethodFactory(); var expectedParameters = dummy.Method.GetParameters(); var sut = new GenericMethod(dummy.Method, anonymousFactory); var result = sut.Parameters; Assert.True(expectedParameters.SequenceEqual(result)); }
public void SubtypeDispatchTest() { var speak = new GenericMethod<IAnimal, String>() .AddLast<Dog>(x => x.Bark()) .AddLast<Cat>(x => x.Purr()) .AddLast<Duck>(x => x.Honk()) .AsFunc(); Assert.AreEqual("Woof", speak(new Dog())); Assert.AreEqual("Meow", speak(new Cat())); Assert.AreEqual("Quack", speak(new Duck())); }
/// <summary> /// Executes a function on each item in a <see cref="ICollection" /> /// and collects all the entries for which the result /// of the function is equal to <c>true</c>. /// </summary> /// <param name="coll"></param> /// <param name="func"></param> /// <returns></returns> public static IList Select(ICollection coll, GenericMethod <object> func) { IList result = new ArrayList(); foreach (object obj in coll) { if (true.Equals(func(obj))) { result.Add(obj); } } return(result); }
public static void Main(string[] args) { OverrideMethod om = new OverrideMethod(); om.Print(100); om.Print(3.14); GenericMethod gm = new GenericMethod(); gm.Print <int>(100); gm.Print <double>(3.14); gm.Print <string>("ABC"); }
public void InitializeObservableCollections() { ControlRegistrationMethod = new ControlRegistrationMethod(); ControlScheduleMethod = new ControlScheduleMethod(); FrontpageMethod = new FrontpageMethod(); ProductionMethod = new ProductionMethod(); ShiftRegistrationMethod = new ShiftRegistrationMethod(); TuMethod = new TuMethod(); XamlBindings = new XamlBindings(); TrendAdminstrator = new TrendAdminstrator(); GenericMethod = new GenericMethod(); }
public void PredicateDispatchTest() { var method = new GenericMethod<int, int>() .AddLast(Z.Neg, Z.Negate) .AddLast(_ => true, Z.Id) .AsFunc(); Assert.AreEqual(5, method(-5)); Assert.AreEqual(2, method(2)); Assert.AreEqual(0, method(0)); Check.That(Sample.Ints.Except(int.MinValue), x => method(x) >= 0); Check.Idempotent(method); }
public void InvokeWithGenericMethodReturnsCorrectResult(Type targetType, int index, object values) { var method = (from mi in targetType .GetMethods(BindingFlags.Static | BindingFlags.Public) select mi).ElementAt(index); var factory = new DelegatingMethodFactory(); factory.OnCreate = m => new StaticMethod(m); var sut = new GenericMethod(method, factory); var result = sut.Invoke((object[])values); Assert.IsAssignableFrom(targetType, result); }
static void Main(string[] args) { //SimpleGenericsClass.Start(); //TwoGenericParameters.Start(); //BaseClassConstraint.Start(); //InterfaceConstraint.Start(); //ConstructorConstraint.Start(); //TypeConstraint.Start(); GenericMethod.Start(); //GenericInterface.Start(); Console.WriteLine("Done"); Console.ReadKey(); }
static void Main(string[] args) { OverrideMethod om = new OverrideMethod(); om.Print(1); om.Print(3.14); om.Print("Hello World"); GenericMethod gm = new GenericMethod(); gm.Print <int>(1); gm.Print <double>(3.14); gm.Print <string>("Hello World"); }
public void InvokeWithGenericMethodThatCannotBeInferedThrows(Type targetType, int index, object values) { var method = (from mi in targetType .GetMethods(BindingFlags.Static | BindingFlags.Public) select mi).ElementAt(index); var factory = new DelegatingMethodFactory(); factory.OnCreate = m => new StaticMethod(m); var sut = new GenericMethod(method, factory); var exception = Assert.Throws <TypeArgumentsCannotBeInferredException>( () => sut.Invoke((object[])values)); Assert.Contains(method.Name, exception.Message); }
private static void InitAbs() { Abs = GenericMethod.Get("Abs", "Abs"); Abs.Add <byte>(a => (byte)Math.Abs(a)); Abs.Add <sbyte>(a => Math.Abs(a)); Abs.Add <char>(a => (char)Math.Abs(a)); Abs.Add <short>(a => Math.Abs(a)); Abs.Add <ushort>(a => (ushort)Math.Abs(a)); Abs.Add <int>(a => Math.Abs(a)); Abs.Add <uint>(a => (uint)Math.Abs(a)); Abs.Add <long>(a => Math.Abs(a)); //Abs.Add<ulong>(a => (ulong)Math.Abs(a)); Abs.Add <decimal>(a => Math.Abs(a)); Abs.Add <float>(a => Math.Abs(a)); Abs.Add <double>(a => Math.Abs(a)); }
private static void InitSign() { Sign = GenericMethod.Get("Sign", "Sign"); Sign.Add <byte>(a => Math.Sign(a)); Sign.Add <sbyte>(a => Math.Sign(a)); Sign.Add <char>(a => Math.Sign(a)); Sign.Add <short>(a => Math.Sign(a)); Sign.Add <ushort>(a => Math.Sign(a)); Sign.Add <int>(a => Math.Sign(a)); Sign.Add <uint>(a => Math.Sign(a)); Sign.Add <long>(a => Math.Sign(a)); //Sign.Add<ulong>(a => Math.Sign(a)); Sign.Add <decimal>(a => Math.Sign(a)); Sign.Add <float>(a => Math.Sign(a)); Sign.Add <double>(a => Math.Sign(a)); }
private static void InitMin() { Min = GenericMethod.Get("Min", "Min"); Min.Add <byte, byte>((a, b) => Math.Min(a, b)); Min.Add <sbyte, sbyte>((a, b) => Math.Min(a, b)); Min.Add <char, char>((a, b) => (char)Math.Min(a, b)); Min.Add <short, short>((a, b) => Math.Min(a, b)); Min.Add <ushort, ushort>((a, b) => Math.Min(a, b)); Min.Add <int, int>((a, b) => Math.Min(a, b)); Min.Add <uint, uint>((a, b) => Math.Min(a, b)); Min.Add <long, long>((a, b) => Math.Min(a, b)); Min.Add <ulong, ulong>((a, b) => Math.Min(a, b)); Min.Add <decimal, decimal>((a, b) => Math.Min(a, b)); Min.Add <float, float>((a, b) => Math.Min(a, b)); Min.Add <double, double>((a, b) => Math.Min(a, b)); }
/// <summary> /// Invokes generic method /// </summary> /// <param name="instance">Object to invoke generic method on</param> /// <param name="typeList">Generic parameter list</param> /// <param name="parameters">Method parameters</param> public object Invoke(object instance, GenericParameterList typeList, params object[] parameters) { if (!GenericMethodCache.ContainsKey(typeList)) { lock (GenericMethodCache) { if (!GenericMethodCache.ContainsKey(typeList)) { GenericMethodCache[typeList] = GenericMethod.MakeGenericMethod(typeList.Types); } } } var method = GenericMethodCache[typeList]; return(method.FastInvoke(instance, parameters)); }
private static void InitSqrt() { Sqrt = GenericMethod.Get("Sqrt", "Sqrt"); Sqrt.Add <byte>(a => (byte)Math.Sqrt(a)); Sqrt.Add <sbyte>(a => (sbyte)Math.Sqrt(a)); Sqrt.Add <char>(a => (char)Math.Sqrt(a)); Sqrt.Add <short>(a => (short)Math.Sqrt(a)); Sqrt.Add <ushort>(a => (ushort)Math.Sqrt(a)); Sqrt.Add <int>(a => (int)Math.Sqrt(a)); Sqrt.Add <uint>(a => (uint)Math.Sqrt(a)); Sqrt.Add <long>(a => (long)Math.Sqrt(a)); Sqrt.Add <ulong>(a => (ulong)Math.Sqrt(a)); //Sqrt.Add<decimal>(a => (decimal)Math.Sqrt(a)); Sqrt.Add <float>(a => (float)Math.Sqrt(a)); Sqrt.Add <double>(a => Math.Sqrt(a)); }
private static void InitNegative() { Negative = GenericMethod.Get("Negative", "Negative", "op_UnaryNegation"); Negative.Add <bool>(a => !a); Negative.Add <byte>(a => (byte)-a); Negative.Add <sbyte>(a => (sbyte)-a); Negative.Add <char>(a => (char)-a); Negative.Add <short>(a => (short)-a); Negative.Add <ushort>(a => (ushort)-a); Negative.Add <int>(a => - a); Negative.Add <uint>(a => (uint)-a); Negative.Add <long>(a => - a); //Negative.Add<ulong>(a => (ulong)-a); Negative.Add <decimal>(a => - a); Negative.Add <float>(a => - a); Negative.Add <double>(a => - a); }
private static void InitZero() { Zero = GenericMethod.Get("Zero", "Zero"); Zero.AddValue(false); Zero.AddValue <byte>(0); Zero.AddValue <sbyte>(0); Zero.AddValue('\0'); Zero.AddValue <short>(0); Zero.AddValue <ushort>(0); Zero.AddValue(0); Zero.AddValue(0U); Zero.AddValue(0L); Zero.AddValue(0UL); Zero.AddValue(0.0M); Zero.AddValue(0.0F); Zero.AddValue(0.0); }
/// <summary> /// Executes a function on each item in a <see cref="ICollection" /> /// but does not accumulate the result. /// </summary> /// <param name="coll"></param> /// <param name="func"></param> public static void Apply(ICollection coll, GenericMethod<object> func) { foreach (object obj in coll) func(obj); }
internal SignatureConverter( PEFileToObjectModel peFileToObjectModel, MemoryReader signatureMemoryReader, MetadataObject metadataOwnerObject ) //^ requires signatureMemoryReader.Length > 0; { this.PEFileToObjectModel = peFileToObjectModel; this.SignatureMemoryReader = signatureMemoryReader; this.MetadataOwnerObject = metadataOwnerObject; this.ModuleMethodReference = metadataOwnerObject as IMethodReference; this.ModuleMemberReference = metadataOwnerObject as ITypeMemberReference; TypeMember/*?*/ moduleTypeMember = metadataOwnerObject as TypeMember; if (moduleTypeMember != null) { this.ModuleGenericType = moduleTypeMember.ContainingTypeDefinition as TypeBase; this.ModuleGenericMethod = moduleTypeMember as GenericMethod; return; } var moduleGenericType = metadataOwnerObject as TypeBase; if (moduleGenericType != null) { this.ModuleGenericType = moduleGenericType; return; } GenericTypeParameter/*?*/ genericTypeParam = metadataOwnerObject as GenericTypeParameter; if (genericTypeParam != null) { this.ModuleGenericType = genericTypeParam.OwningGenericType; return; } GenericMethodParameter/*?*/ genericMethodParam = metadataOwnerObject as GenericMethodParameter; if (genericMethodParam != null) { this.ModuleGenericType = genericMethodParam.OwningGenericMethod.ContainingTypeDefinition as TypeBase; this.ModuleGenericMethod = genericMethodParam.OwningGenericMethod; return; } }
IMethodDefinition CreateMethod( uint methodDefRowId, TypeBase parentModuleType ) { Debug.Assert(methodDefRowId > 0 && methodDefRowId <= this.PEFileReader.MethodTable.NumberOfRows); MethodRow methodRow = this.PEFileReader.MethodTable[methodDefRowId]; IName methodName = this.GetNameFromOffset(methodRow.Name); if ((methodRow.Flags & MethodFlags.AccessMask) == MethodFlags.CompilerControlledAccess) { //Methods that are compiler controlled access are excempted from duplicate checking and may thus have names that cause //their intern keys to clash with other methods. Avoid this by mangling the names of such methods. //compiler controlled methods are always referred to via their tokens, so this renaming is safe to do. methodName = NameTable.GetNameFor(methodName.Value+"$PST"+((int)TableIndices.Method+methodDefRowId)); } uint genericParamRowIdStart; uint genericParamRowIdEnd; this.GetGenericParamInfoForMethod(methodDefRowId, out genericParamRowIdStart, out genericParamRowIdEnd); IMethodDefinition moduleMethod; if (genericParamRowIdStart == 0) { moduleMethod = new NonGenericMethod(this, methodName, parentModuleType, methodDefRowId, methodRow.Flags, methodRow.ImplFlags); } else { moduleMethod = new GenericMethod(this, methodName, parentModuleType, methodDefRowId, methodRow.Flags, methodRow.ImplFlags, genericParamRowIdStart, genericParamRowIdEnd); } if (methodName.UniqueKey != this.ModuleReader._Deleted_.UniqueKey) { moduleMethod = this.ModuleReader.metadataReaderHost.Rewrite(this.Module, moduleMethod); parentModuleType.AddMember(moduleMethod); } return moduleMethod; }
internal GenericMethodParameter/*?*/ GetGenericMethodParamAtRow( uint genericParamRowId, GenericMethod moduleMethodOwner ) //^ requires genericParamRowId >= 1; { if (this.ModuleGenericParamArray[genericParamRowId] == null) { lock (GlobalLock.LockingObject) { if (this.ModuleGenericParamArray[genericParamRowId] == null) { GenericParamRow genericParamRow = this.PEFileReader.GenericParamTable[genericParamRowId]; IName genericParamName = this.GetNameFromOffset(genericParamRow.Name); this.ModuleGenericParamArray[genericParamRowId] = new GenericMethodParameter(this, genericParamRow.Number, genericParamRow.Flags, genericParamName, genericParamRowId, moduleMethodOwner); } } } return this.ModuleGenericParamArray[genericParamRowId] as GenericMethodParameter; }
MethodDefinition CreateMethod( uint methodDefRowId, TypeBase parentModuleType ) { Debug.Assert(methodDefRowId > 0 && methodDefRowId <= this.PEFileReader.MethodTable.NumberOfRows); MethodRow methodRow = this.PEFileReader.MethodTable[methodDefRowId]; IName methodName = this.GetNameFromOffset(methodRow.Name); uint genericParamRowIdStart; uint genericParamRowIdEnd; this.GetGenericParamInfoForMethod(methodDefRowId, out genericParamRowIdStart, out genericParamRowIdEnd); MethodDefinition moduleMethod; if (genericParamRowIdStart == 0) { moduleMethod = new NonGenericMethod(this, methodName, parentModuleType, methodDefRowId, methodRow.Flags, methodRow.ImplFlags); } else { moduleMethod = new GenericMethod(this, methodName, parentModuleType, methodDefRowId, methodRow.Flags, methodRow.ImplFlags, genericParamRowIdStart, genericParamRowIdEnd); } if (methodName.UniqueKey != this.ModuleReader._Deleted_.UniqueKey) { parentModuleType.AddMember(moduleMethod); } return moduleMethod; }