【问题标题】:Removing properties of an object in List<>删除 List<> 中对象的属性
【发布时间】:2016-10-14 21:03:32
【问题描述】:

我正在收听进入我的服务器的“推送”通知。我已经使用所有可能的属性设置了SubscriptionModel,并且我可以正确地遍历通过的 JSON 主体,解析每个订阅,并在返回我创建的列表之前修改输出。但是,我想知道当我根本不需要返回 SubscriptionModel 的属性时,我该如何去删除它们;或者如果它们为空,则在回复 List&lt;SubscriptionModel&gt; subscriptions 之前将其删除。

namespace TextMessagingListener.Controllers
{
    public class SubscriptionModel
    {
        public long push_id { get; set; }
        public string request_id { get; set; }
        public string subscription_id { get; set; }
        public string message { get; set; }
        public string status_code { get; set; }
        public string error_message { get; set; }
    }

    [Route("api/[controller]")]
    public class SubscriptionController : Controller 
    {
        // PUT api/subscription
        [HttpPut]
        public List<SubscriptionModel> Put([FromBody] List<SubscriptionModel> model)
        {
            // Receive a report of whether your subscription(s) was successfully added or not.
            List<SubscriptionModel> subscriptions = new List<SubscriptionModel>();

            foreach (SubscriptionModel m in model)
            {
                m.message = "Push notification successfully received.";
                subscriptions.Add(m);
            }

            return subscriptions;
        }
    }
}

我能想到的唯一解决方案是创建另一个仅用于返回信息的对象;并应用我要发送的每个subscriptions 项目。

【问题讨论】:

标签: c# asp.net oop


【解决方案1】:

你不能。你需要另一堂课。仅包含属性的“轻量级”版本。或者你可以做一个匿名类型,但这很难使用。不过,我同意其他人的命名约定 :)。

【讨论】:

  • 我想得越多,我就越认为我只需要另一个类/对象(我应该使用哪个术语?)。 Anonymous Type 听起来很有趣,但目前我可能无法掌握。
  • @TheGuitarShawn,SubscriptionLiteModel 或类似的东西没有错。如果您将它公开为 API 或类似的东西,我可能会选择一个不同的名称,但仅限于内部,类似的东西很好。
猜你喜欢
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
相关资源
最近更新 更多