C# - 通过相同的方法传递不同类型的对象

ini*_*tar 1 .net c#

原始问题

所以我有这三个对象......

public class obj1
{
  public int Id { get; set; }
  public string Name { get; set; }
}

public class obj2
{
  public int AccNum { get; set; }
  public string Name { get; set; }
}

public class obj3
{
  public string Email { get; set; }
  public string Phone { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

...和一个应该接收其中一个的方法,在评估对象类型后,程序应该决定调用哪个函数.

我尝试过泛型,但它不能像我预期的那样工作.到目前为止,这就是我所拥有的......

    public class NotificationHelper: INotificationHelper
    {

        public bool SendNotification<TNotInfo>(TNotInfo obj) where TNotInfo : class
        {
            if (contract.GetType() == typeof (obj1))
            {
                var sender = new SendSMS();
                return sender.Send(obj);
            }
            if (contract.GetType() == typeof(obj2))
            {
                var sender = new SendPush();
                return sender.Send(obj);
            }
            else
            {
                var sender = new SendEmail();
                return sender.Send(obj);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

但我收到错误"无法从TNotInfo转换为Models.obj1".有没有办法克服这个问题?或者我必须改变我的逻辑?

感谢任何帮助,提前感谢.

*编辑

using System;

namespace EmailNotifications
{

    public interface IEmailNotification
    {
        void SendEmailNotification();
    }

    public class EmailNotificationA : IEmailNotification
    {

        public void SendEmailNotification(Contract1 a)
        {
            Console.WriteLine($"Sending EmailNotificationA ({a})");
        }
    }

    public class EmailNotificationB : IEmailNotification
    {
        public void SendEmailNotification(Contract2 b)
        {
            Console.WriteLine($"Sending EmailNotificationB ({b})");
        }
    }

    public class EmailNotificationC : IEmailNotification
    {

        public void SendEmailNotification(Contrac3 c)
        {
            Console.WriteLine($"Sending EmailNotificationC ({c})");
        }
    }

    public class EmailNotificationService
    {
        private readonly IEmailNotification _emailNotification;

        public EmailNotificationService(IEmailNotification emailNotification)
        {
            this._emailNotification = emailNotification;
        }

        public void ServiceHelper()
        {
            _emailNotification.SendEmailNotification();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以上解决方案是我尝试实现的策略设计模式.但我无法设法使我的接口方法接收不同的对象,这是必需的,因为每个通知都有自己的实现.如上面没有工作的例子所示,我有3种不同的同一方法的实现,它们都接收不同的对象.知道如何使这个逻辑工作?

Abi*_*n47 14

这是接口设计用来做的事情.首先,定义一个通用接口:

public interface INotifier
{
    bool Notify();
}
Run Code Online (Sandbox Code Playgroud)

其次,在你的objX课程中实现它:

public class obj1 : INotifier
{
    public int Id { get; set; }
    public string Name { get; set; }

    public bool Notify()
    {
        var sender = new SendSMS();
        return sender.Send(this);
    }
}

public class obj2 : INotifier
{
    public int AccNum { get; set; }
    public string Name { get; set; }

    public bool Notify()
    {
        var sender = new SendPush();
        return sender.Send(this);
    }
}

public class obj3 : INotifier
{
    public string Email { get; set; }
    public string Phone { get; set; }

    public bool Notify()
    {
        var sender = new SendEmail();
        return sender.Send(this);
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,更改您的通知方法以接受接口类型作为参数:

public class NotificationHelper : INotificationHelper
{
    public bool SendNotification(INotifier obj)
    {
        return obj.Notify();
    }
}
Run Code Online (Sandbox Code Playgroud)


Fab*_*bio 5

另一种方法是重载方法。
因为您根据给定的类型有不同的逻辑。和类型没有任何共同点(接口/抽象类)。

public class NotificationHelper
{
    public bool SendNotification(obj1 obj)
    {
        var sender = new SendSMS();
        return sender.Send(obj);
    }

    public bool SendNotification(obj2 obj)
    {
        var sender = new SendPush();
        return sender.Send(obj);
    }

    public bool SendNotification(obj3 obj)
    {
        var sender = new SendEmail();
        return sender.Send(obj);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后使用就足够清楚了

var someObject = GetObjectFromSomeWhere();
var isSuccessful = SendNotification(someObject);
Run Code Online (Sandbox Code Playgroud)

  • 这种方法违反了开闭原则。每一种新形式的对象都需要一个新的重载方法。我的答案最好使用一些冗余,以便仅在相关类中保持所有代码的简洁和独立。 (2认同)