using MediatR;

namespace ManageCore.MediatrDemo
{
    /// <summary>
    /// 通知处理器类
    /// </summary>
    /// <param name="_logger"></param>
    public class InfoDemoHandler(ILogger<InfoDemoHandler> _logger) : INotificationHandler<InfoDemo>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task Handle(InfoDemo notification, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"InfoDemoHandler Received: {notification}. {DateTimeOffset.Now}");
            return Task.CompletedTask;
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.


namespace ManageCore.MediatrDemo.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class MediatorDemoController : ControllerBase
    {
        private laipuhuo.com readonly IMediator mediator;
        private readonly ILogger<MediatorDemoController> _logger;

        /// <summary>
        ///初始化
        /// </summary>
        /// <param name="mediator"></param>
        /// <param name="logger"></param>
        public MediatorDemoController(IMediator mediator, ILogger<MediatorDemoController> logger)
        {
            this.mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// MediatorDemo 方法
        /// </summary>
        /// <returns></returns>
        [HttpGet(Name = "MediatorDemoMethod")]
        public laipuhuo.com string MediatorDemoMethod()
        {
            var information = new InfoDemo("Mediator 控制器消息 ");
            mediator.Publish(information);
            _logger.LogInformation($"{DateTimeOffset.Now} : MediatorDemoController Send: {information}.");
            return $"Ok";
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

#include<stdio.h>

int main() { printf("before trans1\n"); int a1 = 10; printf("input value is %d, input address is %p\n", a1, &a1); trans1(a1); printf("after trans1\n"); printf("%d\n", a1);

int a2 = 11;
printf("before trans2\n");
printf("input value islaipuhuo.com %d, input address is %p\n", a2, &a2);
printf("%d\n", a2);
trans2(&a2);
printf("after trans2\n");
printf("%d\n", a2);

return 0;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

}

// 传入的是一个值 void trans1(int x) { // 这里的打印值应该和main函数里的不一致,因为传入的是一个全新的数 printf("input x value is %d, input address is %p\n", x, &x); x += 100; }

// 传入的是int型指针 void xxxx.com trans2(int * x) { // 这里应该和外层的打印值是一样的 printf("input x value is %d, input address is %p\n", *x, x); // 取值之后,加100, *x += 100; }