【问题标题】:Cast a property to its actual type dynamically using reflection (where actual type is generic) v3使用反射将属性动态转换为其实际类型(其中实际类型是通用的)v3
【发布时间】:2020-10-23 08:31:23
【问题描述】:

感谢所有在我之前的两个问题herehere 中帮助我的人。

这是代表代码片段:

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

有什么方法可以独立于类型从fieldProperty 获取theValue?由于OracleParameterValue 属性是对象,我预计用theValue 设置它不会有任何困难。

任何帮助将不胜感激。

提前谢谢。

【问题讨论】:

    标签: c# generics casting propertyinfo


    【解决方案1】:

    我注意到你已经尝试了 3 次这个问题,这让我对回答持谨慎态度

    但是,你有什么理由可以做到以下几点

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      相关资源
      最近更新 更多