c#中的高级语法(.Net4)

 

 

高级语法

 

委托

委托就是指针函数,委托的定义与类的属性定义类似都必须在类的方法体进行。

 

委托的定义:

class Program

    {

        //定义委托:委托不能在方法体内定义。

        public delegate string PrintSomething(string data);

        static void Main(string[] args)

        {

 

        }

    }

 

使用委托:

        static void Main(string[] args)

        {

            //HelloWorld级别的委托

            PrintSomething PrintFruit = new PrintSomething(Do_1);

            Console.WriteLine(PrintFruit("123"));

}

 

执行结果:

you want to print the data:123


 

 

委托数组

    class Program

    {

 

        //定义委托数组

        public delegate void BlukPrintSomething();

 

        

 

        static void Main(string[] args)

        {

            //委托数组

            BlukPrintSomething[] bluckPrintSomething = new BlukPrintSomething[3]{

            BlukPrintSomething_1,BlukPrintSomething_2,BlukPrintSomething_3

            };

 

            foreach (BlukPrintSomething bps in bluckPrintSomething)

            {

                bps();

            }

 

            Console.ReadKey();

 

        }

 

        static void BlukPrintSomething_1()

        {

            Console.WriteLine("Apple");

        }

 

        static void BlukPrintSomething_2()

        {

            Console.WriteLine("Orange");

        }

 

        static void BlukPrintSomething_3()

        {

            Console.WriteLine("Banana");

        }

    

    }


 

 

 

Action<T>和Func<T>

这两个类型可以理解为便捷委托,Action<T>代表没返回值的委托。Func<T>代表有返回值的委托。

 

    

class Program

    {

        static void Main(string[] args)

        {

            //Action<T> 和Func<T> 委托

            //Action<T> 就是一个便捷类型,让我们省去了定义一个委托类型的步骤

           //Action<string,string> tmp 等效于delegate void myDelegate(string,

string) 

Do_2(Do_3);

 

            //Func<T>也是一个便捷类型,只不过该类型能具有返回值

            //Func<int,string>等效与 delegate string myDelegate(int)

            Console.WriteLine(Do_4(Do_5));         

            Console.ReadKey();

 

        }

 

        static string Do_1(string data)

        {

            return "you want to print the data:" + data;

        }

 

        static void Do_2(Action<string,string,string,string> data)

        {

            data("1", "2", "3", "4");

        }

 

        static void Do_3(string d1, string d2, string d3, string d4)

        {

            Console.WriteLine(d1 + d2 + d3 + d4);

        }

 

        static string Do_4(Func<string,string,string,string,string> data)

        {

            return data("1", "2", "3", "4");

        }

 

        static string Do_5(string d1, string d2, string d3, string d4)

        {

            return d1 + "|" + d2 + "|" + d3 + "|" + d4;

        }

        

    }//class


 

 

多播委托

    一个函数委托绑定多个实现函数,调用一个委托则可以调用绑定在该委托上的所有实现函数。

 

    

class Program

    {

        static void Main(string[] args)

        {

            BlukPrintSomething bluckPrintSomething = new BlukPrintSomething(BlukPrintSomething_1);

            bluckPrintSomething += BlukPrintSomething_2;

            bluckPrintSomething += BlukPrintSomething_3;

            bluckPrintSomething();

 

            Console.ReadKey();            

 

        }

 

        static void BlukPrintSomething_1()

        {

            Console.WriteLine("Apple");

        }

 

        static void BlukPrintSomething_2()

        {

            Console.WriteLine("Orange");

        }

 

        static void BlukPrintSomething_3()

        {

            Console.WriteLine("Banana");

        }

        

    }

 

执行结果:

Apple

Orange

Banana


 

 

匿名方法

    不长期保存的方法,也许只能使用一次。对那些只使用一次的方法,主要用来解决程序在运行时的需求。

 

    

class Program

    {

 

        public delegate void BlukPrintSomething();

 

        static void Main(string[] args)

        {

            BlukPrintSomething bluk = delegate()

            {

                Console.WriteLine("niming func!");

            };

 

            bluk();

            Console.ReadKey();

        }

       

    }

 

执行结果:

niming func!


 

 

Lambada表达式

Lambada表达式就是一个便捷的匿名函数。"=>"箭头左边是传进匿名函数的参数,而右边则是处理参数的逻辑。

 

    

class Program

    {

 

        public delegate string PrintSomething(string data);

 

        static void Main(string[] args)

        {

            PrintSomething printSomething = a => string.Format("this data:{0} if from Lambada", a);

            Console.WriteLine(printSomething("GB"));

 

            Console.ReadKey();         

 

        }

       

    }

 

执行结果:

this data:GB if from Lambada


 

 

事件

事件与委托类似,不同的是事件会将调用则及调用环境作为参数传递给绑定的处理程序。

 

    

class Program

    {

        //定义事件

//不接受参数的事件

        public static event EventHandler NotifyAnyone;

 

//接受一个参数的事件

        public static event EventHandler<GhostEventArgs> NotifyAnyone2;

        

static void Main(string[] args)

        {

            //事件

            //为事件添加订阅者,这个是匿名的订阅者

            NotifyAnyone += delegate(object sender, EventArgs e)

            {

                Console.WriteLine("NotifyAnyone event is trigger!");

            };

            NotifyAnyone(new object(),new EventArgs());

            

 

            //添加订阅或取消订阅。这里.Net已经将观察者模式很好的结合起来了。

            

            NotifyAnyone += NotifyAnyoneNoParam_1;

            NotifyAnyone += NotifyAnyoneNoParam_2;

            NotifyAnyone += NotifyAnyoneNoParam_3;

            NotifyAnyone(new object(), new EventArgs());

            Console.WriteLine();

            NotifyAnyone -= NotifyAnyoneNoParam_3;

            NotifyAnyone(new object(), new EventArgs());

            

 

            //带参数的事件订阅与取消

            

            GhostEventArgs e1 = new GhostEventArgs();

            e1.FireTime = DateTime.Now;

 

            NotifyAnyone2 += NotifyAnyoneHaveParam_1;

            NotifyAnyone2 += NotifyAnyoneHaveParam_2;

            NotifyAnyone2 += NotifyAnyoneHaveParam_3;

            NotifyAnyone2(new object(), e1);

            Console.WriteLine();

            NotifyAnyone2 -= NotifyAnyoneHaveParam_3;

            NotifyAnyone2(new object(), e1);

 

            Console.ReadKey();

        }

 

        static void NotifyAnyoneNoParam_1(object sender, EventArgs e)

        {

            Console.WriteLine(" Event NotifyAnyoneNoParam_1 Was Trigger!");

        }

 

        static void NotifyAnyoneNoParam_2(object sender, EventArgs e)

        {

            Console.WriteLine(" Event NotifyAnyoneNoParam_2 Was Trigger!");

        }

 

        static void NotifyAnyoneNoParam_3(object sender, EventArgs e)

        {

            Console.WriteLine(" Event NotifyAnyoneNoParam_3 Was Trigger!");

        }

 

        static void NotifyAnyoneHaveParam_1(object sender, GhostEventArgs e)

        {

            Console.WriteLine(string.Format("this event is trigger on {0}", e.FireTime));

        }

 

        static void NotifyAnyoneHaveParam_2(object sender, GhostEventArgs e)

        {

            Console.WriteLine(string.Format("this event is trigger on {0}", e.FireTime));

        }

 

        static void NotifyAnyoneHaveParam_3(object sender, GhostEventArgs e)

        {

            Console.WriteLine(string.Format("this event is trigger on {0}", e.FireTime.ToString()));

        }

       

    }

 

GhostEventArgs:

    class GhostEventArgs:EventArgs

    {

        public DateTime FireTime { get; set; }

    }

 

执行结果:

NotifyAnyone event is trigger!

NotifyAnyone event is trigger!

 Event NotifyAnyoneNoParam_1 Was Trigger!

 Event NotifyAnyoneNoParam_2 Was Trigger!

 Event NotifyAnyoneNoParam_3 Was Trigger!

 

NotifyAnyone event is trigger!

 Event NotifyAnyoneNoParam_1 Was Trigger!

 Event NotifyAnyoneNoParam_2 Was Trigger!

this event is trigger on 2012/2/23 15:50:22

this event is trigger on 2012/2/23 15:50:22

this event is trigger on 2012/2/23 15:50:22

 

this event is trigger on 2012/2/23 15:50:22

this event is trigger on 2012/2/23 15:50:22


 

 

动态语言(DynamicObject)

    Var类型与dynamic类型的区别:Var类型在为其赋值前类型不确定,但赋值后类型则定下来了不能进行修改。而Dynamic类型却没有这样的限制,可以在任何时候存放任何类型的东西。

 

    

var data = "this is a string";

    Console.WriteLine(data);

    //data = 1;//这句无法通过

    Console.WriteLine(data);

  

    dynamic data2 = "this is a string";

    Console.WriteLine(data2);

    data2 = 1;

    Console.WriteLine(data2);

    data2 = new Object();

    Console.WriteLine(data2);

 

执行结果:

this is a string

this is a string

 

this is a string

1

System.Object


 

 

ExpandoObject(扩展对象)

    这个类型的对象与javascript中的变量类似,可以动态的扩展。

 

   

 dynamic data = new ExpandoObject();

 

    data.FirstAttribute = "FirstAttribute";

    data.SecondAttribute = "SecondAttribute";

    data.ThirdAttribute = "ThirdAttribute";

 

    ArrayList nums = new ArrayList();

    for (int i = 1; i <= 10; i++)

    {

        nums.Add(i);

    }

 

    data.Nums = nums;

    foreach (int tmp in data.Nums)

    {

        Console.WriteLine(tmp);

    }

 

    Console.WriteLine(data.FirstAttribute);

    Console.WriteLine(data.SecondAttribute);

    Console.WriteLine(data.ThirdAttribute);

 

    Action GB = delegate() { GhostBear2(); };

    data.MyMethod2 = GB;

    data.MyMethod2();

 

    Action<string> GhostBear = a => Console.WriteLine(a);

    data.MyMethod = GhostBear;

    data.MyMethod("Go!Go!Fighting!");

           

 

    //被调用的方法体

    public static void GhostBear2()

    {

        Console.WriteLine("This is dynamic method.");

    }

 

 

 

执行结果:

1

2

3

4

5

6

7

8

9

10

FirstAttribute

SecondAttribute

ThirdAttribute

This is dynamic method.

Go!Go!Fighting!


 

 

DynamicObject(实现自己的可扩展类型)

    自己也可以实现扩展类型,但没有系统自带的扩展类型(ExpandoObject)好用。

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

using System.Dynamic;

 

namespace chapter12

{

    class MyDynamicObject:DynamicObject

    {

        Dictionary<string, object> data = new Dictionary<string, object>();

 

        /// <summary>

        /// 

        /// </summary>

        /// <param name="binder">传进来的参数,包含了调用信息</param>

        /// <param name="result">返回包含的对象</param>

        /// <returns>返回是否成功</returns>

        public override bool TryGetMember(GetMemberBinder binder, out object result)

        {

            bool isContain = false;

            if (data.ContainsKey(binder.Name))

            {

                result = data[binder.Name];

                isContain = true;

            }

            else

            {

                result = "Not Find";

            }

            return isContain;

        }

 

        /// <summary>

        /// 

        /// </summary>

        /// <param name="binder">传进来的参数,包含了调用信息</param>

        /// <param name="value">传进来的参数,包含了调用信息</param>

        /// <returns></returns>

        public override bool TrySetMember(SetMemberBinder binder, object value)

        {

            bool result = false;

            if (!data.ContainsKey(binder.Name))

            {

                data[binder.Name] = value;

                result = true;

 

            }

            return result;

        }

 

        /// <summary>

        /// 

        /// </summary>

        /// <param name="binder">传进来的参数,包含了调用信息</param>

        /// <param name="args">传进来的参数,包含了调用信息</param>

        /// <param name="result">方法体执行后返回的结果</param>

        /// <returns>调用是否成功</returns>

        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)

        {

            dynamic tmp = data[binder.Name];

            result = null;

 

            tmp((string)args[0]);

           

            return true;

        }

    }

}

 

调用代码:

    dynamic myDynamicObject = new MyDynamicObject();

            

    myDynamicObject.FirstAttribute = "FirstAttribute";

            

    Action<string> myMethod = a => Console.WriteLine(a);

    myDynamicObject.myMethod = myMethod;

 

    Console.WriteLine(myDynamicObject.FirstAttribute);

    myDynamicObject.myMethod("this is Dynamic Object");

 

执行结果:

FirstAttribute

this is Dynamic Object


 

 

反射

   反射在日常开发中用的很少,也很难用。我至今也没找到在什么地方使用这项技术会有意想不到的效果。下面贴个小例子,简单了描述下反射。

 

特性类:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

using System.IO;

using System.Reflection;

 

namespace chapter14

{

    [AttributeUsage(AttributeTargets.All,AllowMultiple=true,Inherited=true)]

    public class MemoAttribute:Attribute

    {

        public MemoAttribute() { }

        public string MemoTime { get; set; }

        public string MemoContent { get; set; }

        public void WriteSomething(string path) 

        {

            File.WriteAllText(path, MemoContent + MemoTime.ToString());

        } 

    }

}


 

调用特性类的类型

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

[assembly: chapter14.Memo(MemoContent = "创建", MemoTime = "2011.11.1")]

 

namespace chapter14

{

 

   

    [Memo(MemoContent="创建",MemoTime="2011.11.1")]

    class Robot

    {

 

        public string Name { get; set; }

        [Memo(MemoContent="修改",MemoTime="2011.11.2")]

        public string Detail { get; set; }

 

        [Memo(MemoContent="修改",MemoTime="2011.11.4")]

        [Memo(MemoContent="修改",MemoTime="2011.11.3")]

        [Memo(MemoContent="创建",MemoTime="2011.11.1")]

        public void CreateBomb()

        {

            Console.WriteLine("CreateBomb");

        }

 

        public void LayBomb()

        {

            Console.WriteLine("LayBomb");

        }

 

        public void Kill()

        {

            Console.WriteLine("Kill");

        }

 

        public void Save()

        {

            Console.WriteLine("Save");

        }

 

        public void Escape()

        {

            Console.WriteLine("Escape");

        }

 

        public void Kill(string name)

        {

            Console.WriteLine(name + " was dead.");

        }

 

        public string GetInfomation()

        {

            return DateTime.Now.ToString();

        }

    }

}


 

执行代码(获取定义在robot类上的特性标记“[Memo(MemoContent="创建",MemoTime="2011.11.1")]”)

   

 //简单的获取在类上定义的“特性类”MemoAttribute         

    Type robot = typeof(Robot);

    object[] attributes = robot.GetCustomAttributes(true);

 

    foreach (object tmp in attributes)

    {

        MemoAttribute memo = tmp as MemoAttribute;

        if (memo == null)

        {

            continue;

        }

        Console.WriteLine(string.Format("Content:{0},DateTime:{1}", memo.MemoContent, memo.MemoTime));

    }

 

执行结果:

Content:创建,DateTime:2011.11.1


 

 

调用代码(打印出标记在robot类型内的所有MemoAttribute特性类的值:方法,属性等)

    

Type robot = typeof(Robot);

    MemberInfo[] members = robot.GetMembers();

    object[] tmp;

    MemoAttribute memo;

 

 

    foreach (MemberInfo member in members)

    {

        int indent = 0;//添加缩进

        Console.WriteLine(string.Format("Name:{0},Type:{1}", member.Name, member.MemberType));

        indent++;

        tmp = member.GetCustomAttributes(typeof(MemoAttribute), true);

        foreach (object o in tmp)

        {

            memo = o as MemoAttribute;

            if (o == null)

            {

                continue;

            }

            Console.WriteLine(string.Format("{0}content:{1},datetime:{2}", "".PadLeft(indent * 5, ' '), memo.MemoContent, memo.MemoTime));

 

        }

 

    }

 

执行结果:

Name:get_Name,Type:Method

Name:set_Name,Type:Method

Name:get_Detail,Type:Method

Name:set_Detail,Type:Method

Name:CreateBomb,Type:Method

     content:创建,datetime:2011.11.1

     content:修改,datetime:2011.11.4

     content:修改,datetime:2011.11.3

Name:LayBomb,Type:Method

Name:Kill,Type:Method

Name:Save,Type:Method

Name:Escape,Type:Method

Name:Kill,Type:Method

Name:GetInfomation,Type:Method

Name:ToString,Type:Method

Name:Equals,Type:Method

Name:GetHashCode,Type:Method

Name:GetType,Type:Method

Name:.ctor,Type:Constructor

Name:Name,Type:Property

Name:Detail,Type:Property

     content:修改,datetime:2011.11.2


 

 

通过反射在运行时调用方法

    也就是在程序运行后更具某些情况来决定调用定义在类型上的某个方法,真正的解耦。

 

    

Type robot = typeof(Robot);

    MemberInfo[] members = robot.GetMembers();

    object[] tmp;

    MemoAttribute memo;

 

    //动态调用方法   插件编程

    foreach (MemberInfo member in members)

    {

   if (member.MemberType == MemberTypes.Method && member.Name.ToLower()

                           =="createbomb")

     {

        //构造函数

        object tmpRobot = robot.InvokeMember(null, BindingFlags.DeclaredOnly |

                                                         BindingFlags.Public |

                                                      BindingFlags.NonPublic |

                                                       BindingFlags.Instance |

                                                  BindingFlags.CreateInstance

, null, null, null);

 

       robot.InvokeMember("CreateBomb", BindingFlags.InvokeMethod

, null, tmpRobot, null);

       robot.InvokeMember("Kill", BindingFlags.InvokeMethod

, null, tmpRobot, new object[] { "秦萱" });

       Console.WriteLine((string)robot.InvokeMember("GetInfomation",

                              BindingFlags.InvokeMethod, null, tmpRobot, null));

        }

 

    }

 

执行结果:

CreateBomb

秦萱 was dead.

2012/2/23 20:03:55


 

 

应用在程序集上的反射

    这个例子获取的是定义在程序集“robot”上的MemoAttribute特性。

 

    

//加载程序集

    Assembly chapter14 = Assembly.GetAssembly(typeof(Robot));

    object[] attributes = chapter14.GetCustomAttributes(typeof(MemoAttribute), true);

    MemoAttribute memo;

 

    foreach (object attribute in attributes)

    {

        memo = attribute as MemoAttribute;

        if (memo == null)

        {

            continue;

        }

        Console.WriteLine(string.Format("Content:{0},DateTime:{1}", memo.MemoContent, memo.MemoTime));

    }

 

    Console.WriteLine();

 

    Console.ReadKey();

 

执行结果:

Content:创建,DateTime:2011.11.1


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(.net,String,object,C#,attributes)