[英]How to restrict passed parameter in method
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. 我认为您必须具有
IHerbivore
, ICarnivore
和IOmnivore
才能在设计时允许它。
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 这样,您的狮子可以成为
ICarnivore
,只能吃肉
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 ? MS代码合同呢? I would swear its Visual Studio integration add at least warnings at compile time.
我发誓它的Visual Studio集成至少在编译时添加警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.