无法将类型'string'隐式转换为'System.Type'

时间:2015-07-28 05:46:58

标签: c# .net enums

我正在尝试使用列表来存储和显示数据,从而在控制台应用程序中构建联系人管理器程序。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与该程序进行交互。我有一个方法来创建一个包含数据和联系对象的列表,但我不断收到错误不能在我的createContact()方法中隐式地将类型'string'转换为'System.Type'。我不知道如何解决这个问题。

任何指导都将不胜感激

     public static void createContact()
    {
        Contact c1 = new Contact();
        Console.WriteLine("\nGetFirstName");
        c1.GetFirstName = Console.ReadLine();
        Console.WriteLine("\nGetLastName");
        c1.GetLastName = Console.ReadLine();
        Console.WriteLine("\nGetEmailAddress");
        c1.GetEmailAddress = Console.ReadLine();
        Console.WriteLine("\nGetPhoneNumber");
        c1.GetPhoneNumber = Console.ReadLine();
        Console.WriteLine("\nContactTypes");
        c1.ContactTypes = Console.ReadLine();

        //Create more contacts...

        //Add all contacts here
        ContactCollection contactList = new ContactCollection();
        contactList.Add(c1);

        //Loop through list
        foreach (Contact c in contactList)
        {
            Console.WriteLine(c.GetFirstName);
            Console.WriteLine(c.GetLastName);
            Console.WriteLine(c.GetEmailAddress);
            Console.WriteLine(c.GetPhoneNumber);
           // error line
            Console.WriteLine(c.ContactTypes);

        }

        Console.ReadLine();

    }
  

这是我的联系班级

class Contact
{

    //private member variables
    private String _firstName;
    private String _lastName;
    private Type _contactTypes;
    private String _phoneNumber;
    private String _emailAddress;




    //Public constructor that takes five arguments
    public Contact()
    {
        //Call the appropriate setter (e.g. FirstName) to set the member variable value
        /*GetFirstName = firstName;
        GetLastName = lastName;
        ContactTypes = contactTypes;
        GetPhoneNumber = phoneNumber;
        GetEmailAddress = emailAddress;*/

    }


    /*********************************************************************
     * Public accessors used to get and set private member variable values
     *********************************************************************/
    //Public  ContactTypes accessor
    public Type ContactTypes
    {
        get
        {
            //Return member variable value
            return _contactTypes;
        }
        set
        {
              //Validate value and throw exception if necessary
            if (value == null)
                throw new Exception("ContactType must have a value");
            else
                //Otherwise set member variable value*/
                _contactTypes = value;
        }
    }
    enum ContactTypesEnum { Family, Friend, Professional }
    //Public FirstName accessor: Pascal casing
    public String GetFirstName
    {
        get
        {
            //Return member variable value
            return _firstName;
        }
        set
        {
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("First name must have a value");
            else
                //Otherwise set member variable value
                _firstName = value;
        }
    }

    //Public LastName accessor: Pascal casing
    public String GetLastName
    {
        get
        {
            //Return member variable value
            return _lastName;
        }
        set
        {
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("Last name must have a value");
            else
                //Otherwise set member variable value
                _lastName = value;
        }
    }



    //Public PhoneNumber accessor
    public String GetPhoneNumber
    {
        get
        {
            //Return member variable value
            return _phoneNumber;
        }
        set
        {
            bool isValid = Regex.IsMatch(value, @"/d{3}-/d{3}-/d{4}"); 
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("PhoneNumber must have a value");
            else
                //Otherwise set member variable value
                _phoneNumber = value;
        }
    }



    //Public Email accessor
    public String GetEmailAddress
    {
        get
        {
            //Return member variable value
            return _emailAddress;
        }
        set
        {
            //Validate value and throw exception if necessary
            if (value == "")
                throw new Exception("EmailAddress must have a value");
            else
                //Otherwise set member variable value
                _emailAddress = value;
        }
    }

}

4 个答案:

答案 0 :(得分:4)

确保您的联系人类属性(GetFirstName,GetLastName等)是字符串, 或将输入(Console.ReadLine())值转换为所需类型。

答案 1 :(得分:3)

Console.ReadLine()只返回一个字符串。如果要创建任何对象,即:ContactTypes,则应使用该字符串创建其实例。

ContactTypes应该是枚举而不是类型。

public Type ContactTypes应为public ContactTypes ContactTypes

enum ContactTypesEnum { Family, Friend, Professional }应为enum ContactTypes { Family, Friend, Professional }

您应该执行以下操作。最后一个参数是ignoreCare,我将其设置为true

c1.ContactTypes = (ContactTypes) Enum.Parse(typeof(ContactTypes ), Console.ReadLine(), true);

答案 2 :(得分:1)

我认为你错过了输入联系人课程

也许

public Type ContactTypes

应该是这样的:

public ContactTypesEnum Type 

并且

private Type _contactTypes;
像这样:

private ContactTypesEnum _contactTypes;

答案 3 :(得分:0)

Console.ReadLine()返回一个字符串。 你想要的是从Readline()返回的类型。

c1.ContactTypes = Console.ReadLine().GetType();

但这没有任何意义,因为Console.Readline将永远是“Sytem.String”。您可能必须将返回的值(字符串)转换为您喜欢的对象。

var currString = Console.ReadLine().GetType();
object currObject = currString;
if(//Check if numeric for ex.)
{
    currObject = Convert.ToInt32(currString);     
}
//Do some more validation
//Now getType()
c1.ContactTypes = Console.ReadLine();

另见CharithJ和Chandrashekar Jupalli回答