c# - using PropertyInfo to assign a value to a wrapper class with a custom indexer -



c# - using PropertyInfo to assign a value to a wrapper class with a custom indexer -

i need assign value via propertyinfo.

i'm having problems when type of property custom class (a wrapper around dictionary, designed contain multiple language versions of same text).

it looks that:

public class multilingualstring { dictionary<string, string> versions; public string this[string languagecode] { { if (versions.keys.contains(languagecode)) { homecoming versions[languagecode]; } homecoming null; } set { if (versions.keys.contains(languagecode)) { versions[languagecode] = value; } else { versions.add(languagecode, value); } } // [blah blah other stuff...] }

so; have propertyinfo object - , string value assign default language code.

certainpropertyinfo.setvalue( instance, // instance of class exposing multilingualstring type property somestring, new[] { "eng" }); // default language code

this throws exception.

i guess lastly argument of setvalue meant collection index , doesn't work custom indexer.

effectively i'm trying is, obviously:

instance.msproperty["eng"] = somestring;

but given name of msproperty, that's why i'm using reflection.

so far have thought implementing implicit operator (within multilingualstring class), allowing convert string values multilingualstring... can see problems approach eg. static operator hardly have way of "knowing" default language code is.

can accomplish goal via reflection?

the indexer property of own. need indexer property of instance in property of yours:

var multilingualstring = certainpropertyinfo.getvalue(instance, null); multilingualstring.gettype().getproperty("item").setvalue(multilingualstring, somestring, new object[]{ "eng" });

item default name indexer property.

if using .net 4.0, can utilize new dynamic type:

dynamic multilingualstring = certainpropertyinfo.getvalue(instance, null); multilingualstring["eng"] = somestring;

c# reflection indexer propertyinfo setvalue

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -