【问题标题】:How can I pass a LIST<T> to as message to pubsub in c#如何在 C# 中将 LIST<T> 作为消息传递给 pubsub
【发布时间】:2019-05-21 08:55:52
【问题描述】:

这里是新手程序员的问题。我有一个 pubsub 的发布者代码,我在其中从数据库查询并将结果存储在 C# 中的 LIST 中。现在我需要将该 LIST 作为消息传递给 pubsub 主题。我怎样才能做到这一点?

var egv_data = do_cgmdata_query(conn); //query method
if (egv_data != null)
{
    while (egv_data.Read())
    {
        Participant_List_Data.Add(new Participant_Data()
        {
            fitbit_id = (string)egv_data[0],
            sf_candidate_id = (string)egv_data[1],
            egv = (string)egv_data[2],
            egv_datetimelocal = (string)egv_data[3]
        });
    }
    egv_data.Close();

    //publish a message to the topic
    foreach (Participant_Data aList in Participant_List_Data)
    {
        PubsubMessage message = new PubsubMessage
        {
            Data = aList /*need to pass this correctly to pubsub topic, aList.fitbit_id, aList.egv...etc*/
            Attributes = {
                {"version","1.0"}
            }
        };
        //publish the message to topic
        pub.Publish(topicName, new[] { message });
    }
}

【问题讨论】:

  • PubsubMessage.DataParticipant_Data 类型的对象吗?

标签: c# publish-subscribe


【解决方案1】:

在这种情况下,您必须使用 C# 泛型,如下所示:-

像这样创建你的类

public class PubsubMessage<T>
{
   public PubsubMessage(List<T> Data, Object someObject)// This object is just an example but replace it with some type or class in actual code
   {
   }
}

然后你可以将任何类型传递给构造函数。去研究一些关于 C# 泛型的主题,你就会明白了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2012-10-14
    相关资源
    最近更新 更多