无法从接口转换为类

时间:2017-03-07 15:53:26

标签: c# class interface

当我尝试将数据库添加到我的界面时,我的方法出错了,它给了我错误,

  

参数1:无法从'ForumSite.ActionsAndMethods.Registration.IRegistration'转换为'ForumSite.Models.User'。

以下是IRegistration中的代码:

 using ForumSite.Models;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

namespace ForumSite.ActionsAndMethods.Registration
{
public interface IRegistration
{
    int UserId { get; }
    string Email { get; }
    string Password { get; }
    string FirstName { get; }
    string LastName { get; }
    DateTime Birthday { get; }
    DateTime DateCreated { get; set; }
    string MobileNumber { get; }
    string Address { get; }
    int UserIsDeleted { get; set; }
    int UserRoleId { get; set; }
    UserRole UserRole { get; }
}
}

这是我模型中的代码:

namespace ForumSite.Models
{
using ActionsAndMethods.Registration;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public partial class User : IRegistration
{
    public int UserId { get; set; }
    [Display(Name = "Email Address")]
    [Required(ErrorMessage = "This field required.")]
    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@([a-zA-Z]+)\.([a-zA-Z]{2,5})$", ErrorMessage = "Enter Valid Email Address")]
    [StringLength(50, MinimumLength = 8, ErrorMessage = "8 to 50 characters only")]
    public string Email { get; set; }

    [Display(Name = "Password")]
    [Required(ErrorMessage = "This field required.")]
    [RegularExpression(@"^[a-zA-Z0-9]+$", ErrorMessage = "Alphanumeric characters only")]
    [StringLength(50, MinimumLength = 8, ErrorMessage = "8 to 50 characters only")]
    public string Password { get; set; }

    [Display(Name = "Confirm Password")]
    [Required(ErrorMessage = "This field required.")]
    [RegularExpression(@"^[a-zA-Z0-9]+$", ErrorMessage = "Alphanumeric characters only")]
    [StringLength(50, MinimumLength = 8, ErrorMessage = "8 to 50 characters only")]
    public string PasswordConfirm { get; set; }

    [Display(Name = "First Name")]
    [Required(ErrorMessage = "This field required.")]
    [RegularExpression(@"^[a-zA-Z\s]+$", ErrorMessage = "Letters Only.")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    [Required(ErrorMessage = "This field required.")]
    [RegularExpression(@"^[a-zA-Z\s]+$", ErrorMessage = "Letters Only.")]
    public string LastName { get; set; }

    [Display(Name = "Birthday")]
    [Required(ErrorMessage = "This field required.")]
    public DateTime Birthday { get; set; }
    public DateTime DateCreated { get; set; }

    [Display(Name = "Mobile Number")]
    [Required(ErrorMessage = "This field required.")]
    [RegularExpression(@"^[0-9]+$", ErrorMessage = "Numeric input only.")]
    public string MobileNumber { get; set; }

    [Display(Name = "Address")]
    [Required(ErrorMessage = "This field required.")]
    public string Address { get; set; }
    public int UserIsDeleted { get; set; }
    public int UserRoleId { get; set; }

    public UserRole UserRole { get; set; }
}
}

最后,我的方法将用户添加到我的数据库中:

using ForumSite.ActionsAndMethods.Registration;
using ForumSite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ForumSite.ActionsAndMethods
{
public class RegisterAction : IRegistration
{
    ForumDBEntities ent = new ForumDBEntities();
    public void Registration(IRegistration reg)
    {
        reg.DateCreated = DateTime.Now;
        reg.UserRoleId = 1;
        reg.UserIsDeleted = 0;

        ent.Users.Add(reg);
        ent.SaveChanges();
    }

    string IRegistration.Address { get; }
    int IRegistration.UserId { get; }
    string IRegistration.Email { get; }
    string IRegistration.Password { get; }
    string IRegistration.FirstName { get; }
    string IRegistration.LastName { get; }
    DateTime IRegistration.Birthday { get; }
    DateTime IRegistration.DateCreated { get; set; }
    string IRegistration.MobileNumber { get; }
    int IRegistration.UserIsDeleted { get; set; }
    int IRegistration.UserRoleId { get; set; }

    UserRole IRegistration.UserRole { get; }
}

}

我想知道导致此错误的原因是什么?

2 个答案:

答案 0 :(得分:4)

虽然每个User实施IRegistration,但不是每个IRegistration实例都是User

例如,您的RegisterAction 实施IRegistration。因此,如果您想要做的事情是可能的,理论上您可以使用此代码:

RegisterAction action = GetRegisterAction();

RegisterAction action2 = new RegisterAction(action);

这意味着你什么时候来做:

ent.Users.Add(reg);

它会失败,因为reg不是User

你可以这样解决:

var user = reg as User;
if(user != null)
{
    ent.Users.Add(user);
}

但实际上你应该首先看看你的结构,因为这样做似乎很奇怪。

答案 1 :(得分:0)

您还没有告诉您编辑错误的确切位置,但我的猜测是它在ent.Users.Add(reg)方法的Registration行中。

要解决编译错误,您应该将Registration方法的参数从IRegistration更改为User

public void Registration(User reg)
{
    reg.DateCreated = DateTime.Now;
    reg.UserRoleId = 1;
    reg.UserIsDeleted = 0;

    ent.Users.Add(reg);
    ent.SaveChanges();
}

但是,如果您尝试将IRegistration而不是User传递给Registration方法,则可能会在调用链中引发另一个编译错误。

虽然User可以(并且确实)实施IRegistration,但您无法假设实施IRegistration的任何内容都是User。如果您的代码基于该假设,则必须解决该问题以解决您的问题。