I'm not looking for a magic LINQ query, I am looking for a viable way to solve my problem in the simplest manner possible.
I'm stuck with a certain object hierarchy due to legacy code I cannot change. Imagine the following structure:
Settings.DefaultSettings -> Contains an instance for every settings type, one for each, that is the default when the type is instantiated
Settings.InstantiatedSettings -> Contains the instantiated settings
Using reflection and some other magic, two values are generated for each property, one is the property path inside a parent object (the Settings may be and is nested), the other is a path relative to a visual root in our application.
For my purposes, it is fine if the defaults have properties generated on a bare class instance, i.e.
public class SettingsTypeInstance
{
public string Property { get; set; } = "";
}
//Produces:
//root.Property <- causing problems
//rootWindow.Settings.Properts <- nevermind this one, it's fine
GenerateTwoValues(new SettingsTypeClass());
Now, the result of the generation (some more metadata) is stored and read as part of the Settings.DefaultSettings list. When an instance is generated, it's retrieved from the list, deep copied, and stored in the Settings.InstantiatedSettings. During the process, one of the two properties should be corrected to point at the new destination, so the final value of the result above should be
root.Settings.InstantiatedSettings.[0].Property instead of root.Property. LStrip the root., paste the path to the object, then repaste the root, sounds simple enough, right? I suspect I'm just too close to the project at this point to see a simple solution that doesn't involve hardcoding the string paths."
Problem is, there are quite a few places this procedure takes place, as there are several defaults instances.
What I need is something like
GeneratePath(rootObject=>rootObject.List1.IndexOf(i).List2.IndexOf(j).SomeSubProperty.SomeOtherSubProperty)
that would generate the string value
"List1.[2].List2.[3].SomeSubProperty.SomeOtherSubProperty"
for values of i=2 and j=3. As an upgrade, it could find the instance (provided there was guaranteed only one reference in the tree) just by giving something like
rootObject => SomeOtherSubProperty
and generate the path from that.
I suspect something like this could be done using Expressions and reflection, but am too lost in expressions to figure it out myself.
Any help on the matter would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire