static void Main(string[] args) { //initialize delegates method1Delegate obj1 = new method1Delegate(method1); string name = obj1.Invoke("kavya"); Console.WriteLine(name); method2Delegate obj2 = new method2Delegate(method2); float result = obj2.Invoke(10, 20, 30); Console.WriteLine(result); method3Delegate obj3 = new method3Delegate(method3); obj3.Invoke("Delegate code"); method4Delegate obj4 = new method4Delegate(method4); obj4.Invoke(10.32f, 20.56f); method5Delegate obj5 = new method5Delegate(method5); bool val = obj5.Invoke(30); Console.WriteLine(val); method6Delegate obj6 = new method6Delegate(method6); bool str = obj6.Invoke("jerry"); Console.WriteLine(str); Console.ReadKey(); }
static void Main(string[] args) { //initialize delegates method1Delegate obj1 = name1 => { return("Hello " + name1); }; string name = obj1.Invoke("kavya"); Console.WriteLine(name); method2Delegate obj2 = (a, b, c) => { return(a * b * c); }; float result = obj2.Invoke(10, 20, 30); Console.WriteLine(result); method3Delegate obj3 = str1 => { Console.WriteLine("program is: " + str1); }; obj3.Invoke("Delegate code"); method4Delegate obj4 = (a, b) => { Console.WriteLine(a + b); }; obj4.Invoke(10.32f, 20.56f); method5Delegate obj5 = value => { if (value > 18) { return(true); } return(false); }; bool val = obj5.Invoke(30); Console.WriteLine(val); method6Delegate obj6 = name1 => { if (name1.Length < 5) { return(true); } return(false); }; bool str = obj6.Invoke("jerry"); Console.WriteLine(str); Console.ReadKey(); }
public static void testClass(string dllPath) { var DLL = Assembly.LoadFile(dllPath); var theType = DLL.GetType("dll_test.Class1"); // 네임스페이스. 클래스 명 if (theType == null) { // not found calss } // 2. 델리게이트를 이용한 방식 MethodInfo minfo = theType.GetMethod("Output"); OutputDelegate _Output = (OutputDelegate)Delegate.CreateDelegate(typeof(OutputDelegate), null, minfo); _Output(@"Hello_2"); MethodInfo method1_p = theType.GetMethod("method1"); method1Delegate _method1 = (method1Delegate)Delegate.CreateDelegate(typeof(method1Delegate), null, method1_p); string outputStr = ""; int result = (int)_method1(@"passArgumentTest", out outputStr); MethodInfo method2_p = theType.GetMethod("method2"); method2_Delegate _method2 = (method2_Delegate)Delegate.CreateDelegate(typeof(method2_Delegate), null, method1_p); outputStr = ""; result = (int)_method2(@"passArgumentTest", ref outputStr); /* * // 1. 메서드 직접 호출이 아닌 type에서 GetMethod로 함수를 찾고 실행시키는 방법 * var c = Activator.CreateInstance(theType); * var method = theType.GetMethod("Output"); * method.Invoke(c, new object[] { @"Hello" }); */ //Console.ReadLine(); // 사용자 입력대기 }