且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-18 21:00:05

 使用 System.ComponentModel; 

TypeConverter typeConverter = TypeDescriptor.GetConverter(propType);
object propValue = typeConverter.ConvertFromString(inputValue);


如果ContactType是枚举,你需要将从控制台读取的字符串值转换为相应的枚举值。



例如,如果ContactType定义如下:



  public   enum  ContactType 
{
Home,
Office
}





用户输入Home,然后你需要将Home(字符串)转换为ContactType.Home(类型/枚举)



您可以通过以下方式执行此操作:



 ContactType contactType =(ContactType)Enum.Parse(typeof(ContactType),contactTypeString); 


I am trying to build a contact managers program in a console application using a list to store and display the data. I need to view a report that displays a summary of contacts available and then have a menu to allow the user to interact with the program. I have a method to create a list with data and a contact object. contact object is all stings except for ContactTypes which is a Type data type. I keep getting the error Cannot implicitly convert type string to System.Type in createContact() method. I am not sure how to fix this.

any guidance would be appreciated

static void Main(string[] args)
        {         
            //Declare the list

            List<Contact> contactList = new List<Contact>();
                       
            //Main Driver
            char menuItem;
             Console.WriteLine("Contact List\n");
            menuItem = GetMenuItem();
            while (menuItem != 'Q')
            {

                ProcessMenuItem(menuItem, contactList);
                menuItem = GetMenuItem();

            }
            Console.WriteLine("\nThank you, goodbye");
            Console.ReadLine();
        }
        //Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller
        static char GetMenuItem()
        {
            char menuItem;
            DisplayMenu();
            menuItem = char.ToUpper(IOConsole.GetChar("\nPlease pick an item: "));
            while (menuItem != 'C'
                && menuItem != 'R' && menuItem != 'Q' && menuItem != 'U' && menuItem != 'D' && menuItem != 'S' && menuItem != 'L' && menuItem != 'F' && menuItem != 'P' && menuItem != 'T')
            {
                Console.WriteLine("\nError - Invalid menu item");
                DisplayMenu();
                menuItem = char.ToUpper(IOConsole.GetChar("\nEnter option or M for menu:"));
            }
            return menuItem;
        }

        static void DisplayMenu()
        {
           Console.WriteLine("C-> Create Contacts");
           Console.WriteLine("R-> Remove Contacts");
           Console.WriteLine("U-> Update Contacts");
           Console.WriteLine("D -> Load data from file");
           Console.WriteLine("S-> Save data to file");
           Console.WriteLine("L-> View sorted by last name");
           Console.WriteLine("F-> View sorted by first name");
           Console.WriteLine("P-> View by partial name search");
           Console.WriteLine("T-> View by contact type");
           Console.WriteLine("Q-> Quit");
        }

        //Routes to the appropriate process routine based on the user menu choice
        static void ProcessMenuItem(Char menuItem, List<Contact> contactList)
        {
            switch (menuItem)
            {
                case 'C':
                    createContact();
                    break;
                case 'R':
                    removeContact(contactList);
                    break;
                case 'U':
                    updateContact(contactList);
                    break;
                case 'D':
                    LoadToFile();
                    break;
                case 'S':
                    saveToFile();
                    break;
                    
                case 'L':
                    sortByLastName(contactList);
                    break;
                case 'F':
                    sortByFirstName(contactList);
                       break;
                case 'P':
                       DisplayList(contactList);
                       break;
                case 'T':
                       sortByContactType();
                       break;
                case 'Q':
                       
                       break;

            }                   
        }

         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);
                Console.WriteLine(c.ContactTypes);
               
            }

            Console.ReadLine();

        }

using System.ComponentModel;

TypeConverter typeConverter = TypeDescriptor.GetConverter(propType);
object propValue = typeConverter.ConvertFromString(inputValue);


If ContactType is an enum, you need to convert the string value you read from console to corresponding enum value.

For example, if ContactType is defined like:

public enum ContactType
{
Home,
Office
}



And user enters "Home", then you need co convert "Home" (string) to ContactType.Home (Type/enum)

You can do this the following way:

ContactType contactType = (ContactType) Enum.Parse(typeof(ContactType), contactTypeString);