简体   繁体   中英

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

Thanks for all those who helped me in my prior 2 questions here and here .

Here is the representing code fragment:

using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace cns01
{
    class Program
    {
        public interface IDataFieldInfo
        {
            bool IsSearchValue { get; set; }
        }
        public class DataFieldInfo2<T> : IDataFieldInfo
        {
            public T theValue { get; set; }
            public bool IsSearchValue { get; set; } = false;
        }

        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>() { theValue = 777, IsSearchValue = false };
            tester.VSNR = new DataFieldInfo2<string>() { theValue = "234234234", IsSearchValue = true };

            var g = RetData3(tester);
        }

        public static object RetData3(object fsr)
        {
            List<OracleParameter> oParColl = new List<OracleParameter>();
            var fieldProperties = fsr.GetType().GetProperties()
                .Where(prop => typeof(IDataFieldInfo).IsAssignableFrom(prop.PropertyType));
            foreach (var fieldProperty in fieldProperties)
            {
                var field = (IDataFieldInfo)fieldProperty.GetValue(fsr);
                if (field.IsSearchValue)
                {
                    OracleParameter tmpPar = new OracleParameter()
                    {
                        ParameterName = fieldProperty.Name,
                        Value = fieldProperty.theValue // <-- Designer error: CS1061    'PropertyInfo' does not contain a definition for 'theValue' and no accessible extension method 'theValue' accepting a first argument of type 'PropertyInfo' could be found (are you missing a using directive or an assembly reference?)
                    };
                }
            }
            return null;
        }
    }
}

Is there any way to get theValue from fieldProperty independently from type? Since OracleParameter 's Value Property is object, I do not expect any difficulties to set it with theValue .

Any help will be mostly appreciated.

Thx beforehand.

I notice you have had 3 attempts at this question, which makes me wary about answering

However, is there any reason why you just can to the following

var propertyValue = fieldProperty.GetValue(fsr);
OracleParameter tmpPar = new OracleParameter()
{
   ParameterName = fieldProperty.Name,
   Value = propertyValue
      .GetType()
      .GetProperty("theValue")
      .GetValue(propertyValue)

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM