private void View() { dataGridView1.Rows.Clear(); try{ SampleGenericDelegate <int, Document[]> del = new SampleGenericDelegate <int, Document[]>(serverClient.GetAllDocumentPerson); IAsyncResult result = del.BeginInvoke(person.Nif, null, null); Document[] list = del.EndInvoke(result); if (list == null) { return; } documents = new List <Document>(list); documents.ForEach(AddDocument); } catch (Exception exception) { var info = new InfoForm(); info.Add(exception.Message); info.ShowDialog(this); info.Dispose(); } }
private void View() { dataGridView1.Rows.Clear(); try{ SampleGenericDelegate<int,Document[]> del = new SampleGenericDelegate<int,Document[]>(serverClient.GetAllDocumentPerson); IAsyncResult result = del.BeginInvoke(person.Nif, null, null); Document[] list = del.EndInvoke(result); if (list == null) return; documents = new List<Document>(list); documents.ForEach(AddDocument); } catch (Exception exception) { var info = new InfoForm(); info.Add(exception.Message); info.ShowDialog(this); info.Dispose(); } }
static void Main() { //IComparer<Circle> areaComparer = new CircleComparer(); IComparer <IShape> areaComparer = new AreaComparer(); ComparisonHelper <IShape, Circle> areaComp = new ComparisonHelper <IShape, Circle>(areaComparer); List <Circle> circles = new List <Circle>(); circles.Add(new Circle(2)); circles.Add(new Circle(20)); circles.Add(new Circle(12)); circles.Add(new Circle(14)); circles.Sort(areaComp); foreach (Circle c in circles) { Console.WriteLine($"Circle radius:{c.Radius}"); } //委托的变体 SampleDelegate dNonGeneric = ASecondRFirst; SampleDelegate dNonGenericConversion = AFirstRSecond; SampleGenericDelegate <Second, First> dGeneric = ASecondRFirst; SampleGenericDelegate <Second, First> dGenericConversion = AFirstRSecond; SampleGenericDelegate <string> hahah = () => ""; SampleGenericDelegate <object> obj = hahah; Console.ReadKey(); }
private void saveButton_Click(object sender, EventArgs e) { try { regist = new Regist(); regist.Date = dateTimePicker1.Value; regist.Description = richTextBox1.Text; regist.Code = textBox3.Text; SampleGenericDelegate <Regist, int> del = new SampleGenericDelegate <Regist, int>(serverClient.InsertRegist); IAsyncResult result = del.BeginInvoke(regist, null, null); regist.Id = del.EndInvoke(result); SarcIntelService.PersonView.PersonEditor person = new SarcIntelService.PersonView.PersonEditor(serverClient, regist); person.ShowDialog(this); person.Close(); } catch (Exception exc) { var info = new InfoForm(); info.Add(exc.Message); info.ShowDialog(this); info.Dispose(); } }
public PersonView(ServerServiceClient serverClient,Person p) { InitializeComponent(); person = p; nameBoxText.Text = p.Name; birthBoxText.Text = p.Birthday.ToShortDateString(); moradaBoxText.Text = p.Address; textBox3.Text = p.Nif.ToString(); TimeSpan x = DateTime.Now - p.Birthday; idadeBoxText.Text = (x.Days/365).ToString(); nacioBoxText.Text = p.Birthplace; this.serverClient = serverClient; try { SampleGenericDelegate<int, Regist[]> del = new SampleGenericDelegate<int, Regist[]>(serverClient.GetRegists); IAsyncResult result = del.BeginInvoke(person.Nif, null, null); registos = del.EndInvoke(result); if (registos != null) SetRegists(); } catch (Exception e) { var info = new InfoForm(); info.Add(e.Message); info.ShowDialog(this); info.Dispose(); } }
public void NonGeneric() { // Assigning a method with a matching signature // to a non-generic delegate. No conversion is necessary. SampleDelegate dNonGeneric = ASecondRFirst; // Assigning a method with a more derived return type // and less derived argument type to a non-generic delegate. // The implicit conversion is used. SampleDelegate dNonGenericConversion = AFirstRSecond; // Assigning a method with a matching signature to a generic delegate. // No conversion is necessary. SampleGenericDelegate <Second, First> dGeneric = ASecondRFirst; // Assigning a method with a more derived return type // and less derived argument type to a generic delegate. // The implicit conversion is used. SampleGenericDelegate <Second, First> dGenericConversion = AFirstRSecond; SampleGenericDelegate <Second, Second> dGenericConversion2 = AFirstRSecond; // in A, out T // 1 //dGenericConversion = dGenericConversion2; // 2 //var genList = new List<SampleGenericDelegate<Second, First>>(); //genList.Add(dGenericConversion); //genList.Add(dGenericConversion2); }
public static void Test() { SampleGenericDelegate <String> dString = () => " "; // You can assign delegates to each other, // because the type T is declared covariant. SampleGenericDelegate <Object> dObject = dString; }
public static void Test() { SampleGenericDelegate <String> dString = () => " "; // You can assign the dObject delegate // to the same lambda expression as dString delegate // because of the variance support for // matching method signatures with delegate types. SampleGenericDelegate <Object> dObject = () => " "; // The following statement generates a compiler error // because the generic type T is not marked as covariant. // SampleGenericDelegate <Object> dObject = dString; }
public void Generic() { SampleGenericDelegate <string> dString = () => " "; // You can assign delegates to each other, // because the type T is declared covariant. // Если убрать Out, то не будет рабоать SampleGenericDelegate <object> dObject = dString; var genList = new List <SampleGenericDelegate <object> >(); genList.Add(dString); genList.Add(dObject); }
public void saveButton_Click(object sender, EventArgs e) { var document = new Document(); document.code = textBox1.Text; document.emission_local = textBox2.Text; document.emission_date = dateTimePicker2.Value; document.expiration_date = dateTimePicker1.Value; if (document.emission_date > document.expiration_date) { InfoForm info = new InfoForm(); info.Add("Aviso: Data de Emissao é maior do que data de Expiraçao !"); info.Enabled = false; info.Visible = true; } else { DocumentType doctype = documents.FirstOrDefault(d => d.Name.Equals(comboBox1.Text)); if (doctype == null) { return; } document.Type = doctype; serverClient.InsertPersonAsync(person); document.Person = person; try { SampleGenericDelegate <Document, int> del = new SampleGenericDelegate <Document, int>(serverClient.InsertDocument); IAsyncResult result = del.BeginInvoke(document, null, null); del.EndInvoke(result); Close(); } catch (Exception exception) { var info = new InfoForm(); info.Add(exception.Message); info.ShowDialog(this); info.Dispose(); } } }
public void saveButton_Click(object sender, EventArgs e) { var document = new Document(); document.code = textBox1.Text; document.emission_local = textBox2.Text; document.emission_date = dateTimePicker2.Value; document.expiration_date = dateTimePicker1.Value; if (document.emission_date > document.expiration_date) { InfoForm info = new InfoForm(); info.Add("Aviso: Data de Emissao é maior do que data de Expiraçao !"); info.Enabled = false; info.Visible = true; } else { DocumentType doctype = documents.FirstOrDefault(d => d.Name.Equals(comboBox1.Text)); if (doctype == null) return; document.Type = doctype; serverClient.InsertPersonAsync(person); document.Person = person; try { SampleGenericDelegate<Document, int> del = new SampleGenericDelegate<Document, int>(serverClient.InsertDocument); IAsyncResult result = del.BeginInvoke(document, null, null); del.EndInvoke(result); Close(); } catch (Exception exception) { var info = new InfoForm(); info.Add(exception.Message); info.ShowDialog(this); info.Dispose(); } } }
static void Main(string[] args) { // Assigning a method with a matching signature // to a non-generic delegate. No conversion is necessary. SampleDelegate dNonGeneric = ASecondRFirst; // Assigning a method with a more derived return type // and less derived argument type to a non-generic delegate. // The implicit conversion is used. SampleDelegate dNonGenericConversion = AFirstRSecond; // Assigning a method with a matching signature to a generic delegate. // No conversion is necessary. SampleGenericDelegate <Second, First> dGeneric = ASecondRFirst; // Assigning a method with a more derived return type // and less derived argument type to a generic delegate. // The implicit conversion is used. SampleGenericDelegate <Second, First> dGenericConversion = AFirstRSecond; }
private void AddRegist(Regist r) { SampleGenericDelegate <int, CrimeType[]> del = new SampleGenericDelegate <int, CrimeType[]>(serverClient.GetAllCrimeTypeByRegist); IAsyncResult result = del.BeginInvoke(r.Id, null, null); var crimes = del.EndInvoke(result); DataGridViewComboBoxColumn comb = (DataGridViewComboBoxColumn)dataGridView2.Columns[1]; foreach (CrimeType c in crimes) { comb.Items.Add(c.Name); } dataGridView2.Rows.Add(new object[] { r.Id, crimes[0].Name, r.Date.ToShortDateString() }); }
public Participants(ServerServiceClient serverClient,Regist regist) { InitializeComponent(); this.serverClient = serverClient; SampleGenericDelegate<int, Person[]> del = new SampleGenericDelegate<int, Person[]>(serverClient.GetAllPersonByIdRegist); IAsyncResult result= del.BeginInvoke(regist.Id, null, null); this.persons=del.EndInvoke(result); this.regist = regist; foreach (Person p in persons) dataGridView1.Rows.Add(new object[] { p.Nif, p.Name, p.Address, p.Birthday.ToShortDateString(), p.Birthplace, }); }
private void AddRegist(Regist r) { SampleGenericDelegate<int,CrimeType[]> del = new SampleGenericDelegate<int,CrimeType[]>(serverClient.GetAllCrimeTypeByRegist); IAsyncResult result = del.BeginInvoke(r.Id,null, null); CrimeType[] crimes = del.EndInvoke(result); DataGridViewComboBoxColumn comb = (DataGridViewComboBoxColumn)dataGridView1.Columns[1]; foreach (CrimeType c in crimes) comb.Items.Add(c.Name); SampleGenericDelegate<int, int> dele = new SampleGenericDelegate<int, int>(serverClient.GetNumberOfParticipants); IAsyncResult result2 = dele.BeginInvoke(r.Id, null, null); int count = dele.EndInvoke(result2); dataGridView1.Rows.Add(new object[] {r.Id,crimes[0].Name,r.Date.ToShortDateString(),count}); }
public Participants(ServerServiceClient serverClient, Regist regist) { InitializeComponent(); this.serverClient = serverClient; SampleGenericDelegate <int, Person[]> del = new SampleGenericDelegate <int, Person[]>(serverClient.GetAllPersonByIdRegist); IAsyncResult result = del.BeginInvoke(regist.Id, null, null); this.persons = del.EndInvoke(result); this.regist = regist; foreach (Person p in persons) { dataGridView1.Rows.Add(new object[] { p.Nif, p.Name, p.Address, p.Birthday.ToShortDateString(), p.Birthplace, }); } }
public static void Test() { SampleGenericDelegate <String> dString = () => " "; // You can assign the dObject delegate // to the same lambda expression as dString delegate // because of the variance support for // matching method signatures with delegate types. SampleGenericDelegate <Object> dObject = () => " "; // The following statement generates a compiler error // because the generic type T is not marked as covariant. //SampleGenericDelegate <Object> dObject = dString; Action <object> actObj = x => Console.WriteLine("object: {0}", x); Action <string> actStr = x => Console.WriteLine("string: {0}", x); // All of the following statements throw exceptions at run time. //Action<string> actCombine = actStr + actObj; //actStr += actObj; //Delegate.Combine(actStr, actObj); }
public PersonView(ServerServiceClient serverClient, Person p) { InitializeComponent(); person = p; nameBoxText.Text = p.Name; birthBoxText.Text = p.Birthday.ToShortDateString(); moradaBoxText.Text = p.Address; textBox3.Text = p.Nif.ToString(); TimeSpan x = DateTime.Now - p.Birthday; idadeBoxText.Text = (x.Days / 365).ToString(); nacioBoxText.Text = p.Birthplace; this.serverClient = serverClient; try { SampleGenericDelegate <int, Regist[]> del = new SampleGenericDelegate <int, Regist[]>(serverClient.GetRegists); IAsyncResult result = del.BeginInvoke(person.Nif, null, null); registos = del.EndInvoke(result); if (registos != null) { SetRegists(); } } catch (Exception e) { var info = new InfoForm(); info.Add(e.Message); info.ShowDialog(this); info.Dispose(); } }
private void AddRegist(Regist r) { SampleGenericDelegate <int, CrimeType[]> del = new SampleGenericDelegate <int, CrimeType[]>(serverClient.GetAllCrimeTypeByRegist); IAsyncResult result = del.BeginInvoke(r.Id, null, null); CrimeType[] crimes = del.EndInvoke(result); DataGridViewComboBoxColumn comb = (DataGridViewComboBoxColumn)dataGridView1.Columns[1]; foreach (CrimeType c in crimes) { comb.Items.Add(c.Name); } SampleGenericDelegate <int, int> dele = new SampleGenericDelegate <int, int>(serverClient.GetNumberOfParticipants); IAsyncResult result2 = dele.BeginInvoke(r.Id, null, null); int count = dele.EndInvoke(result2); dataGridView1.Rows.Add(new object[] { r.Id, crimes[0].Name, r.Date.ToShortDateString(), count }); }
static void Main(string[] args) { // Assignment compatibility // 赋值兼容 string str = "test"; // An object of a more derived type is assigned to an object of a less derived type. // 派生程度较大的类型可以赋值给派生程度较小的类型 object obj = str; object[] strs = new string[] { "a", "b", "c" }; strs[0] = 3;// ArrayTypeMismatchException exception List <Giraffe> giraffes = new List <Giraffe>(); giraffes.Add(new Giraffe()); //List<Animal> animals = giraffes; //animals.Add(new Lion()); // Aargh! // Assigning a method with a matching signature // to a non-generic delegate. No conversion is necessary. // 将签名完全匹配的方法赋值给委托,无需转换 SampleDelegate d1 = RL1PL3; // Assigning a method with a more derived return type // and less derived argument type to a non-generic delegate. // The implicit conversion is used. // 将一个返回值的派生程度更大(协变),参数值的派生程度更小(逆变)的方法赋值给委托 SampleDelegate d2 = RL2PL2; // 方法返回值派生程度继续变大,参数值的派生程度继续变小 SampleDelegate dNonGenericConversion1 = RL3PL1; // 一个委托就可以适用于全部返回值、参数组合 // 使代码更通用 SampleDelegate sd1 = RL1PL1; SampleDelegate sd2 = RL1PL2; SampleDelegate sd3 = RL1PL3; SampleDelegate sd4 = RL2PL1; SampleDelegate sd5 = RL2PL2; SampleDelegate sd6 = RL2PL3; SampleDelegate sd7 = RL3PL1; SampleDelegate sd8 = RL3PL2; SampleDelegate sd9 = RL3PL3; // 泛型委托类似: SampleGenericDelegate <L3, L1> dg1 = RL1PL1; SampleGenericDelegate <L3, L1> dg2 = RL1PL2; SampleGenericDelegate <L3, L1> dg3 = RL1PL3; SampleGenericDelegate <L3, L1> dg4 = RL2PL1; SampleGenericDelegate <L3, L1> dg5 = RL2PL2; SampleGenericDelegate <L3, L1> dg6 = RL2PL3; SampleGenericDelegate <L3, L1> dg7 = RL3PL1; SampleGenericDelegate <L3, L1> dg8 = RL3PL2; SampleGenericDelegate <L3, L1> dg9 = RL3PL3; SampleGenericDelegate1 <string> dString = () => " "; SampleGenericDelegate1 <object> dObject1 = () => " "; // The following statement generates a compiler error // 如果不使用 out 显示指明委托的结果支持逆变,下面的一行代码无法编译通过 SampleGenericDelegate1 <object> dObject = dString; IEnumerable <string> listL3 = new List <string>(); IEnumerable <object> lis = listL3; // 协变 var employees = new List <Teacher>(); PrintFullName(employees); // 协变 var students = new List <Student>(); PrintFullName(students); // 协变 逆变只适用于引用类型 IEnumerable <int> listOfInt = new List <int>(); //IEnumerable<object> lo = listOfInt; // You can pass ShapeAreaComparer, which implements IComparer<Shape>, // even though the constructor for SortedSet<Circle> expects // IComparer<Circle>, because type parameter T of IComparer<T> is // contravariant. // SortedSet<Circle> 的构造函数需要IComparer<Circle>,但仍然可以传入是实现IComparer<Shape>的ShapeAreaComparer SortedSet <Circle> circlesByArea = new SortedSet <Circle>(new ShapeAreaComparer()) { new Circle(7.2), new Circle(100), null, new Circle(.01) }; foreach (Circle c in circlesByArea) { Console.WriteLine(c == null ? "null" : "Circle with area " + c.Area); } }
private void AddRegist(Regist r) { SampleGenericDelegate<int, CrimeType[]> del = new SampleGenericDelegate<int, CrimeType[]>(serverClient.GetAllCrimeTypeByRegist); IAsyncResult result = del.BeginInvoke(r.Id, null, null); var crimes = del.EndInvoke(result); DataGridViewComboBoxColumn comb = (DataGridViewComboBoxColumn)dataGridView2.Columns[1]; foreach (CrimeType c in crimes) comb.Items.Add(c.Name); dataGridView2.Rows.Add(new object[] { r.Id, crimes[0].Name, r.Date.ToShortDateString() }); }
private void saveButton_Click(object sender, EventArgs e) { try { regist = new Regist(); regist.Date = dateTimePicker1.Value; regist.Description = richTextBox1.Text; regist.Code = textBox3.Text; SampleGenericDelegate<Regist,int> del = new SampleGenericDelegate<Regist,int>(serverClient.InsertRegist); IAsyncResult result = del.BeginInvoke(regist,null, null); regist.Id = del.EndInvoke(result); SarcIntelService.PersonView.PersonEditor person = new SarcIntelService.PersonView.PersonEditor(serverClient,regist); person.ShowDialog(this); person.Close(); } catch (Exception exc) { var info = new InfoForm(); info.Add(exc.Message); info.ShowDialog(this); info.Dispose(); } }