My question is in sample code. How to i restrict developers for passing true parameter. I try something about generics but i couldn't fix.
The important thing here is i want to restrict in DesignTime. So i know how can i prevent in Runtime.
namespace TheLiving
{
public interface IFood
{
int Protein { get; set; }
int Carbohydrate { get; set; }
}
public interface IMeat : IFood
{
int Nitrogen { get; set; }
}
public interface IVegetable : IFood
{
int Vitamin { get; set; }
}
public class Veal : IMeat
{
public int Protein { get; set; }
public int Carbohydrate { get; set; }
public int Nitrogen { get; set; }
}
public class Spinach : IVegetable
{
public int Protein { get; set; }
public int Carbohydrate { get; set; }
public int Vitamin { get; set; }
}
public interface IEating
{
void Eat(IFood food);
}
public class Lion : IEating
{
public int Protein { get; set; }
public int Carbohydrate { get; set; }
public int Nitrogen { get; set; }
//But lion is eating only Meat. So any developer can pass vegatable to lion for eating.
//May be god is not a good developer. So i want restrict him on DesignTime!! for passing only Meat. :)
//The important thing here is i want restrict on DesignTime not RunTime!!!
public void Eat(IFood food)
{
Protein = food.Protein;
Carbohydrate = food.Carbohydrate;
//Nitrogen = ?? //So i know that i can cast and validate food but i want ensure this on DesignTime!!
}
}
public class Sheep : IEating
{
public int Protein { get; set; }
public int Carbohydrate { get; set; }
public int Vitamin { get; set; }
public void Eat(IFood food)
{
Protein = food.Protein;
Carbohydrate = food.Carbohydrate;
//Vitamin = food.??
}
}
}
Aucun commentaire:
Enregistrer un commentaire