在c#中有一个包含许多字符串和整数的大对象。 例如:
object.something.something = "somestring";
object.something2.something = "somestring2";
我还有一个int,它表示对象中的一个元素。例如:
object.something.1 = "the content of number 1";
object.something.2 = "the content of number 2";
object.something.3 = "the content of number 3";
object.something.4 = "the content of number 4";
依旧......
如果我有一个包含诸如
之类的数字的变量int number = 3;
我需要使用它来查找object.something.3
答案 0 :(得分:1)
基本上,您有一个方法/属性的名称,您想要调用它。首先,我会考虑重新设计程序,但你不想,然后你可以使用反射
{% include 'builder/header.html.twig' %}
<div class="user-container" id="user-content">
{% block body %}
{% include 'builder/notices.html.twig' %}
<div class="user-container">
<i class="fas fa-user-edit fa-5x"></i>
</div>
<hr />
{{ form_start(form) }}
{{ form_row(form.username, { 'attr': {'class': 'form-control', 'value': app.user.username} }) }}
{{ form_row(form.email, { 'attr': {'class': 'form-control', 'value': app.user.email} }) }}
{{ form_row(form.password, { 'attr': {'class': 'form-control'} }) }}
{{ form_row(form.plainPassword.first, { 'attr': {'class': 'form-control'} }) }}
{{ form_row(form.plainPassword.second, { 'attr': {'class': 'form-control'} }) }}
<div class="register-btn-container">
<button class="btn btn-danger" id="return-to-dash-btn" type="button">Cancel!</button>
<button class="btn btn-primary" type="submit">Update!</button>
</div>
{{ form_end(form) }}
{% endblock %}
</div>
{% include 'builder/footer.html.twig' %}
如果是属性,则使用MethodInfo method = something.getType().GetMethod(number.ToString());
string result = (string)method.Invoke(something);
代替GetProperty(number.ToString()).GetGetMethod()