文章分类 -  C#

语言基础
摘要:动态创建菜单(*)•菜单项不仅可以静态设计,也可以动态的增加。–设计父菜单项,在Load事件中 ToolStripItem item = 工具ToolStripMenuItem.DropDownItems.Add("1");编写for循环添加10项–响应菜单事件,item.Click += new EventHandler(item_Click);–如何点击菜单的时候知道点击的是哪个菜单?sender就是被点击的菜单项:ToolStripItem item = sender as ToolStripItem。在添加菜单项的时候将菜单的Tag设置为序号,在item_Click 阅读全文
posted @ 2013-03-18 19:24 Big.Eagle 阅读(198) 评论(0) 推荐(0)
摘要:反射的一个重要应用 •调用private方法,• Person p1 = new Person();• Type type = p1.GetType();• //BindingFlags.Instance表示是实例方法,也就是不是static方法• //MethodInfo mHaha = type.GetMethod("Haha",BindingFlags.NonPublic|BindingFlags.Instance);• //mHaha.Invoke(p1, null);View Code 1 namespace 封闭的类库 2 { 3 public class Pe 阅读全文
posted @ 2013-03-18 14:52 Big.Eagle 阅读(207) 评论(0) 推荐(0)
摘要:View Code 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Type typePerson = typeof(Person); 6 object obj = 7 Activator.CreateInstance(typePerson); 8 //获得Person类的Age属性的描述,和某个对象无关系 9 PropertyInfo propAge ... 阅读全文
posted @ 2013-03-18 14:41 Big.Eagle 阅读(161) 评论(0) 推荐(0)
摘要:•Activator.CreateInstance(Type t)会动态调用类的无参构造函数创建一个对象,返回值就是创建的对象,如果类没有无参构造函数就会报错。View Code 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 string filename = @"D:\我的文档\快盘\传智资料\班级资料\2011年2月28日班 \课上代码\Sln0410\测试程序集\bin\Debug\测试程序集.dll"; 6 //从文件中加载... 阅读全文
posted @ 2013-03-17 22:47 Big.Eagle 阅读(109) 评论(0) 推荐(0)
摘要:反射:Type类(*)•万物皆对象 •Type类可以叫做“类的类”<是对类的抽象>,一个类型对应一个Type类的对象,通过Type对象可以获得类的所有的定义信息,比如类有哪些属性、哪些方法等。Type就是对类的描述。类是对对象的抽象。•获得类的Type对象的方法:–通过类获得Type:Type t = typeof(Person)–通过对象获得类的Type:Type t = p.GetType()–调用Assembly的GetExportedTypes方法可以得到Assembly中定义的所有的public类型。–调用Assembly的GetTypes()方法可以得到Assembly 阅读全文
posted @ 2013-03-17 22:34 Big.Eagle 阅读(90) 评论(0) 推荐(0)
摘要:class Program { static void Main(string[] args) { //Assembly[] asms = // AppDomain.CurrentDomain.GetAssemblies(); //foreach (Assembly asm in asms) //{ //打印程序运行时用到的所有程序集的引用 // Console.WriteLine(asm.Locatio... 阅读全文
posted @ 2013-03-17 22:22 Big.Eagle 阅读(131) 评论(0) 推荐(0)
摘要:View Code 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //这里调用的是MyLazy的构造函数 6 //只有使用Value的时候调用的才是Person的构造函数 7 //实际应用时只有非常消耗内存资源 8 //或者其他非托管资源的类才值得用Lazy<T> 9 MyLazy<Person> person = new MyLazy<Person>();10... 阅读全文
posted @ 2013-03-17 20:29 Big.Eagle 阅读(196) 评论(0) 推荐(0)
摘要:泛型目的:约束容器中能放何种类型的数据,使编程变成类型安全的了。如:•对比ArrayList和List<T>,ArrayList中的数据放进去以后再拿出来都是object标签,因此需要类型转换,而且很难阻止非法类型的加入。List<T>则可以指定盛放的数据的类型。编写泛型类:View Code 1 class MyList<T> 2 { 3 private ArrayList list = new ArrayList(); 4 5 public T this[int index] 6 { 7 get 8 ... 阅读全文
posted @ 2013-03-17 19:13 Big.Eagle 阅读(108) 评论(0) 推荐(0)
摘要:View Code 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 // XElement xPersons = new XElement("Persons"); 6 // XElement xPerson1 = new XElement("Person"); 7 // XElement xPerson2 = new XElement("Person"); 8 // xPerson1.Value... 阅读全文
posted @ 2013-03-17 18:26 Big.Eagle 阅读(103) 评论(0) 推荐(0)
摘要:class Program { static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 10000; i++) { //因为Instance是唯一的实例,所以其他类、任何地方调用 //FileCacheManager.Instance得到的都是同一个实例 ... 阅读全文
posted @ 2013-03-17 14:56 Big.Eagle 阅读(119) 评论(0) 推荐(0)
摘要:View Code 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //Earth e1 = new Earth(); 6 //Earth e2 = new Earth(); 7 Earth earth = Earth.GetEarth(); 8 Earth earth2 = Earth.GetEarth(); 9 Console.WriteLine(10 ... 阅读全文
posted @ 2013-03-16 20:20 Big.Eagle 阅读(76) 评论(0) 推荐(0)
摘要:View Code 1 private void button1_Click(object sender, EventArgs e) 2 { 3 List<string> list = new List<string>(); 4 list.Add("DirectX游戏开发"); 5 list.Add("高等数学"); 6 list.Add("C++开发"); 7 8 BinaryFormatter bf = new BinaryFormatter(); ... 阅读全文
posted @ 2013-03-16 18:52 Big.Eagle 阅读(184) 评论(0) 推荐(0)
摘要:View Code 1 //WebClient wc = new WebClient(); 2 //wc.DownloadFile("http://192.168.1.100/b.xls", @"c:\b.xls"); 3 4 //using(Stream stream = File.OpenRead(@"c:\b.xls")) 5 //{ 6 // HSSFWorkbook workbook = new HSSFWorkbook(stream); 7 // ... 阅读全文
posted @ 2013-03-16 16:33 Big.Eagle 阅读(261) 评论(0) 推荐(0)
摘要:static void Main(string[] args) { FileStream fs = new FileStream(@"c:\1.txt",FileMode.Create); string s = "hello"; //得到字符串的二进制编码形式 byte[] bytes = Encoding.UTF8.GetBytes(s); fs.Write(bytes, 0, bytes.Length);//FileStream会把写的数据放到缓冲区中 //fs.Flush();//强制吧缓冲区中的数据写到文件 fs.Close();//关闭流,在关 阅读全文
posted @ 2013-03-16 15:11 Big.Eagle 阅读(206) 评论(0) 推荐(0)
摘要:string dataDir = AppDomain.CurrentDomain.BaseDirectory; if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\")) { dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName; AppDomain.CurrentDomain.SetData("DataDirectory", dataDir); }详见: 阅读全文
posted @ 2013-03-16 14:15 Big.Eagle 阅读(236) 评论(0) 推荐(0)
摘要:•拷贝文件的两种方式:将源文件内容全部读到内存中,再写到目标文件中;读取源文件的1KB内存,写到目标文件中,再读取源文件的1KB内存,再写到目标文件中……。第二种方式就是一种流(Stream)的操作。•用File.ReadAllText、File.WriteAllText进行文件读写是一次性读、写,如果文件非常大会占内存、慢。需要读一行处理一行的机制,这就是流(Stream)。Stream会只读取要求的位置、长度的内容。•<注意:>就像SqlConnection一样,Stream不会将所有内容一次性读取到内存中,有一个指针,指针指到哪里才能读、写到哪里。•流有很多种类,文件流是其中 阅读全文
posted @ 2013-03-16 11:38 Big.Eagle 阅读(129) 评论(0) 推荐(0)
摘要:http://blog.csdn.net/guxianga/article/details/1791195 阅读全文
posted @ 2013-03-15 19:01 Big.Eagle 阅读(126) 评论(0) 推荐(0)