简体   繁体   中英

Dynamic variable name with “this.” keyword

Example:

public string varName = "val";
public string val = "some value";

public void meth() 
{
    // Actually need: Trace.WriteLine( this.val);
    Trace.WriteLine( this+ "this.varName"); 
}

Actually, I need print some value , that is, I need concatenate this. keyword with string value, and create something like dynamic variable , but not found what is right syntax for this.

public void meth()
{
    var value = GetType().GetField(varName).GetValue(this);
    Trace.WriteLine(value); // "some value"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM