Cast a property to its actual type dynamically using reflection (where actual type is generic)

我是研究僧i 提交于 2021-02-05 09:17:06

问题


This is a slightly different question asked here. I modified the same code into my needs like below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace cns01
{
    class Program
    {
        public class ClassA<T>
        {
            public int IntProperty { get; set; } = 999;
        }

        public class ClassB<T2>
        {
            public ClassA<int> MyIntProperty { get; set; }
            public ClassA<string> MyStringProperty { get; set; }
        }

        static void Main(string[] args)
        {
            ClassB<int> tester = new ClassB<int>();
            tester.MyIntProperty = new ClassA<int>() { IntProperty = 777 };

            PropertyInfo propInfo = typeof(ClassB<>).GetProperty("MyIntProperty");

            dynamic property = propInfo.GetValue(tester, null); // <-- Here I get error : Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true

            int result = property.IntProperty; //(*1)
        }
    }
}

anyone got an idea how to set (and/or get) value into result(*1)?

I appreciate any help, Thx beforehand...

My actual position is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace cns01
{
    class Program
    {
        public class DataFieldInfo2<T>
        {
            public bool IsSearchValue { get; set; } = false;
            public T Value { get; set; }
        }

        public class SmartRowVertrag
        {
            public DataFieldInfo2<int> ID { get; set; }
            public DataFieldInfo2<string> VSNR { get; set; }
        }

        static void Main(string[] args)
        {
            SmartRowVertrag tester = new SmartRowVertrag();
            tester.ID = new DataFieldInfo2<int>() { Value = 777, IsSearchValue = false };
            tester.VSNR = new DataFieldInfo2<string>() { Value = "234234234", IsSearchValue = true };

            var g = RetData3(tester);
        }

        public static object RetData3(object fsr)
        {
            object retVal = new object();
            var props = fsr.GetType().GetProperties();
            foreach (var prop in props)
            {
                PropertyInfo propInfo = prop.GetType().GetProperty("IsSearchValue"); // <-- here I get null
                propInfo = prop.PropertyType.GetProperty("IsSearchValue"); // here I get a propertyInfo, but...
                dynamic property = propInfo.GetValue(fsr, null); // ...<-- here fires error
                var result = property.IsSearchValue;
                if (result == true)
                {
                    // doThis
                }
            }
            return retVal;
        }
    }
}

how can I accomplish this without any error? "prop" seems to be a PropertyInfo which I cant use as a generic DataFieldInfo2.


回答1:


Change your code to receive property to:

PropertyInfo propInfo = tester.GetType().GetProperty("MyIntProperty");

So you use constructed generic type.



来源:https://stackoverflow.com/questions/64485988/cast-a-property-to-its-actual-type-dynamically-using-reflection-where-actual-ty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!