0

我正在尝试将包含的搜索字符串拆分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);
        }
4

2 回答 2

0
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; }
    }
}
于 2013-09-11T14:43:23.623 回答
0

我认为您在滥用 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,这可能会引发异常。

于 2013-09-11T14:27:20.087 回答