Foreach语句不能对'object'类型的变量进行操作,因为'object'不包含'getenumerator的公共定义 [英] Foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'getenumerator

查看:258
本文介绍了Foreach语句不能对'object'类型的变量进行操作,因为'object'不包含'getenumerator的公共定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,我有一个类,我在每个数组中有4个数组,我有参数,所以我尝试使用for-each循环在数组列表中分配参数,但我得到了这个错误,所以请帮助我。



我尝试过:



这是我的主要课程:

-----------------------

Hi sir, i have one class in that i have 4 arrays in each array i have parameters so i tried to assign parameters in the array list using for-each loop but i got this error so please help me.

What I have tried:

This is my main class:
-----------------------

public class BookRequest
{
        public string EndUserIp { get; set; }
        public string TokenId { get; set; }
        public string TraceId { get;set; }
        public int ResultIndex { get; set; }
        public string HotelCode { get; set; }
        public string HotelName { get; set; }
        public string GuestNationality { get; set; }
        public int NoOfRooms { get; set; }
        public string ClientReferenceNo { get; set; }
        public Boolean IsVoucherBooking { get; set; }
        public List<hotelroomsdetails> RoomInfo { get; set; }
        public int RoomIndex { get; set; }
        public string RatePlanCode { get; set; }
        public string RoomTypeCode { get; set; }
        public string RoomTypeName { get; set; }
        public List<bedtypes> bedtype { get; set; }
        public string SmokingPreference { get; set; }
        public List<string> Supplements { get; set; }
        public Price price { get; set; }
        public string CurrencyCode { get; set; }
        public decimal RoomPrice { get; set; }
        public decimal Tax { get; set; }
        public decimal ExtraGuestCharge { get; set; }
        public decimal ChildCharge { get; set; }
        public decimal OtherCharges { get; set; }
        public decimal Discount { get; set; }
        public decimal PublishedPrice { get; set; }
        public int PublishedPriceRoundedOff { get; set; }
        public decimal OfferedPrice { get; set; }
        public decimal AgentCommission { get; set; }
        public decimal AgentMarkUp { get; set; }
        public decimal TDS { get; set; }
        public decimal ServiceTax { get; set; }
        public List<hotelpassenger> hotelpass { get; set; }
}

推荐答案

您无法使用<$ c迭代对象的属性$ c> foreach 甚至是 for 循环 - 或者你可以,但它需要大量的反射工作,它会创建代码难以维护。

要使用 foreach ,目标对象必须是实现iEnumerable或iEnumerable< T>的类的实例。 - 诸如List< T>之类的其他对象的集合。甚至是ArrayList。你不能只是在那里找一个随机类的实例,并希望它知道你想要在每个属性上执行相同的任务。



如果你想要分配参数,你真的应该一个接一个地做,不要尝试使用循环 - 否则你会遇到维护问题。
You can't iterate over the properties of an object with a foreach or even a for loop - or you can, but it takes a good deal of work with reflection, and it creates code that is hard to maintain.
To use foreach the target object must e an instance of a class that implements iEnumerable or iEnumerable<T> - a Collection of other objects such as a List<T> or even an ArrayList. You can't just plonk an instance of a "random" class there and expect it to know that you want to perform the same task on each property.

If you want to assign parameters, you really should do it one by one, not try to work with a loop - you will get maintenance problems if you don't.


public class BookRequest:IEnumerable< KeyValuePair< string,object>>

{

public string EndUserIp {get;组; }

公共字符串TokenId {get;组; } $ / $
公共字符串TraceId {get; set; } $ / $
public int ResultIndex {get;组; }

公共字符串HotelCode {get;组; }

公共字符串HotelName {get;组; }

public string GuestNationality {get;组; } $ / $
public int NoOfRooms {get;组; } $ / $
public string ClientReferenceNo {get;组; } $ / $
public Boolean IsVoucherBooking {get;组; } b / b
public IEnumerator< KeyValuePair< string,object>> GetEnumerator()

{

返回this.GetType()。GetProperties(BindingFlags.Public | BindingFlags.Instance).ToDictionary(p => p.Name,p = > p.GetGetMethod()。Invoke(this,null))。GetEnumerator();

}



IEnumerator IEnumerable.GetEnumerator( )

{

抛出新的NotImplementedException();

}

}







现在用于迭代



var obj = new MyModel() ;

foreach(obj中的var kv)

Console.WriteLine(kv.Key +:+ kv.Value ??null);
public class BookRequest : IEnumerable<KeyValuePair<string, object>>
{
public string EndUserIp { get; set; }
public string TokenId { get; set; }
public string TraceId { get;set; }
public int ResultIndex { get; set; }
public string HotelCode { get; set; }
public string HotelName { get; set; }
public string GuestNationality { get; set; }
public int NoOfRooms { get; set; }
public string ClientReferenceNo { get; set; }
public Boolean IsVoucherBooking { get; set; }

public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToDictionary(p => p.Name, p => p.GetGetMethod().Invoke(this, null)).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}



now for iteration

var obj = new MyModel();
foreach(var kv in obj)
Console.WriteLine(kv.Key + ": " + kv.Value ?? "null");


这篇关于Foreach语句不能对'object'类型的变量进行操作,因为'object'不包含'getenumerator的公共定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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