10

I am trying to access an object stored in a dictionary of type String, UnknownClass. I have the key, and know the value is one of several container classes. Since the value is passed to a method that takes an object, I do not need to know the type of the class stored as a value. I also need to call ContainsKey to confirm the key exists.

I have tried the following methods with no success:

Dictionary<String, object> list = (Dictionary<String, object>)source.GetType().GetProperty(dictionaryName).GetValue(source, null);
nextSource = list[key];

Which gives me a casting error, and:

nextSource = source.GetType().GetMethod("get_Item").Invoke(source, new object[] { key });

Which gives me a null reference exception.

Here is a bit more of the code, although I am not quite sure it will help much.

private void SetValue(object source, String path, String value)
{
    if (path.Contains('.'))
    {
        //  If this is not the ending Property, continue recursing
        int index = path.IndexOf('.');
        String property = path.Substring(0, index);

        object nextSource;
        if(property.Contains("*"))
        {
            path = path.Substring(index + 1);
            index = path.IndexOf('.');
            String dictionaryName = path.Substring(0, index);

            Dictionary<String, object> list = (Dictionary<String, object>)source.GetType().GetProperty(dictionaryName).GetValue(source, null);
            nextSource = list[property.Substring(1)];
            //property = property.Substring(1);
            //nextSource = source.GetType().GetMethod("Item").Invoke(source, new[] { property });
        } ...

The dictionary being accessed is defined in the PersonObject class as such:

public class PersonObject
{
    public String Name { get; set; }
    public AddressObject Address { get; set; }
    public Dictionary<String, HobbyObject> Hobbies { get; set; }

The value of Path, at this stage, is set to "*Hiking.Hobbies.Hobby". Basically, the path string allows me to navigate to a Property in a subclass, and I need the Dictionary to access properties for a list of the same class.

4

2 回答 2

9

Dictionary<TKey, TValue> 类实现了非泛型IDictionary 接口object因此,如果您有一个包含对实例的引用的 tye 变量,则Dictionary<String, HobbyObject>可以按如下方式检索字典中的值:

object obj = new Dictionary<String, HobbyObject>
{
    { "Hobby", new HobbyObject() }
};

IDictionary dict = obj as IDictionary;
if (dict != null)
{
    object value = dict["Hobby"];
    // value is a HobbyObject
}
于 2013-07-12T22:33:52.243 回答
3

It sounds like you're trying to do this:

var source = new Dictionary<string, object>();
var key = "some key";
source.Add(key, "some value");

var property = source.GetType().GetProperty("Item");
var value = property.GetValue(source, new[] { key });

Console.WriteLine(value.ToString()); // some value

Update: The problem is that a Dictionary<string, HobbyObject> cannot be cast to a Dictionary<string, object>. I think you'd have to do this:

private void SetValue(object source, String path, String value)
{
    if (path.Contains('.'))
    {
        //  If this is not the ending Property, continue recursing
        int index = path.IndexOf('.');
        String property = path.Substring(0, index);

        object nextSource;
        if(property[0] = '*')
        {
            path = path.Substring(index + 1);
            index = path.IndexOf('.');
            String dictionaryName = path.Substring(0, index);

            property = property.Substring(1);

            Object list = source.GetType().GetProperty(dictionaryName)
                                .GetValue(source, null);
            nextSource = list.GetType().GetProperty("Item")
                             .GetValue(list, new[] { property });
        }
于 2013-07-12T21:25:12.777 回答