小编Dim*_*oof的帖子

如何在C#中的类中动态设置变量的值?

我正在尝试做这样的事情:

public class MyClass()
{
  private void SetAValiable(int boolNumber)
  {
    bool b1 = false;
    bool b2 = false;

    ("b" + boolNumber) = true;
  }
}
Run Code Online (Sandbox Code Playgroud)

我试过这个但是不断从GetProperty调用中得到一个null:

Type myType = typeof(MyClass);
PropertyInfo pinfo = myType.GetProperty("b" + boolNumber);
pinfo.SetValue(myType, true, null);
Run Code Online (Sandbox Code Playgroud)

任何人都有任何想法如何让这个工作?

谢谢!

c# reflection

-2
推荐指数
1
解决办法
82
查看次数

标签 统计

c# ×1

reflection ×1