public void ProcessCharacters(FightManager.CharacterOrderDelegate proc) { Console.WriteLine("Calling: {0}", proc.Method); string targetMsg = proc.Target == null ? "--> Target is a static method." : "--> Target: " + proc.Target; Console.WriteLine(targetMsg); foreach (RPGCharacter rpgCharacter in party) { Console.WriteLine("Processing RPG Character"); proc(rpgCharacter); } }
//Call my item processor in the AdventureParty object, passing a delegate //There are two way to pass the delegate... explicitly by creating the delegate, //passing a method with the proper signature to this delegate, and then sending the delegate //to the item processor, or more simply, sending the method with the proper signature to //the item processor with will behind the scenes do this work for you. //When the process is invoked in the item processor, the methods will be executed with the //appropriate parameters/arguments. private void RunRPGAdventure() { AdventureParty party = new AdventureParty(); FightManager fightManager = new FightManager(); FightManager.SpawnPartyMember characterFactory = fightManager.GetNewPartyMember; FightManager.SpawnPartyMember dwarfFactory = fightManager.CreateDwarf; party.AddMember( dwarfFactory( "Riah" ) ); party.AddMember( characterFactory( "Billrhiem" ) ); party.AddMember( characterFactory( "Fark" ) ); party.AddMember( characterFactory( "Storak" ) ); party.AddMember( characterFactory( "Werkiel" ) ); party.ProcessCharacters(fightManager.SendToBattle); party.GetCharacter( "Riah" ).IsDead = true; party.GetCharacter( "Fark" ).IsDead = true; party.ProcessCharacters(fightManager.CheckHealth); }