Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从变量名访问属性的子属性_C#_System.reflection - Fatal编程技术网

C# 如何从变量名访问属性的子属性

C# 如何从变量名访问属性的子属性,c#,system.reflection,C#,System.reflection,我想修改模型中列表中的属性。因为有很多这样的列表,所以我使用reflection GetType().GetProperty(propertyName)按如下方式进行操作: public class SalesViewModel { public List<SelectListItem> SourceTypes0 { get; set; } ... public List<SelectListItem> SourceTypes9 { get; set; } } Sale

我想修改模型中列表中的属性。因为有很多这样的列表,所以我使用reflection GetType().GetProperty(propertyName)按如下方式进行操作:

public class SalesViewModel
{
public List<SelectListItem> SourceTypes0 { get; set; }
...
public List<SelectListItem> SourceTypes9 { get; set; }
}

SalesViewModel model = new SalesViewModel();
model.SourceTypes0 = from r in this._repository.SourceTypes
                    select new SelectListItem
                    {
                        Text = r.Name,
                        Value = SqlFunctions.StringConvert((double)r.SourceTypeID)
                    }).OrderBy(c => c.Text).ToList();
...
model.SourceTypes9 = from r in this._repository.SourceTypes
                    select new SelectListItem
                    {
                        Text = r.Name,
                        Value = SqlFunctions.StringConvert((double)r.SourceTypeID)
                    }).OrderBy(c => c.Text).ToList();

for (int i = 0; i < 10; i++)
{
string propertyName = "SourceTypes" + i.ToString();
var propInfo = model.GetType().GetProperty(propertyName);
if (propInfo != null)
{
propInfo.SetValue(model, "...", null);
}
}
公共类SalesViewModel
{
公共列表SourceTypes0{get;set;}
...
公共列表SourceTypes9{get;set;}
}
SalesViewModel模型=新的SalesViewModel();
model.SourceTypes0=来自此存储库中的r.\u repository.SourceTypes
选择新的SelectListItem
{
Text=r.名称,
Value=SqlFunctions.StringConvert((双精度)r.SourceTypeID)
}).OrderBy(c=>c.Text).ToList();
...
model.SourceTypes9=来自此存储库中的r.\u repository.SourceTypes
选择新的SelectListItem
{
Text=r.名称,
Value=SqlFunctions.StringConvert((双精度)r.SourceTypeID)
}).OrderBy(c=>c.Text).ToList();
对于(int i=0;i<10;i++)
{
string propertyName=“SourceTypes”+i.ToString();
var propInfo=model.GetType().GetProperty(propertyName);
if(propInfo!=null)
{
propInfo.SetValue(模型,“…”,null);
}
}
如果要修改model.SourceTypes0[1]。所选字段,问题是如何强制转换propInfo以列出或访问SourceTypes0[1]。所选字段。我试着

List<SelectListItem> propInfo = (List<SelectListItem>)model.GetType().GetProperty(propertyName);
List-propInfo=(List)model.GetType().GetProperty(propertyName);
但它也给出了错误

Cannot convert System.Reflection.PropInfo to System.Collections.Generic.List<System.Web.Mvc.SelectListItem>
无法将System.Reflection.PropInfo转换为System.Collections.Generic.List

我需要帮助。谢谢。

方法
GetProperty()
返回
PropertyInfo
。如果要获取该属性的值,需要调用其
GetValue()
方法:

var propInfo = model.GetType().GetProperty(propertyName);
var theList = (List<SelectListItem>)propInfo.GetValue(model);
var-propInfo=model.GetType().GetProperty(propertyName);
var theList=(List)propInfo.GetValue(model);

将来,清理文章中的代码会更容易阅读。好的,我希望你更好地在编辑器中实现tab键,这样清理代码就会容易得多。我不能(我不是管理员或任何人),但我同意这会很好。