Tuesday, July 8, 2008

Tài liệu tham khảo nhanh dành cho .NET Developer

    Cách thiết lập focus cho một control khác sau khi sử lý sự kiện AfterSelect của TreeView

    TreeView control có thể gây cho bạn khó khăn trong trường hợp này, nó cập nhật lại Node được lựa chọn và thực hiện focus trở lại Node này sau khi xử lý sự kiện. Cách thực hiện cho trường hợp này là làm trễ việc thực thi đoạn code của bạn cho đến khi hoàn thành việc xử lý sự kiện trên control TreeView. Dưới đây là sample cho việc xử lý trường hợp này:

    private delegate void FocusAfterSelect(TreeNode node);
     
    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) 
    {
        this.BeginInvoke(new FocusAfterSelect(HandleAfterSelect), e.Node);
    }
     
    private void HandleAfterSelect(TreeNode node) 
    {
        textBox1.Text = node.Text;
        textBox1.Focus();
    }

    Monday, July 7, 2008

    Truy cập các phương thức private sử dụng Reflection trong .NET.

    Để truy cập vào các phương thức hoặc thuộc tính private của một lớp, ta sẽ sử dụng gói Reflection được xây dựng bên trong .NET Framework. Để minh họa cho khả năng của của Reflection trong .NET Framework, tôi xin đưa ra một ví dụ bên dưới:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
     
    namespace ReflectionSamples
    {
        public class ReflectionSample
        {
            private void Method1()
            {
                Console.WriteLine("Method 1 called");
            }
            
            private int Method2(int x, int y)
            {
                Console.WriteLine("Method 2 called {0}, {1}", x, y);
                return x*y;
            }
            
            internal string RedAlert
            {
                get
                {
                    Console.WriteLine("Red Alert");
                    return "Red Alert";
                }
            }
        }
        
        internal class Program
        {
            private static void Main(string[] args)
            {
                //Tạo instance của lớp mà ta sẽ thực hiện truy cập các hàm private
                MyClass instance = new MyClass();
                
                //Get Type of class
                Type t = typeof (MyClass);
                
                //Lấy thông tin của phương thức Method1
                MethodInfo method1 = t.GetMethod("Method1",
                    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod);
     
                //Gọi phương thức trên đối tượng được tham chiếu bằng instance và truyền tham số
                method1.Invoke(instance, null);
                
                //Truy cập thông tin của Properties
                PropertyInfo property = t.GetProperty("RedAlert", 
                    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty);
                
                //Gọi Properties tương ứng và truyền tham số
                string str = (string)property.GetValue(instance, null);
                
                //Truy cập thông tin của phương thức Method2
                MethodInfo method2 = t.GetMethod("Method2", 
                    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod);
     
                //Gọi phương thức và truyền 2 tham số (các tham số được truyền thông qua danh sách đối tượng
                object o = method2.Invoke(instance, new object[] {2, 3});
                
                //Với trường hợp phương thức có giá trị trả lại
                if (o != null)
                {
                    Console.WriteLine("Method 2 returned: " + o.ToString());
                }
                
                Console.ReadKey();
            }
        }
    }