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 Compile Time. 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 Compile Time!! for passing only Meat. :)
//The important thing here is i want restrict on Compile Time 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.??
}
}
}
I think you'll have to have interfaces for IHerbivore
, ICarnivore
, and IOmnivore
to allow this at design time.
public interface IHerbivore
{
void Eat(IVegetable food);
}
public interface ICarnivore
{
void Eat(IMeat food);
}
public interface IOmnivore : IHerbivore, ICarnivore
{
}
Then your lion can be an ICarnivore
and will only be able to eat meat
Alternative you can change the interface...
public interface IEating
{
bool Eat(IFood food); //return wheater the eater eats the food or not
}
And implement the lion like:
public class Lion : IEating
{
public int Protein { get; set; }
public int Carbohydrate { get; set; }
public int Nitrogen { get; set; }
public bool Eat(IFood food)
{
IMeat meat = food as IMeat;
if (meat != null)
{
Protein = meat.Protein;
Carbohydrate = meat.Carbohydrate;
Nitrogen = meat.Nitrogen;
return true;
}
return false;
}
}
public interface IEating<in T> where T : IFood {
void Eat(T food);
}
public class Lion : IEating<IMeat>, IEating<IFood> {
public int Protein { get; set; }
public int Carbohydrate { get; set; }
public int Nitrogen { get; set; }
public void Eat(IMeat food) {
Protein = food.Protein;
Carbohydrate = food.Carbohydrate;
Nitrogen = food.Nitrogen;
}
public void Eat(IFood food) {
var meat = food as IMeat;
if (meat == null) return;
Eat(meat);
}
}
public class Sheep : IEating<IVegetable>, IEating<IFood> {
public int Protein { get; set; }
public int Carbohydrate { get; set; }
public int Vitamin { get; set; }
public void Eat(IVegetable food) {
Protein = food.Protein;
Carbohydrate = food.Carbohydrate;
Vitamin = food.Vitamin;
}
public void Eat(IFood food) {
var vegetable = food as IVegetable;
if (vegetable == null) return;
Eat(vegetable);
}
}
What about MS Code Contracts ? I would swear its Visual Studio integration add at least warnings at compile time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.