如何创建一个使用不同对象类型调用的方法,使用该参数调用强类型方法?

How can I create one method called with different object types call a strongly typed method with that parameter?

提问人:Eddie Green 提问时间:11/9/2023 更新时间:11/9/2023 访问量:14

问:

我目前有两个方法,除了传入的对象类型和使用该对象的方法调用之外,它们是相同的。

对象:

  • 邮戳留言
  • TempatedPostmarkMessage

方法调用(接受任一对象类型)

  • 客户。SendMessageAsync(<PostmarkMessage 或 TemplatedPostmarkMessage>)

如果可能的话,我怎样才能使这种方法成为一种方法?

    public static async Task<bool> SendPostmarkMessage(PostmarkMessage postmarkMessage)
    {
        var client = new PostmarkClient(ServiceConfig.PostmarkClientId);

        var messageSent = false;

        var retries = 0;

        while(retries < 5 && !messageSent)
        {
            try
            {
                var response = await client.SendMessageAsync(postmarkMessage);

                if(response.Status == PostmarkStatus.Success)
                {
                    messageSent = true;
                }
                else
                {
                    LogEvent(
                        LogLevel.Warn,
                        ServiceLogger,
                        $"Postmark response: {response.Message}"
                    );

                    LogEvent(
                        LogLevel.Warn,
                        ServiceLogger,
                        $"Sending Postmark message failed. Retrying..."
                    );

                    retries++;

                    await Task.Delay(1000);
                }
            }
            catch(Exception ex)
            {
                LogEvent(
                    LogLevel.Warn,
                    ServiceLogger,
                    $"Postmark response: {ex.Message}"
                );

                LogEvent(
                    LogLevel.Warn,
                    ServiceLogger,
                    $"Sending Postmark message failed. Retrying..."
                );

                retries++;

                await Task.Delay(1000);
            }
        }

        return messageSent;
    }

    public static async Task<bool> SendPostmarkMessage(TemplatedPostmarkMessage templatedPostmarkMessage)
    {
        var client = new PostmarkClient(ServiceConfig.PostmarkClientId);

        var messageSent = false;

        var retries = 0;

        while(retries < 5 && !messageSent)
        {
            try
            {
                var response = await client.SendMessageAsync(templatedPostmarkMessage);

                if(response.Status == PostmarkStatus.Success)
                {
                    messageSent = true;
                }
                else
                {
                    LogEvent(
                        LogLevel.Warn,
                        ServiceLogger,
                        $"Postmark response: {response.Message}"
                    );

                    LogEvent(
                        LogLevel.Warn,
                        ServiceLogger,
                        $"Sending Postmark message failed. Retrying..."
                    );

                    retries++;

                    await Task.Delay(1000);
                }
            }
            catch(Exception ex)
            {
                LogEvent(
                    LogLevel.Warn,
                    ServiceLogger,
                    $"Postmark response: {ex.Message}"
                );

                LogEvent(
                    LogLevel.Warn,
                    ServiceLogger,
                    $"Sending Postmark message failed. Retrying..."
                );

                retries++;

                await Task.Delay(1000);
            }
        }

        return messageSent;
    }

我尝试创建一个泛型方法,但不明白如何将泛型对象转换为该方法所需的强类型对象。

泛型多 性特征

评论


答: 暂无答案