【问题标题】:how to run through each class item and set value to it [duplicate]如何遍历每个类项并为其设置值[重复]
【发布时间】:2013-09-01 08:40:41
【问题描述】:

我有一个类,其中包含一些不同数据类型的项目。我想为每个项目设置值并将其从控制器发送到视图,并在文本框中查看数据。

是否可以循环遍历类中的这些对象,检查其数据类型,然后为每个对象赋值?

这是我正在尝试完成的伪代码示例

foreach item in class ItemProduction
{
    if data type  == string 
        value of item = " " ; 

    if data type  == int
        value of item = 0 ; 

    if data type  == DAte
       value of item = date of today ;          
 }

这是我的类定义和其中的对象。

public class ItemProduction : Common, IDataErrorInfo, INotifyPropertyChanged
{
    public string Main{ get; set; }
    public DateTime itemDate { get; set; }
    public string ItemType{ get; set; }
    public string productionId{ get; set; }
    public int Quantity { get; set; }
    public string Status { get; set; }

}

【问题讨论】:

  • 你只是想初始化ItemProduction的List吗?
  • @Bhushan Firake 我想为每个项目设置值,但首先我想检查每个项目的数据类型
  • @user2269364 请检查我的回答。

标签: c# asp.net asp.net-mvc-3 class foreach


【解决方案1】:

使用 if 语句根据数据类型设置值不是一个好主意。 如果您只想将此 ItemProduction 类的对象作为模型传递给您的视图,只需为您的 ItemProduction 类创建一个构造函数并使用默认值初始化您的模型值并将其发送到您的视图

 public class ItemProduction 
{
    public string Main { get; set; }
    public DateTime itemDate { get; set; }
    public string ItemType { get; set; }
    public string productionId { get; set; }
    public int Quantity { get; set; }
    public string Status { get; set; }
    public ItemProduction()
    {
        Main = "main value";
        itemDate =DateTime.Now;
        ItemType ="type";
        productionId ="productionId";
        Quantity =5;
        Status = "status";
    }
}

如果您想使用反射动态设置值,请参考以下问题: c# - How to iterate through classes fields and set properties

【讨论】:

  • 和我在回答中说的不一样吗?
【解决方案2】:

你可以试试这个:

 List<ItemProduction> items = Enumerable.Range(1, 5).Select(i => new ItemProduction
 {
      Main = string.Empty,
      ItemDate = DateTime.UtcNow,
      ItemType = string.Empty,
      ProductionId = string.Empty,
      Quantity = 0,
      Status = string.Empty
 }).ToList();

这将创建 5 个 ItemProduction 实例,并根据您的需要使用给定的值初始化它们。

最好的办法是在您的 ItemProduction 模型中有一个构造函数,如下所示:

public class ItemProduction
{
   public string Main { get; set; }
   public DateTime TtemDate { get; set; }
   public string ItemType { get; set; }
   public string ProductionId { get; set; }
   public int Quantity { get; set; }
   public string Status { get; set; }

   public ItemProduction()
   {
       this.Main = string.Empty;
       this.ItemDate = DateTime.UtcNow;
       this.ItemType = string.Empty;
       this.ProductionId = string.Empty;
       this.Quantity = 0;
       this.Status = string.Empty;
   }
}

那么,上面的代码就变成了

List<ItemProduction> items= Enumerable.Range(1, 5).Select(i => new ItemProduction()).ToList();

注意:

将类属性设置为 PascalCase 是一种约定,因此我已使用嵌套命名约定修改了您的类。您可以通过此 Property Naming Guidelines.

更新:

如果你有很多属性,你可以使用反射来做到这一点,如下所示:

public static T Init<T>(T TObject)
{
  var properties = TObject.GetType().GetProperties();
  foreach (var property in properties)
  {
     if (property.PropertyType.Equals(typeof(string)))
     {
         property.SetValue(TObject, string.Empty);
     }
     else if (property.PropertyType.Equals(typeof(int)))
     {
         property.SetValue(TObject, 0);
     }
     else if (property.PropertyType.Equals(typeof(DateTime)))
     {
         property.SetValue(TObject, DateTime.UtcNow);
     }
  }
  return TObject;
}

【讨论】:

  • 但如果我想为更多项目设置值,而不仅仅是 5 个?
  • @user2269364 只需传递您要为其设置值的项目数..
  • 是的,但我的意思是当我设置值时,我应该为每一个设置值,一一设置吗?
  • 好吧,工作太多了。我正在考虑检查每个项目的数据类型并为其设置值的 en foreach 循环。类似 if(item.data type is string) { item.value = "" ; }
  • @user2269364 那么,既然你的问题已经解决了,可以考虑接受我的回答吗?
猜你喜欢
  • 2017-11-26
  • 2013-11-14
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
  • 2016-03-31
  • 2013-11-03
  • 2013-09-22
  • 1970-01-01
相关资源
最近更新 更多