private void button2_Click(object sender, EventArgs e) { Dele temp = null; try { switch(comboBox1.SelectedItem.ToString()) { case "+": temp = new Dele(cal.Add); break; case "-": temp = new Dele(cal.Sub); break; case "*": temp = new Dele(cal.Mul); break; case "/": temp = new Dele(cal.Div); break; } } catch { MessageBox.Show("정상적으로 입력해 주세요"); } if(temp != null && textBox2.Text != "0") temp.BeginInvoke(int.Parse(textBox1.Text), int.Parse(textBox2.Text), Callback, null); }
extern static bool TakeDelegateByOutRefParam([MarshalAs(UnmanagedType.FunctionPtr)] out Dele dele);
extern static Dele ReturnDelegatePtrByVal([MarshalAs(UnmanagedType.Interface)] Dele dele);
extern static bool Take_DelegatePtrByOutRefParam([Out, MarshalAs(UnmanagedType.Interface)] out Dele dele, [MarshalAs(UnmanagedType.Interface)] Dele deleHelper);
public C() { field = C.Foo; }
extern static Dele ReturnDelegatePtrByVal([MarshalAs(UnmanagedType.IDispatch)] Dele dele);
extern static bool Take_DelegatePtrByInRefParam([In, MarshalAs(UnmanagedType.IDispatch)] ref Dele dele);
extern static bool TakeDelegateByInValParam([In] Dele dele);
extern static bool TakeDelegateByRefParam(ref Dele dele);
extern static bool TakeDelegateByValParam(Dele dele);
private static void Menu() { TablePainter obj = new TablePainter(); Console.WriteLine("Выберите пункт меню:"); Console.WriteLine(" 1 - Синус"); Console.WriteLine(" 2 - Синус Квадрат Плюс Кос"); Console.WriteLine(" 3 - 1/х"); int sw = int.Parse(Console.ReadLine()); switch (sw) { case 1: { Dele dele = new Dele(obj.SinTable); double[,] table = dele(); Console.WriteLine("Table sin"); Console.WriteLine("0\t30\t45\t90\t180\t270\t360"); for (int i = 1; i < table.GetLength(0); i++) { for (int j = 0; j < table.GetLength(1); j++) { Console.Write(Math.Round(table[i, j], 2) + "\t"); } Console.WriteLine(); } break; } case 2: { Dele dele = new Dele(obj.SinAndCosTable); double[,] table2 = dele(); Console.WriteLine("Table sinAndCos "); Console.WriteLine("0\t30\t45\t90\t180\t270\t360"); for (int i = 1; i < table2.GetLength(0); i++) { for (int j = 0; j < table2.GetLength(1); j++) { Console.Write(Math.Round(table2[i, j], 2) + "\t"); } Console.WriteLine(); } break; } case 3: { Dele dele = new Dele(obj.OneDelX); double[,] table3 = dele(); Console.WriteLine("Table 1 / x "); Console.WriteLine("0\t30\t45\t90\t180\t270\t360"); for (int i = 1; i < table3.GetLength(0); i++) { for (int j = 0; j < table3.GetLength(1); j++) { Console.Write(Math.Round(table3[i, j], 2) + "\t"); } Console.WriteLine(); } break; } } }
static void Main(string[] args) { #region 枚举器 Enumerator.Test(); #endregion #region 匿名委托 Del de = delegate(int k) { Console.WriteLine(k); }; de(1); #endregion #region 线程 Thread thread = new Thread(delegate() { Console.WriteLine("start"); }); thread.Start(); #endregion #region 委托 int i = 1; Del del = delegate(int a) { i++; Console.WriteLine(i); }; del(1); //lambda表达式 Dele lam = x => x * x; Console.WriteLine(lam(2)); //lambda表达式(用于表达式树) Expression <Dele> la = a => a * a; //lambda语句 Del deOne = (aa) => { Console.WriteLine(aa); }; deOne(6); #endregion #region 表达式树 //创建一个表达式树中的参数,作为一个节点,这里是最下层的节点 ParameterExpression aaa = Expression.Parameter(typeof(int), "i"); ParameterExpression bbb = Expression.Parameter(typeof(int), "j"); //这里i*j,生成表达式树中的一个节点,比上面节点高一级 BinaryExpression resultOne = Expression.Multiply(aaa, bbb); ParameterExpression c = Expression.Parameter(typeof(int), "w"); ParameterExpression d = Expression.Parameter(typeof(int), "x"); BinaryExpression resultTwo = Expression.Multiply(c, d); //运算两个中级节点,产生终结点 BinaryExpression result = Expression.Add(resultOne, resultTwo); Expression <Func <int, int, int, int, int> > lambda = Expression.Lambda <Func <int, int, int, int, int> >(result, aaa, bbb, c, d); Console.WriteLine(lambda); Func <int, int, int, int, int> fu = lambda.Compile(); Console.WriteLine(fu(1, 2, 3, 4)); #endregion #region reflection Man man = new Man(); Type manTypesTwo = man.GetType(); Type manTypes = typeof(Man); MemberInfo[] members = manTypes.GetMembers(); MethodInfo[] methods = manTypes.GetMethods(); FieldInfo[] fields = manTypes.GetFields(); PropertyInfo[] proerties = manTypes.GetProperties(); string mem = ""; bool met = false; foreach (MemberInfo item in members) { mem = item.Name; } foreach (MethodInfo item in methods) { met = item.IsPublic; } foreach (FieldInfo item in fields) { } foreach (PropertyInfo item in proerties) { } #endregion #region 算法 //斐波那契数列 Console.WriteLine("斐波那契数列:{0}", Algorithm.Reverse(7)); //n!的阶乘 Console.WriteLine("n!的阶乘:{0}", Algorithm.Factorial(5)); //冒泡排序 //TestOne.BubbleSort(new int[] { 5, 4, 3, 2, 1 }); //冒泡排序2 Algorithm.BubbleSort2(new int[] { 5, 4, 3, 2, 1 }); #endregion #region 基础知识 BaseKonwledes.EqualDifference(); #endregion #region 算法 int aaaa = 1, bbbb = 2; Algorithm.TwoExchange(ref aaaa, ref bbbb); Algorithm.FibonacciStack(7); #endregion #region 基础知识-继承 Parent ch = new Child(); ch.SetName("a"); ch.GetName(); Child cc = new Child(); Console.WriteLine("{{0}} = {0}", 1); //int? ab; //Console.WriteLine("ab:", ab.Value); //Parent p; //Console.WriteLine("Parent:",p); #endregion #region 基础-引用类型 Refference re = new Refference(); int numA = 1; BaseKonwledes.Refference(re, numA); Console.WriteLine("re.num :{0}", re.num); Console.WriteLine("num :{0}", numA); Refference ree = new Refference(); int numB = 1; BaseKonwledes.Refference(ref ree, ref numB); Console.WriteLine("re.num :{0}", ree.num); Console.WriteLine("num :{0}", numB); #endregion #region 基础-ref-out Refference myRef = new Refference(); BaseKonwledes.MyRefAsParameter(myRef); Console.WriteLine("myRef :{0} ", myRef.num); Refference myOut; BaseKonwledes.MyOutAsParameter(out myOut); Console.WriteLine("myOut.num :{0}", myOut.num); #endregion #region 参数数组 BaseKonwledes.ParamsArray(); BaseKonwledes.ParamsArray(1); BaseKonwledes.ParamsArray(1, 2); #endregion #region 二分查找 Console.WriteLine("二分查找"); Console.WriteLine(Algorithm.BinarySearch(new int[] { 1, 2, 3, 4, 5 }, 4)); Console.WriteLine(Algorithm.BinarySearch2(new int[] { 1, 2, 3, 4 }, 0, 3, 1)); Console.WriteLine(Algorithm.Binary3(new int[] { 1, 2, 3, 4 }, 0)); #endregion #region 泛型 GenericParadigm.MyStackGeneric(); //泛型方法、推断类型 GenericParadigm.GenericFunctions(); //泛型类的扩展 GenericParadigm.GenericClassExt(); //泛型结构 GenericParadigm.GenericStruct(); //泛型委托 GenericParadigm.GenericDelegate(); //泛型接口 GenericParadigm.GenericInterface(); #endregion #region 基础-反射 ReflectionSyntax.ExcuteMethod(); #endregion #region 反射 Reflection.getAllTypes(); #endregion #region linq Linq.LinqTest(); #endregion #region 算法 Algorithm.IsBracketEquals("1{2[3]4"); #endregion #region base Algorithm.RandomAZ(); BaseKonwledes.StringAndStringBuilder(); #endregion #region 集合 BaseOne.TestList(); #endregion #region 表达式树 ExpressionList.One(); ExpressionList.Two(); #endregion #region 引用类型 List <string> list = new List <string>() { "1", "2", "3" }; BaseOne baseOne = new BaseOne(); List <string> one = baseOne.GetListOne(list); one.Add("4"); List <string> two = baseOne.GetListOne(list); string temp = string.Empty; one.ForEach(f => { temp += f + " "; }); Console.WriteLine("one: {0}", temp); temp = string.Empty; two.ForEach(f => { temp += f + " "; }); Console.WriteLine("two: {0}", temp); #endregion }
extern static bool TakeDelegateByInOutRefParam([In, Out, MarshalAs(UnmanagedType.FunctionPtr)] ref Dele dele);
public C() { E += C.Foo1; E += C.Foo2; }
extern static bool TakeDelegateByInRefParam([In] ref Dele dele);
public C() { field = C.Foo1; E += C.Foo1; E += C.Foo2; }
extern static bool TakeDelegateByOutValParam([Out] Dele dele);
public VAL Method<VAL> (Generic <KEY> k) { Dele<VAL,Generic <KEY>> t = new Dele<VAL,Generic <KEY>> (Test<VAL>); return t (k); }
extern static bool TakeDelegateByOutRefParam(out Dele dele);
extern static bool Take_DelegatePtrByInOutValParam([In, Out, MarshalAs(UnmanagedType.IDispatch)] Dele dele);
extern static bool TakeDelegateByInOutValParam([In, Out] Dele dele);
extern static void TakeDelegateAsInterface([MarshalAs(UnmanagedType.Interface)] Dele dele);
extern static bool TakeDelegateByInOutRefParam([In, Out] ref Dele dele);
extern static bool Take_DelegatePtrByOutValParam([Out, MarshalAs(UnmanagedType.Interface)] Dele dele);
extern static bool Take_DelegatePtrByInOutRefParam([In, Out, MarshalAs(UnmanagedType.Interface)] ref Dele dele);
public C() { field = C.Foo3; E += C.Foo1; E += C.Foo2; }
static int Main() { try{ Console.WriteLine("Scenario 1 : Delegate marshaled by val with attribute [MarshalAs(UnmanagedType.Interface)]."); Dele dele1 = new Dele(CommonMethod1); dele1 += CommonMethod2; dele1 += CommonMethod3; Assert.IsTrue(Take_DelegatePtrByValParam(dele1), "Take_DelegatePtrByValParam"); Console.WriteLine("\n\nScenario 2 : Delegate marshaled by ref with attribute [MarshalAs(MarshalAs(UnmanagedType.Interface)]."); Dele dele2 = new Dele(CommonMethod1); dele2 += CommonMethod2; dele2 += CommonMethod3; Assert.IsTrue(Take_DelegatePtrByRefParam(ref dele2), "Take_DelegatePtrByRefParam"); Assert.IsNull(dele2, "dele2 should equal to null"); Console.WriteLine("\n\nScenario 3 : Delegate marshaled by val with attribute [In,MarshalAs(UnmanagedType.Interface)]."); Dele dele3 = new Dele(CommonMethod1); dele3 += CommonMethod2; dele3 += CommonMethod3; Assert.IsTrue(Take_DelegatePtrByInValParam(dele3), "Take_DelegatePtrByInValParam"); Console.WriteLine("\n\nScenario 4 : Delegate marshaled by ref with attribute [In,MarshalAs(UnmanagedType.Interface)]."); Dele dele4 = new Dele(CommonMethod1); dele4 += CommonMethod2; dele4 += CommonMethod3; Assert.IsTrue(Take_DelegatePtrByInRefParam(ref dele4), "Take_DelegatePtrByInRefParam"); Assert.IsNotNull(dele4, "dele4 does't set to null correctly."); Console.WriteLine("\n\nScenario 5 : Delegate marshaled by val with attribute [Out,MarshalAs(UnmanagedType.Interface)]."); Dele dele5 = new Dele(CommonMethod1); dele5 += CommonMethod2; dele5 += CommonMethod3; Assert.IsTrue(Take_DelegatePtrByOutValParam(dele5), "Take_DelegatePtrByOutValParam"); Assert.IsNotNull(dele5, "dele5 does't set to null correctly"); Console.WriteLine("\n\nScenario 6 : Delegate marshaled by ref with attribute [Out,MarshalAs(UnmanagedType.Interface)]."); Dele dele6 = null; Dele deleHelper = new Dele(CommonMethod1); deleHelper += CommonMethod2; Assert.IsTrue(Take_DelegatePtrByOutRefParam(out dele6, deleHelper), "Take_DelegatePtrByOutRefParam"); dele6(); Assert.AreEqual(COMMONMETHOD1_RESULT, RetFieldResult1(), "RetFieldResult1 return value is wrong"); Assert.AreEqual(COMMONMETHOD2_RESULT, RetFieldResult2(), "RetFieldResult2 return value is wrong "); Console.WriteLine("\n\nScenario 7 : Delegate marshaled by val with attribute [In,OutMarshalAs(UnmanagedType.Interface)]."); Dele dele7 = new Dele(CommonMethod1); dele7 += CommonMethod2; dele7 += CommonMethod3; Assert.IsTrue(Take_DelegatePtrByInOutValParam(dele7), "Take_DelegatePtrByInOutValParam"); Console.WriteLine("\n\nScenario 8 : Delegate marshaled by ref with attribute [In,OutMarshalAs(MarshalAs(UnmanagedType.Interface)]."); Dele dele8 = new Dele(CommonMethod1); dele8 += CommonMethod2; dele8 += CommonMethod3; Assert.IsTrue(Take_DelegatePtrByInOutRefParam(ref dele8), "Take_DelegatePtrByInOutRefParam"); Assert.IsTrue(dele8 == null, "dele8 does't set to null correctly."); Console.WriteLine("\n\nScenario 9 : return Delegate marshaled by val with attribute [return:MarshalAs(UnmanagedType.Interface)]."); Dele dele9 = new Dele(CommonMethod1); dele9 += CommonMethod2; dele9 += CommonMethod3; Dele tempDele = ReturnDelegatePtrByVal(dele9); tempDele(); Assert.AreEqual(COMMONMETHOD1_RESULT, RetFieldResult1(), "RetFieldResult1() return value is wrong"); Assert.AreEqual(COMMONMETHOD2_RESULT, RetFieldResult2(), "RetFieldResult2() return value is wrong"); Assert.AreEqual(COMMONMETHOD3_RESULT, RetFieldResult3(), "RetFieldResult3() return value is wrong"); return(100); } catch (Exception e) { Console.WriteLine($"Test Failure: {e}"); return(101); } }
extern static bool TakeDelegateByOutValParam([Out, MarshalAs(UnmanagedType.FunctionPtr)] Dele dele);