繁体   English   中英

使用反射动态地将属性转换为其实际类型(其中实际类型是通用的)v2

[英]Cast a property to its actual type dynamically using reflection (where actual type is generic) v2

这是我之前在这里提出的问题的稍微修改版本。

实际位置如下:

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

我确实需要获取存储在IsSearchValue的布尔值才能完成任务。

先谢谢了。

我非常感谢任何有帮助的答案。

fsrSmartRowVertrag 您正在尝试从中获取IsSearchValue - 但它不存在。

相反,您需要调用GetValue两次 - 一次在prop ,传入fsr (因此等效于使用fsr.IDdsr.VSNR ),然后在propInfopropInfo传入第一次调用的结果。

接下来,您的dynamic使用正在尝试根据propInfo.GetValue()的结果获取IsSearchValue - 您正在尝试有效地评估something.IsSearchValue.IsSearchValue

这是该部分的更正代码:

public static object RetData3(object fsr)
{
    object retVal = new object();
    var props = fsr.GetType().GetProperties();
    foreach (var prop in props)
    {
        PropertyInfo propInfo = prop.PropertyType.GetProperty("IsSearchValue");
        if (propInfo != null)
        {
            object fieldInfo = prop.GetValue(fsr);
            object isSearchValue = propInfo.GetValue(fieldInfo);
            if (isSearchValue.Equals(true))
            {
                Console.WriteLine($"{prop.Name} has a searchable field");
            }
        }
    }
    return retVal;
}

如果您有DataFieldInfo2<T>实现的非通用接口或基类,则可以避免使用反射两次。 例如:

public interface IDataFieldInfo
{
    bool IsSearchValue { get; }
}

public class DataFieldInfo2<T> : IDataFieldInfo
{
    ...
}

然后你可以让你的反射代码更清晰:

public static object RetData3(object fsr)
{
    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)
        {
            Console.WriteLine($"{fieldProperty.Name} is a search value field");
        }
    }
    
    // We don't actually know what you're trying to return
    return null;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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