Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 一种接受两个不同对象的方法_C#_Asp.net Mvc - Fatal编程技术网

C# 一种接受两个不同对象的方法

C# 一种接受两个不同对象的方法,c#,asp.net-mvc,C#,Asp.net Mvc,我有两种方法,一种是签入内部用户,另一种是签入外部用户。除了对象之外,方法是相同的(参见代码)。我想知道是否有一个方法可以同时接受两个对象。我想传递一个参数,说明它是内部的还是外部的,并基于此调用相应的对象并保存它。不确定这是否可能 public JsonResult CheckInInternal(int ID) { var e = EventInternal.Get(ID, EventInternal.FetchType.ID); if (e.ID == 0) {

我有两种方法,一种是签入内部用户,另一种是签入外部用户。除了对象之外,方法是相同的(参见代码)。我想知道是否有一个方法可以同时接受两个对象。我想传递一个参数,说明它是内部的还是外部的,并基于此调用相应的对象并保存它。不确定这是否可能

public JsonResult CheckInInternal(int ID)
{
    var e = EventInternal.Get(ID, EventInternal.FetchType.ID);

    if (e.ID == 0)
    {
        throw new Exception("Registration ID not found.");
    }
    if (DateTime.Now.Date > e.EventDetail.StartTime.Date)
    {
        throw new Exception("Check-in has been closed for this class!");
    }
    e.CheckedIn = true;
    e.Save();
    return Json(new { success = true, message = "Success!" });
}

public JsonResult CheckInExternal(int ID)
{
    var e = EventExternal.Get(ID, EventExternal.FetchType.ID);

    if (e.ID == 0)
    {
        throw new Exception("Registration ID not found.");
    }
    if (DateTime.Now.Date > e.EventDetail.StartTime.Date)
    {
        throw new Exception("Check-in has been closed for this class!");
    }
    e.CheckedIn = true;
    e.Save();
    return Json(new { success = true, message = "Success!" });
}

不是说这是最好的方法,但你可以使用


但正如一些评论者所说,
接口
泛型
是最好的解决方案。取决于您的场景。

什么是
EventInternal
EventExternal
?请查看泛型。是的,您可以使用一个方法来处理这两种类型。。您的参数将是
整数和枚举
泛型和枚举。我认为有一个接口来保存事件外部/内部是很好的。让方法接收接口,您就可以看到一些示例代码了吗?最好的方法是什么?
public enum CallType
{
 Internal, External
}
public JsonResult CheckInInternalOrExternal(int ID, CallType type)
    {
         object e = type == CallType.Internal? EventInternal.Get(ID, EventInternal.FetchType.ID) as object : EventExternal.Get(ID, EventExternal.FetchType.ID) as object;

         var idProperty = e.GetType().GetProperty("ID");
         var idValue = Convert.ToInt32(IdProperty.GetValue(e));

         if (idValue == 0)
            {
                throw new Exception("Registration ID not found.");
            }
         if (DateTime.Now.Date > e.EventDetail.StartTime.Date)
            {
                throw new Exception("Check-in has been closed for this class!");
            }

         var checkedInProperty = e.GetType().GetProperty("CheckedIn");
         checkedInProperty.SetValue(e, true);

         var saveMethod = e.GetType().GetMethod("Save");
         saveMethod.Invoke(e);

         return Json(new { success = true, message = "Success!" });
    }