Приведение свойства к его фактическому типу динамически с использованием отражения

У меня было время, поэтому entity-framework я попытался решить свою проблему c# с помощью VS2010, и я думаю, что visual-c# был прав раньше, когда думал, что c#.net динамическая ключевая работа c#.net «решит» мой вопрос. См. Код csharp ниже.

using System.Reflection;

namespace TempTest
{
    public class ClassA
    {
        public int IntProperty { get; set; }
    }

    public class ClassB
    {
        public ClassB()
        {
            MyProperty = new ClassA { IntProperty = 4 };
        }
        public ClassA MyProperty { get; set; }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            ClassB tester = new ClassB();

            PropertyInfo propInfo = typeof(ClassB).GetProperty("MyProperty");
            //get a type unsafe reference to ClassB`s property
            dynamic property = propInfo.GetValue(tester, null);

            //casted the property to its actual type dynamically
            int result = property.IntProperty; 
        }
    }
}

c#

entity-framework

reflection

2022-10-04T19:19:57+00:00