c# - Fetch value from string expression like "Add Watch" feature -


public class ufmline {     public ufmtemplate elementtag;     public int posx1;     public int posy1;     public int posx2;     public int posy2;     public string property;     public string hierstr = string.empty;     public list<ufmline> ufmlines = new list<ufmline>(); // tricky nested class field }  ufmline ufmobj = new ufmline(); 

this ufmobj populated @ window load.

in button click of wpf window xaml code behind...

string nthitem =  "ufmobj.ufmlines[0].ufmlines[1].ufmlines[1].ufmlines[1].ufmlines[2].ufmlines[2].elementtag";   // tried reflection method, giving null exception  var result = typeof(ufmline).getfield(nthitem).getvalue(ufmobj); 

so when opening watch window , fetch value nthitem name gives appropriate value.

how in code behind or not using reflection?

thanks.

you may change fields of ufmline class public properties:

public class ufmline {     public ufmtemplate elementtag { get; set; }     public int posx1 { get; set; }     public int posy1 { get; set; }     public int posx2 { get; set; }     public int posy2 { get; set; }     public string property { get; set; }     public string hierstr { get; set; } = string.empty;     public list<ufmline> ufmlines { get; set; } = new list<ufmline>(); } 

now use part of nthitem string after "ufmobj." path of binding, use ufmobj source:

var binding = new binding {     source = ufmobj,     path = new propertypath("ufmlines[0].ufmlines[1].ufmlines[1].ufmlines[1].ufmlines[2].ufmlines[2].elementtag") }; 

to make use of binding, need target dependency property, might declare in helper class this:

public class bindinghelper : dependencyobject {     public static readonly dependencyproperty resultproperty =         dependencyproperty.register("result", typeof(object), typeof(mainwindow));      public object result     {         { return getvalue(resultproperty); }         set { setvalue(resultproperty, value); }     } } 

finally assign binding target property , retrieve resulting value this:

var bindinghelper = new bindinghelper(); bindingoperations.setbinding(bindinghelper, bindinghelper.resultproperty, binding); var result = bindinghelper.result; 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -