C#反射使用变量作为对象.[var] [英] C# reflection use variable as object.[var]

查看:127
本文介绍了C#反射使用变量作为对象.[var]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用反射在剃刀视图中生成表,以从模型中提取属性.

I'm trying to generate a table in a razor view using reflection to pull the properties from the model.

这是我尝试过的:

@if (@Model.Count() > 0)
{
    System.Reflection.PropertyInfo[] properties = Model.First().GetType().GetProperties();
    <table>
        <thead>
            <tr>

            @foreach (var property in properties)
            {
                if (char.IsLower(property.Name.ToCharArray()[0])) //ignore foreign keys
                {
                    continue;
                }
                <th>@property.Name</th>
            }
            </tr>
        </thead>
        <tbody>
            @foreach (PCNWeb.Models.Switch item in Model)
            {
                /*System.Reflection.PropertyInfo[]*/ properties = item.GetType().GetProperties();
                <tr>
                @foreach (var property in properties)
                {                    
                    <td>
                        @Html.DisplayFor(modelItem => item.[property.Name])
                    </td>
                }
                </tr>

            }
        </tbody>
    </table>
}

让我指出我不确定该怎么做的代码部分:

Let me point out the part of the code that I'm not sure what to do with:

<td>
    @Html.DisplayFor(modelItem => item.[property.Name])
</td>

property.Name包含我要访问的项目的属性名称. 如果我要手写最里面的td,那么一个例子是:

The property.Name contains the name of the property of item that I want to access. If I were to hand write the innermost td one example would be:

<td>
    @Html.DisplayFor(modelItem => item.Switch_Location)
</td>

其中"Switch_Location"property.Name

因此,基本上我需要根据存储在变量中的属性名称访问项目属性的值.

So basically I need to access the value of a property of item based on the name of the property stored in a variable.

编辑添加模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace PCNWeb.Models
{
    public partial class Switch
    {
        public Switch()
        {
            this.Ports = new List<Port>();
            this.Switch_Location = new Switch_Location();
            this.Switch_Model = new Switch_Model();
            this.UPS = new UPS();
        }
        [Key]
        public int switchRecId { get; set; }
        [Required]
        public int locationRecId { get; set; }
        [Required]
        public int modelRecId { get; set; }
        //public int gatewayRecId { get; set; }
        [Required]
        public int upsRecId { get; set; }
        [Required]
        public int Number { get; set; }
        [Required]
        [StringLength(64)]
        public string Name { get; set; }
        [StringLength(80)]
        public string Description { get; set; }

        [StringLength(32)]
        public string Cabinet { get; set; }

        [StringLength(40)]
        public string Power_Feed { get; set; }
        [Required]
        public Nullable<int> ipOctet1 { get; set; }
        [Required]
        public Nullable<int> ipOctet2 { get; set; }
        [Required]
        public Nullable<int> ipOctet3 { get; set; }
        [Required]
        public Nullable<int> ipOctet4 { get; set; }

        public virtual ICollection<Port> Ports { get; set; }
        public virtual Switch_Location Switch_Location { get; set; }
        public virtual Switch_Model Switch_Model { get; set; }
        public virtual UPS UPS { get; set; }
    }
}

推荐答案

因此,基本上我需要根据存储在变量中的属性名称访问项目属性的值.

So basically I need to access the value of a property of item based on the name of the property stored in a variable.

否,您需要基于描述属性的PropertyInfo对象访问属性的值.这要容易得多.

No, you need to access the value of a property based on a PropertyInfo object describing it. That's far far easier.

property.GetValue(item)

这篇关于C#反射使用变量作为对象.[var]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆