繁体   English   中英

C#使用字符串数组进行反射?

[英]C# using array of Strings for reflection?

我正在尝试将包含Id=23||Header=This is a header||Description=This is a description的搜索字符串拆分成两个数组,可以在以下上下文中使用c.item[i] = property[i] 我尝试了以下解决方案,但与任何类型的帮助均不匹配,将不胜感激:)

        string[] stringSeparators = new string[] {"||"};

        string[] testvalues = selectedSavedSearch.SearchString.Split(stringSeparators, StringSplitOptions.None).Select(sValue => sValue.Trim()).ToArray();

        string[] items = new string[testvalues.Count()] ;
        string[] properties = new string[testvalues.Count()] ;

        for (int i = 0; i < testvalues.Count(); i++)
        {
            string[] values;
            values = testvalues[i].Split('=').Select(sValue => sValue.Trim()).ToArray();
            if (values.Count() > 0)
            {
                items[i] = values[0];
            }
            if (values.Count() > 1)
            {
                properties[i] = values[1];
            }

        }

        for (int i = 0; i < items.Count(); i++)
        {
            currentSearch = typeof(BugManagerQueryOptions).GetProperty(items[i].ToString()).GetValue(properties[i], null);
        }

我认为您在滥用GetValue:

currentSearch = typeof(BugManagerQueryOptions).GetProperty(items[i].ToString()).GetValue(properties[i], null);

这是通过反射获取属性值的一种方法:

PropertyInfo property = typeof(BugManagerQueryOptions).GetProperty(items[i]);

// query is the instance of BugManagerQueryOptions that you're trying to get the value from
var value= property.GetValue(query, null);

您正在将属性值传递给property.GetValue,这可能会引发异常。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnitTest
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BugManagerQueryOptions bmqo = new BugManagerQueryOptions
            {
                Id = 64,
                Header = "This is a header, that has not been set by reflection (yet)",
                Description = "This is a description that has not been set by reflection (yet)"
            };

            var pairs = "Id=23||Header=This is a header||Description=This is a description"
                .Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries)
                .Select(q => new KeyValuePair<string, string>(q.Split('=')[0], q.Split('=')[1]));

            // TEST : Getting values from the BugManagerQueryOptions instance (bmqo)
            foreach (KeyValuePair<string, string> pair in pairs)
            {
                Console.WriteLine(typeof(BugManagerQueryOptions).GetProperty(pair.Key).GetValue(bmqo, null));
            }

            // TEST : Setting values to the BugManagerQueryOptions instance (bmqo)
            foreach (KeyValuePair<string, string> pair in pairs)
            {
                if (typeof(BugManagerQueryOptions).GetProperty(pair.Key).PropertyType == typeof(Int32))
                {
                    typeof(BugManagerQueryOptions).GetProperty(pair.Key).SetValue(bmqo, Int32.Parse(pair.Value), null);
                }
                else
                {
                    typeof(BugManagerQueryOptions).GetProperty(pair.Key).SetValue(bmqo, pair.Value, null);
                }
            }

            // TEST: Getting values from the BugManagerQueryOptions instance (bmqo) AFTER being set by reflection
            foreach (KeyValuePair<string, dynamic> pair in pairs)
            {
                Console.WriteLine(typeof(BugManagerQueryOptions).GetProperty(pair.Key).GetValue(bmqo, null));
            }

            Console.Read();
        }
    }

    public class BugManagerQueryOptions
    {
        public Int32 Id { get; set; }
        public String Header { get; set; }
        public String Description { get; set; }
    }
}

暂无
暂无

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

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