C# 枚举处的源代码问题
目标:C# 枚举处的源代码问题,c#,C#,目标: 将枚举值指定给animalType对象 Categories.CategorityType动物类型= (CategoryType)Enum.Parse(GetType(CategoryType), 类别) 问题: 源代码工作不正常,它给我两条错误消息: “Assignment1.Categories.CategorityType”是一个“类型”,但其用法类似于 “变量” “没有方法'GetType'的重载包含1个参数” 名称空间分配1 { 类AnimalManager { 私有列表_my
将枚举值指定给animalType对象 Categories.CategorityType动物类型= (CategoryType)Enum.Parse(GetType(CategoryType), 类别) 问题: 源代码工作不正常,它给我两条错误消息: “Assignment1.Categories.CategorityType”是一个“类型”,但其用法类似于 “变量” “没有方法'GetType'的重载包含1个参数”
名称空间分配1
{
类AnimalManager
{
私有列表_myAnimal=新列表();
public void CreateNewAnimal(字符串pName、字符串pHousing、字符串pAge、字符串pCategory、字符串pAnimal、字符串pEater、字符串pGender)
{
Categories.categoritytype animalType=(categoritytype)Enum.Parse(GetType(categoritytype),pCategory);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间分配1.类别
{
///
///
///
公共枚举类别类型
{
哺乳动物,
鸟,
海军陆战队,
爬行动物,
虫子
}
}
试试这个:
Categories.CategoryType animalType =
(CategoryType)Enum.Parse(typeof(CategoryType), pCategory);
试试这个:
Categories.CategoryType animalType =
(CategoryType)Enum.Parse(typeof(CategoryType), pCategory);
尝试:
当您需要静态地(在编译时)知道的类型时,请使用typeof
操作符。GetType
方法用于多态情况下的运行时类型信息(在这种情况下,您可以执行类似于pCategory.GetType
)。最后请注意,Enum.Parse
可以抛出-因此您可能想看看。试试:
当您需要静态地(在编译时)知道的类型时,请使用typeof
操作符。GetType
方法用于多态情况下的运行时类型信息(在这种情况下,您可以执行类似于pCategory.GetType
)。最后请注意,Enum.Parse
可以抛出-所以您可能想看看
Categories.CategoryType animalType =
(CategoryType)Enum.Parse(typeof(CategoryType), pCategory);
CategoryType animalType =
(CategoryType)Enum.Parse(typeof(CategoryType), pCategory);