static Crc32_Castagnoli() { try { #if Windows if (System.Environment.Is64BitProcess) { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Library_Security_x64.dll"); } else { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Library_Security_x86.dll"); } #endif #if Unix if (System.Environment.Is64BitProcess) { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Library_Security_x64.so"); } else { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Library_Security_x86.so"); } #endif _compute = _nativeLibraryManager.GetMethod<ComputeDelegate>("compute_Crc32_Castagnoli"); } catch (Exception e) { Log.Warning(e); } }
/* Returns the sum of applying the delegate * method to the numbers from 1.0 to n. */ public double Test(int n, ComputeDelegate cd) { double total = 0.0; for (double i = 1; i <= n; i++) total += cd(i); return total; }
void MulticastDelegate() { ComputeDelegate compute = new ComputeDelegate(MulticastCompute); compute += MulticastComputeMultiply; compute += MulticastComputeDivide; Console.WriteLine("\n\nMulticast delegate:\n" + compute(4, 4)); }
// Method naming convention: Start with upper case letters static void Main(string[] args) { // the Compute method has a signature of: return int, take int as param //ComputeDelegate computeInstance = Compute; // instead of defining a Compute method, we can use anonymous methods // declare anon methods: delegate (params){body} // the anonymous method's signature has to match the delegate's signature. In this case return int, take int as param ComputeDelegate computeInstance = delegate(int x) { return(x * 2); }; // when we defined ComputeDelegate, we already specified the signature of the methods it can reference // the compiler already knows what params a method should take and what its return type should be // so why do we need to specify the parameter's type? // we can use lambda expressions, which are anonymous functions written with less code // lambdas use the syntax: (param list) => { body} or (param list) => (return type) ComputeDelegate computeInstance2 = (x) => { return(x * 2); }; // or (x) => (x*2); // we don't need to specify x's type, because when we defined the ComputeDelegate type, // we specified that it takes one parameter of type int. So x must be an int // lambdas are just a quick way to define anonymous functions , a way of expressing a method/function // they're very convenient because we can pass them as parameters var studentList = new List <Student> { new Student("Genn", "Eric", 20), new Student("Bob", "Ross", 23) }; // the .Where() method is a LINQ method which takes a Func<Student, bool> as param // the type Func<T, R> refers to a method which takes a parameter of type T and returns a value of type R // since lambda expressions return methods, we can use them as parameters in the Where() method var filteredList = studentList.Where((student => (student.Age > 20))).ToList(); // The line above is equivalent to the following 2 statements: /* * Func<Student, bool> explicitFunc = student => (student.Age > 20); * var filteredList = studentList.Where(explicitFunc).ToList(); * * OR * * Func<Student, bool> explicitFunc = delegate(Student student) { return student.Age > 20; } ; * var filteredList = studentList.Where(explicitFunc).ToList(); */ // we now have a list of students where Age > 20 foreach (Student student in filteredList) { Console.WriteLine(student.FullName); } Console.ReadKey(); }
/* Tests with Sqrt and Square */ public static void Main() { Compute test = new Compute(); ComputeDelegate compute = new ComputeDelegate(Math.Sqrt); Console.WriteLine(test.Test(5, compute)); compute = new ComputeDelegate(test.Square); Console.WriteLine(test.Test(5, compute)); Console.WriteLine(test.Test(5, (double x) => x*x*x)); }
internal static void LoadPureUnsafeMethods() { if (_nativeLibraryManager != null) { _nativeLibraryManager.Dispose(); _nativeLibraryManager = null; } _compute = PureUnsafeMethods.Crc32_Castagnoli_Compute; }
static void Main() { List <Employee> employees = new List <Employee>(); employees.Add(new Employee(40)); employees.Add(new Employee(65)); employees.Add(new Employee(95)); MethodInfo mi = typeof(Employee).GetMethod("ComputeSalary", BindingFlags.Public | BindingFlags.Instance); ComputeDelegate <Employee> applyRaise = (ComputeDelegate <Employee>)Delegate.CreateDelegate(typeof(ComputeDelegate <Employee>), mi); foreach (Employee e in employees) { applyRaise(e, (Decimal)0.10); Console.WriteLine(e.Salary); } }
static void Main() { MyClass proc1 = new MyClass(); MyClass proc2 = new MyClass(); ComputeDelegate[] delegates = new ComputeDelegate[] { new ComputeDelegate(proc1.Add), new ComputeDelegate(proc2.Add), new ComputeDelegate(MyClass.Subtract) }; ComputeDelegate chained = (ComputeDelegate)Delegate.Combine(delegates); double combined = chained(4, 5); Console.WriteLine("Output: {0}", combined); }
static void DelegateIntro() { // here we create an instance of our delegate // since HelloWorld() has the same signature (returns void, no params), we can assign it to our delegate MyDelegateType delegateInstance = HelloWorld; // calling delegateInstance() here will simply call HelloWorld() delegateInstance(); ComputeDelegate computeInstance = Compute; int result = computeInstance(5); Console.WriteLine("The result of computeInstance is: " + result); Console.ReadKey(); }
void SimpleDelegate() { ComputeDelegate compute = new ComputeDelegate(Compute); Console.WriteLine("\nPassing method to delegate : " + compute(1, 2)); compute = new ComputeDelegate(delegate(int a, int b) { return(a + b); }); Console.WriteLine("\nPassing anonymous method to delegate : " + compute(1, 2)); compute = new ComputeDelegate((a, b) => a + b); Console.WriteLine("\nPassing Lambda expression to delegate : " + compute(1, 2)); Console.WriteLine("\nPassing method as an argument to a delegate parameter : " + PassingDelegateAsParameter(Compute)); Console.WriteLine("\nPassing anonymous method as an argument to a delegate parameter : " + PassingDelegateAsParameter(delegate(int a, int b) { return(a + b); })); Console.WriteLine("\nPassing Lambda expression as an argument to a delegate parameter : " + PassingDelegateAsParameter((a, b) => a + b)); }
internal static void LoadNativeMethods() { _nativeLibraryManager?.Dispose(); try { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (RuntimeInformation.ProcessArchitecture == Architecture.X64) { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Omnix.Security.win-x64.dll"); } else { throw new NotSupportedException(); } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { if (RuntimeInformation.ProcessArchitecture == Architecture.X64) { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Omnix.Security.linux-x64.so"); } else { throw new NotSupportedException(); } } else { throw new NotSupportedException(); } _compute = _nativeLibraryManager.GetMethod <ComputeDelegate>("compute_Crc32_Castagnoli"); } catch (Exception) { _nativeLibraryManager?.Dispose(); _nativeLibraryManager = null; } }
static Crc32_Castagnoli() { #if Mono #else try { if (System.Environment.Is64BitProcess) { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Library_Security_x64.dll"); } else { _nativeLibraryManager = new NativeLibraryManager("Assemblies/Library_Security_x86.dll"); } _compute = _nativeLibraryManager.GetMethod <ComputeDelegate>("compute_Crc32_Castagnoli"); } catch (Exception e) { Log.Warning(e); } #endif }
int PassingDelegateAsParameter(ComputeDelegate compute) { int a = 2, b = 4; return(compute(a, b)); }