开发者

using PropertyInfo to assign a value to a wrapper class with a custom indexer

开发者 https://www.devze.com 2023-03-19 23:00 出处:网络
I need to assign a value via PropertyInfo. I\'m having some problems when the type of the property is my custom class (a wrapper around a dictionary, designed to contain multiple language versions of

I need to assign a value via PropertyInfo.

I'm having some problems when the type of the property is my custom class (a wrapper around a dictionary, designed to contain multiple language versions of the same text).

It looks like that:

    public class MultilingualString
    {            开发者_如何学Python   
        Dictionary<string, string> Versions;
        public string this[string languageCode]
        {
            get
            {
                if (Versions.Keys.Contains(languageCode))
                {
                    return Versions[languageCode];
                }
                return null;
            }
            set
            {
                if (Versions.Keys.Contains(languageCode))
                {
                    Versions[languageCode] = value;
                }
                else
                {
                    Versions.Add(languageCode, value);
                }
            }
            // [blah blah other stuff...]    
        }

So; now I have this PropertyInfo object - and a string value I would like to assign with a default language code.

certainPropertyInfo.SetValue(
   instance, // an instance of some class exposing a MultilingualString type property 
   someString,
   new[] { "eng" }); // some default language code

This throws an exception.

I guess the last argument of SetValue is meant to be a collection index and it doesn't work with a custom indexer.

Effectively what I'm trying to do is, obviously:

   instance.msProperty["eng"] = someString;

But I am only given the name of msProperty, that's why I'm using reflection.

So far I have thought about implementing an implicit operator (within the MultilingualString class), allowing to convert string values to MultilingualString... but I can see some problems with that approach eg. this static operator would hardly have a way of "knowing" what the default language code is.

Can I achieve my goal via reflection?


The indexer is a property of its own. You need to get the indexer property of the instance in that certain property of yours:

var multilingualString = certainPropertyInfo.GetValue(instance, null);
multilingualString.GetType().GetProperty("Item").SetValue(multilingualString,
                                                          someString,
                                                          new object[]{ "eng" });

Item is the default name for the indexer property.

If you are using .NET 4.0, you can use the new dynamic type:

dynamic multilingualString = certainPropertyInfo.GetValue(instance, null);
multilingualString["eng"] = someString;
0

精彩评论

暂无评论...
验证码 换一张
取 消