零散的代码-1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
        [Developer("李四", "1", Reviewed = true)]
        public void SomeMethod()
        {           
        }

        public static void GetAttribute(Type t)
        {
            DeveloperAttribute myAtt = (DeveloperAttribute)Attribute.GetCustomAttribute(t, typeof(DeveloperAttribute));
            if (myAtt == null)
            {
                Console.WriteLine("没找到特性");
            }
            else
            {
                Console.WriteLine("特性名 {0}", myAtt.Name);
                Console.WriteLine("特性名 {0}", myAtt.Level);
                Console.WriteLine("特性名 {0}", myAtt.Reviewed);
            }
        }

        static void Main(string[] args)
        {
           
        }
    }

    [AttributeUsage(AttributeTargets.All)]
    public class DeveloperAttribute : System.Attribute
    {
        private string name;
        private string level;
        private bool reviewed;
   
        public  DeveloperAttribute(string name,string level)
        {
            this.name = name;
            this.level = level;
            this.reviewed = false;          
        }

        public virtual string Name
        {
            get { return name; }
        }

        public virtual string Level
        {
            get { return level; }
        }

        public virtual bool Reviewed
        {         
            get
            {
                return reviewed ;
            }
            set
            {
                reviewed = value;
            }
        }  
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{

    [Obsolete("该方法已经过时",true)]
    public class OldClass
    {
        [Obsolete("该方法已经过时 请使用NEW",true )]
        public void OldMethod()
        {
            Console.WriteLine("old");
        }

        public void NewMethod()
        {
            Console.WriteLine("New");
        }
    }
   
    class Program
    {
        static void Main(string[] args)
        {

            OldClass o = new OldClass();
            o.OldMethod();
            o.NewMethod();
        }
    }
}
 


//#define DEBUG //这里定义条件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ConsoleApplication3
{
    class Program
    {
        [DllImport("User32.dll")]
        public static extern int MessageBox(int hParent, string Message, string Caption, int Type);

        static void Main(string[] args)
        {
            DisplayRunningMessage();

            MessageBox(0, "helolo", "Message", 0);
            Console.ReadLine();
       
        }

        [Conditional("DEBUG")]

        private static void DisplayRunningMessage()
        {
            Console.WriteLine("开始运行Main子程序。当前时间是" + DateTime.Now);
        }

        [Conditional("DEBUG")]
        [Obsolete]
        private static void DisplayDebugMessage()
        {
            Console.WriteLine("开始Main子程序");
        }
      
    }
}

 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;

namespace ConsoleApplication9
{
    public class Test
    {
        public static Assembly CreateAssembly()
        {
            AssemblyName assemblyName = new AssemblyName();
            assemblyName.Name = "math";

            //创建程序集
            AssemblyBuilder createdAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);

            //定义程序集模块
            ModuleBuilder assemblyMouble = createdAssembly.DefineDynamicModule("mathModule","math.dll");

            //定义类型
            TypeBuilder mathType = assemblyMouble.DefineType("IntOp", TypeAttributes.Public | TypeAttributes.Class);

            //创建方法
            System.Type[] paramTypes = new Type[]
            {
                typeof(int),typeof(int)
            };

            ModuleBuilder addMethod = mathType.DefineMethod("Add", MethodAttributes.Public, typeof(int),paramTypes);
           
            //创建方法的参数

            ParameterBuilder param1 = addMethod.DefineParameter(1, ParameterAttributes.In, "num1"); //--点不出来
            ParameterBuilder param2 = addMethod.DefineParameter(2, ParameterAttributes.In, "num2"); //--点不出来

            //生成方法的中间语言代码
            ILGenerator ilGenerator = addMethod.GetILGenerator(); //--点不出来
            ilGenerator.Emit(OpCodes.Ldarg_1);
            ilGenerator.Emit(OpCodes.Ldarg_2);
            ilGenerator.Emit(OpCodes.Add);
            ilGenerator.Emit(OpCodes.Ret);

            //创建类型
            mathType.CreateType();
            //保存程序集

            createdAssembly.Save("math.dll");
            return createdAssembly();
        }


        static object CallAssembly(Assembly assembly, string className, string methodName, object[] args)
        {
            //获取动态加载类型
            Type t = assembly.GetType(className);
            //构造实例类型
            object obj = Activator.CreateInstance(t);
            //获得要调用的方法
            MethodInfo mi = t.GetMethod(methodName);
            //调用方法
            return mi.Invoke(obj, args);

        }

        static void Main()
        {
            //动态创建
            Assembly a = CreateAssembly();
           
            //动态调用程序集中的类型的方法
            object[] args = new object[] { 2, 3 };
            int i = (int)CallAssembly(a, "IntOp", "Add", args);
            Console.WriteLine("2 + 3 = {0}", i);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

 


namespace ConsoleApplication10
{
    //委托
    public delegate void GetDataCallback(int lineCount);  //


    public  class DataForThread
    {
        private string text;
        private int number;

        private GetDataCallback callback;   //

        public DataForThread(string text, int number,GetDataCallback callback)
        {
            this.text = text;
            this.number = number;

            this.callback = callback;   //
        }

        // 线程执行的方法
        public void ThreadProc()
        {
            int lineCount = 0;           
            Console.WriteLine("\t 线程开始执行的方法 ");
            lineCount++;

            for (int i = 0; i < number ; i++)
            {              
                Console.WriteLine("\t\t<{0}>-------执行线程", i + 1,text );
                lineCount++;
            }

            Console.WriteLine("结束线程-----------");
            lineCount++;

            if (callback != null)
                callback(lineCount);

        }
     
        public class Test
        {
            public static void Main()
            {
                DataForThread dft = new DataForThread("启动",6,new GetDataCallback(ResultCallback));

                Thread t = new Thread(new ThreadStart(dft.ThreadProc));
                t.Start();
                Console.ReadKey();
            }
       
        }

        //回调方法
        public static void ResultCallback(int lineCount)
        {
            Console.WriteLine(" 线程打印了{0} 行文字", lineCount);
        }

    }

}


 public  void WriteFile(string str)
        {
             StreamWriter sr;           
             if (File.Exists(FILE_NAME)) //如果文件存在,则创建File.AppendText对象
             {
                 sr = File.AppendText(FILE_NAME);
             }
             else   //如果文件不存在,则创建File.CreateText对象
             {
                 sr = File.CreateText(FILE_NAME);
             }
             sr.WriteLine(str);
             sr.Close();
         }

posted @ 2009-07-10 12:22  Ry5  阅读(199)  评论(0编辑  收藏  举报