如何限制方法中传递的参数

时间:2015-03-17 11:49:10

标签: c# design-patterns methods parameter-passing restrict

我的问题是示例代码。 如何限制开发人员传递真实参数。我尝试了一些关于泛型的东西,但我无法修复。

这里重要的是我想在编译时限制。所以我知道如何在运行时阻止。

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.??
        }
    }
}

4 个答案:

答案 0 :(得分:5)

我认为您必须拥有IHerbivoreICarnivoreIOmnivore的接口才能在设计时使用此接口。

public interface IHerbivore
{
    void Eat(IVegetable food);
}

public interface ICarnivore
{
    void Eat(IMeat food);
}

public interface IOmnivore : IHerbivore, ICarnivore
{
}

那么你的狮子可以是ICarnivore,只能吃肉

答案 1 :(得分:0)

替代方案,您可以更改界面...

public interface IEating
{
    bool Eat(IFood food); //return wheater the eater eats the food or not
}

像狮子一样实施狮子:

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;
    }
}

答案 2 :(得分:0)

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);
    }
}

答案 3 :(得分:0)

MS Code Contracts怎么样?我发誓它的Visual Studio集成在编译时添加至少警告。