C# 字符串访问属性
标签:bug this oat tty ret 字符串 setvalue his tor
//1.公开字段的实现
public class Foo{
public float aa=100;
public void test(){
this["aa"]=300;
}
public float this[string name]{
get{
return (float)GetType().GetField(name).GetValue(this);
}
set{
GetType().GetField(name).SetValue(this,value);
}
}
}
//2.私有属性的实现
public class Foo{
private float m_aa=100;
public float aa{ get=>m_aa; set=>m_aa=value;}
public void test(){
this["aa"]=300;
}
public float this[string name]{
get{
return (float)GetType().GetProperty(name).GetValue(this);
}
set{
GetType().GetProperty(name).SetValue(this,value);
}
}
}
var v=new Vector2(10,20);
Debug.Log(v.GetType().GetField("x").GetValue(v));//output:10
Debug.Log(v.GetType().GetField("y").GetValue(v));//output:20
var foo=new Foo();
Debug.Log(foo["aa"]);//output:100
foo["aa"]=200;
Debug.Log(foo["aa"]);//output:200
foo.test();
Debug.Log(foo["aa"]);//output:300
C# 字符串访问属性
标签:bug this oat tty ret 字符串 setvalue his tor
温馨提示: 本文由杰米博客推荐,转载请保留链接: https://www.jmwww.net/file/14295.html
- 上一篇:Win10安装gcc、g 、make
- 下一篇:C# 打印Word文档