Monday, 15 August 2011

c# - How to restrict passed parameter in method -


My question is in sample code How can I restrict developers to passing the true parameters? I try something about generic but I can not fix it.

The important thing here is that I want to limit the compilation time. So I know how I can be stopped in runtime.

  Namespace the Levaging {Public Interface IFood {int protein {get; Set; } Intake carbohydrate {get; Set; }} Public Interface IMEAT: iFood {Int Nitrogen {Received; Set; }} Public Interface IVegetable: IFood {int Vitamin {Receive; Set; }} Public Square Wheel: IMET {Public Entity Protein {Received; Set; } Public int carbohydrates {receive; Set; } Public at Nitrogen {Receive; Set; }} Public class spine: vivigable {public int protein {receipt; Set; } Public int carbohydrates {receive; Set; } Get public int vitamins; Set; }} Public Interface Erating {Nilless (iFood Dishes); } Public square lion: elating {public int protein {receipt; Set; } Public int carbohydrates {receive; Set; } Public at Nitrogen {Receive; Set; } // But the lion is eating meat only, so any developer can bring the subject to eat for the lion. // Maybe God is not a good developer so I want to ban him at compile time !! Only for passing the meat :) / / The important thing here is that I do not want runtime on compilation time !!! Public Zero Food (IF food) {Protein = Food. Protein; Carbohydrate = food Carbohydrates; // nitrogen = ?? // So I know that I can cook and validate, but I want to make sure at DesignTime !! }} Public Square Sheep: Illyting {public int protein {receipt; Set; } Public int carbohydrates {receive; Set; } Get public int vitamins; Set; } Public Zero Eat (IPHOOD METHOD) {Protein = Food. Protein; Carbohydrate = food Carbohydrates; // vitamins = meals ?? }}}}  

I think you should have an interface for iHerbivore , ICarnivore , and IOmnivore to allow it at design time.

Public Interface IHerbivore {void Eat (IVegetable food); } Public Interfaces Ikarnivore {Eat less (IMIT food); } Public interface IOmnivore: IHerbivore, ICarnivore {}

Then your tiger can be a ICarnivore and will only be able to eat meat


No comments:

Post a Comment