使用反射(其中实际类型为通用)v2将属性动态转换为其实际类型 [英] Cast a property to its actual type dynamically using reflection (where actual type is generic) v2

查看:52
本文介绍了使用反射(其中实际类型为通用)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 中的布尔值来完成操作.

I do need to get the boolean value stored in IsSearchValue to to accomplish things.

预先感谢.

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

I mostly appreciate any helping answer.

推荐答案

fsr SmartRowVertrag .您正在尝试从中获取 IsSearchValue -但它不存在.

fsr is the SmartRowVertrag. You're trying to get the IsSearchValue from that - but it doesn't exist.

相反,您需要两次调用 GetValue -在 prop 上调用一次,并传入 fsr (因此等效于使用 fsr.ID dsr.VSNR ),然后在 propInfo 上传递一次第一次调用的结果.

Instead, you'll need to call GetValue twice - once on prop, passing in fsr (so which is equivalent to using fsr.ID or dsr.VSNR) and then once on propInfo passing in the result of that first call.

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

Next, your dynamic use is trying to get IsSearchValue on the result of propInfo.GetValue() - you're trying to evaluate something.IsSearchValue.IsSearchValue, effectively.

以下是该部分的更正代码:

Here's corrected code for that part:

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> 的非泛型接口或基类,则可以避免两次使用反射.例如:

You could avoid using reflection twice though if you had either a non-generic interface that DataFieldInfo2<T> implemented, or a base class. For example:

public interface IDataFieldInfo
{
    bool IsSearchValue { get; }
}

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

然后,您可以使反射代码更清晰:

You could then make your reflection code much clearer:

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

这篇关于使用反射(其中实际类型为通用)v2将属性动态转换为其实际类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆