.NET 4.x Как получить свойство класса? - C#

Узнай цену своей работы

Формулировка задачи:

В продолжении темы визитора Есть, к примеру, вот такой код:
public interface IAnimals 
{
   string Name { get; set; } 
}
 
public class Dog : IAnimals
{
    public string Name { get; set; }
    public bool Ears { get; set; } = true;
}
 
public class Cat : IAnimals
{
    public string Name { get; set; }
    public bool Tail { get; set; } = false;
}
public class Squirel: IAnimals
{
    public string Name { get; set; }
}
var secondAnimal = new List<IAnimals>(){new Cat(), new Dog(), new Squirel()};
 
bool haveTail;
foreach (IAnimals currentAnimal in secondAnimal)
{
    if (currentAnimal is Cat)
    {
      haveTail = (currentAnimal as Cat).Tail;
      break;
    }
    if (currentAnimal is Dog)
    {
      (currentAnimal as Dog).Ears = true;
    }
}
Как нужно реализовать код, чтобы не использовать приведение типов при получении и установке значения свойства?

Решение задачи: «.NET 4.x Как получить свойство класса?»

textual
Листинг программы
public class Visitor
    {
        public dynamic GetPropValue { get; set; }
        public void Apply(Dog animal)
        {
        }
 
        public void Apply(Dog animal, string prpName)
        {
        }
 
        public void Apply(Cat animal)
        {
           
        }
 
        public void Apply(Cat animal, string prpName)
        {
            PropertyInfo propInfo = typeof(Cat).GetProperty(prpName);
            GetPropValue = propInfo.GetValue(animal, null);
        }
 
        public void Apply(Squirel animal)
        {
        }
 
        public void Apply(Squirel animal, string prpName)
        {
        }
    }
 
    public abstract class Animal
    {
        public string Name { get; set; }
        public abstract void Apply(Visitor visitor);
        public abstract void Apply(Visitor visitor, string prpName);
 
    }
 
    public class Dog : Animal
    {
        public Dog(string _name)
        {
            Name = _name;
        }
 
        public bool Ears { get; set; } = true;
 
        public override void Apply(Visitor visitor)
        {
            visitor.Apply(this);
        }
 
        public override void Apply(Visitor visitor, string prpName)
        {
            visitor.Apply(this, prpName);
        }
 
    }
 
    public class Cat : Animal
    {
        public Cat(string _name)
        {
            Name = _name;
        }
 
        public bool Tail { get; set; } = false;
 
        public override void Apply(Visitor visitor)
        {
            visitor.Apply(this);
        }
 
        public override void Apply(Visitor visitor, string prpName)
        {
            visitor.Apply(this, prpName);
        }
 
   }
 
    public class Squirel : Animal
    {
        public Squirel(string _name)
        {
            Name = _name;
        }
        public override void Apply(Visitor visitor)
        {
            visitor.Apply(this);
        }
 
 
        public override void Apply(Visitor visitor, string prpName)
        {
            visitor.Apply(this, prpName);
        }
 
    }
 
class Program
    {
        static void Main(string[] args)
        {
            string catName = "Барсик";
            var animals = new List<Animal>() {new Cat(catName), new Cat("Кузя"), new Dog(""), new Squirel("")};
 
            var visitor = new Visitor();
 
            foreach (Animal animal in animals)
            {
                if (animal.Name == catName)
                {
 
                    animal.Apply(visitor, nameof(Cat.Tail));
                    var ee = visitor.GetPropValue;
                }
                Console.ReadLine();
            }
        }
    }

Оцени полезность:

8   голосов , оценка 3.75 из 5
Похожие ответы