示例#1
0
    //两者传的参数一定要一样,才可以用delegate 来调用另外的methods;
    //reference them, assign the veriable
    //allow us to methods another ,can referrence anther methods ,passerd anther methods to ,
    //将一个方法传到另一个方法,将方法作为参数或者变量在另一个方法中
    public void OnEnable()
    {
        //MeDelegate meDelegate = new MeDelegate(Foo);
        //meDelegate.Invoke();
        //meDelegate();

        //MeDelegate meDelegate1 = Goo;
        //// if we write this ,the compiler will replace this with an invoke call
        ////we not invoke foo here ,we're just passing it..

        ////this is shorthans sugar
        //meDelegate1();

        //// when this is compiles ,we will get a new medelegate.,and it's invoke method will be calles
        ////with delegates ,we are able to treat methods like first class objects
        ///
        MedelegateTakeIntReturnBool medel = FooTakeInt;

        ////the same reason we parameterize this ,is why we parameterize code ,or reference to code (_methods /functions)
        //var resultlessthanfive = GetallthenumbersLessThanFive(new List<int>() { 1, 2, 10, 58, 56, 204, });
        //foreach(var number in resultlessthanfive)
        //{

        //}
        ////list 后方可以在定义一个变量,来数出其中的数字
        //var resultlessthanTen = GetallthenumbersLessThanTen(new List<int>() { 20, 25, 10, 58, 56, 204, });
        //foreach (var number in resultlessthanTen)
        //{

        //}

        var numbers = new List <int> {
            2, 30, 45, 62, 10, 12
        };
        var numbersLessThanFive = RunNumbersThroughTheGauntlet(numbers, lessThanFive);



        MedelegatelessthanFive minigauntlet = lessThanFive;
        //this is great but we  still have to deal with making these methods

        //Lambads =>
        // var resultlessThanFive = RunNumbersThroughTheGauntlet(numbers, n => n < 5);

        var resultlessThanFive = numbers.Where(n => n < 5);
        //only have what we need
        //take the lambads  if i create a lamba when i compile the programm it can automaticaly creat a methods for you;
        //add delegating and moving delegating
        //WE know we can assign delegate and create new objects/targets like this
        //MeDelegate meDelegate = MOO;
        //meDelegate = (MeDelegate)Delegate.Combine(meDelegate, (MeDelegate)BOO);
        //meDelegate = meDelegate + BOO;
        //meDelegate.Invoke();
        //meDelegate -= BOO;

        //their are no  targets for moo and sue,since they are static methods ,and are callled by themself;
        //foreach(var DEL in meDelegate.GetInvocationList())
        //{
        //    Debug.Log("");

        //}
        //what will get return;
        //only get the last value
        MeDelegateReturnInt meDelRetInt = ReturnFive;

        meDelRetInt += ReturnTen;
        var value = meDelRetInt();

        Debug.Log(value);

        foreach (var del in meDelRetInt.GetInvocationList())
        {
            Debug.Log(del.DynamicInvoke());
        }

        //we can also generify our delegate
        MeDelegateGeneric <int> meDelegate = ReturnFive;

        meDelegate += ReturnTen;

        //usually ,just like with generics ,we don't have to creat our own delegates, because we have actions and func
        //func have a return
        //Func<int> meFunc = ReturnFive;
        //meFunc += ReturnTen;

        //foreach(var f in meFunc.GetInvocationList())
        //{
        //    Debug.Log(f.DynamicInvoke());
        //}

        //actions have no return (return void)
        Action <int> meAct = TakeAnIntReturnVoid;

        //Action returnNothing = nothing;
        meAct(15);
        //big diffient between is act return nothing;

        //the difference between delegates and events...
        //an event is a delegates,with two restrictions, you can't assign them directly ,and you can't invoke them directly

        //myaction = ReturnTen;
    }
示例#2
0
    public void OnEnable()
    {
        #region Delegates
        ////We're not invoking Foo here, we're just passing it...
        ////MeDelegate meDelegate = Foo;
        //MeDelegate meDelegate = new MeDelegate(Foo);
        //Dele1 dele1 = new Dele1(FooReturnInt);
        ////This is a reference to a class, but it's also treated like a METHOD
        //meDelegate.Invoke();

        //MeDelegate meDelegate2 = Goo;
        //Dele2 dele2 = FooTakeInt;
        ////If we write this, the compiler will replace this with an INVOKE call
        ////This is shorthand/syntactic sugar
        //meDelegate2();
        //dele1();
        //dele2(1);

        ////When this is compiled, we'll get a new MeDelegate, and it's invoke method will be called
        ////With delegates, we are able to treat methods like first class objects,表示可以把方法当成一个类来使用,所以可以将参数设为一个方法

        //InvokeTheDelegate(Foo);

        ////Delegates are references to objects AND methods
        //Debug.Log($"Target:{meDelegate.Target},Method:{meDelegate.Method}");
        ////因为Goo是static方法,所以它没有target,而Foo是静态方法,所以它
        //Debug.Log($"Target:{meDelegate2.Target},Method:{meDelegate2.Method}");

        //MeDelegateTakeIntReturnBool medel = FooTakeIntReturnBool;
        //medel(5);


        ////The same reason we parameterize this, is why we parameterize code, or references to code (methods/functions)
        //var result = GetAllTheNumberLessThanFive(new List<int>() {1, 25, 4, 765, 4});

        //foreach (var number in result)
        //{
        //    Debug.Log(number);
        //}

        //var numbers = new List<int>() {1, 25, 3, 6, 2, 7, 19, 3, 2};

        //var numbersLessThanFive = RunNumbersThroughTheGauntLet(numbers, LessThanFive);
        //var numbersLessThanTen = RunNumbersThroughTheGauntLet(numbers, LessThanTen);
        //var numbersLessThanTwenty = RunNumbersThroughTheGauntLet(numbers, GreaterThanTwenty);
        ////use lambda "=>" to simplize our delegate.(syntactic sugar)
        ////Because delegate has already had the struct.
        ////"=>" means ((parameter) => (return))
        //var numbersLessThanTwo = RunNumbersThroughTheGauntLet(numbers, i => i < 2);

        //Delegate Chaining
        //Adding and removing delegates...
        MeDelegate meDelegate = Moo;
        meDelegate  = (MeDelegate)Delegate.Combine(meDelegate, (MeDelegate)Boo);
        meDelegate  = meDelegate + Sue;
        meDelegate += Moo;
        meDelegate -= Moo;//The Result will be"Moo,Boo,Sue".The last one "Moo" will be removed;
        meDelegate.Invoke();

        foreach (var del in meDelegate.GetInvocationList())
        {
            Debug.Log($"Target:{del.Target}, Method:{del.Method}");
        }

        //What will get returned?
        MeDelegateReturnInt meDelRetInt = ReturnFive;
        meDelRetInt += ReturnTen;
        var value = meDelRetInt();
        Debug.Log(value);//The Result is 10.I will get the last value.

        foreach (var del in meDelRetInt.GetInvocationList())
        {
            Debug.Log(del.DynamicInvoke());
        }//The Result is 5 and then 10. we will get 2 value;


        //Generic Delegate
        MeDelegateGeneric <int> delegateGeneric = () => 5;
        delegateGeneric += () => 10;

        MeDelegateGeneric <string> meDelegateString = () => "Jeff";
        meDelegateString += (() => "Chris");

        //Usually, just like with generics, we don't have to create our own delegates
        //Because Actions and Funcs
        //Funcs have a return
        Func <int> meFunc = () => 5;//Func|Return~~~Fountain
        meFunc += () => 10;

        foreach (var f in meFunc.GetInvocationList())
        {
            Debug.Log(f.DynamicInvoke());
        }

        Func <int, int, string> meFunc2 = (i, i1) => ("me");
        //The last one is return

        //Action return void ,just parameter
        //Because 'Action' means do something. So Action have no return, just doing things in the method
        Action <int> meAct = TakeAnIntReturnVoid;
        #endregion


        #region Events
        //The difference between delegates, and events...


        //!!!An event is a delegate, with TWO restrictions: you can't assign them directly, and you can't invoke them directly

        TrainSignal.TrainsCOMING += ATrainsComing;
        #endregion
    }