关于枚举遍历 枚举和方法不在同一个文件中该怎么解决呢

netques 2013-11-30 10:07:27
这是个枚举遍历(方法和枚举是写在同一个文件下的)
class GetEnumValues
{
public static void Main()
{
Type Sexs = typeof(SexKind);
Console.WriteLine("The kinds of the Sex:");
foreach (string s in Enum.GetNames(Sexs))
{
Console.WriteLine(s);
}
}
}

enum SexKind
{
Male,
Female
}

如果把这个遍历方法放在类A中,枚举在类B中 那么这个方法该怎么写呢 又怎么把这个枚举传递过去呢 感谢
...全文
225 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jsnjlhb 2013-11-30
  • 打赏
  • 举报
回复
A

	public class ClassEnum
	{

		public enum SexKind 
		{ 
			Male, 
			Female 
		}

	}


	public static void Main() 
	{ 
				Type Sexs = typeof(ClassEnum.SexKind); 
				Console.WriteLine("The kinds of the Sex:"); 
				foreach (string s in Enum.GetNames(Sexs)) 
				{ 
					Console.WriteLine(s); 
				} 
	} 

youzelin 2013-11-30
  • 打赏
  • 举报
回复
我没搞明白,这个方法和在哪个类里面有关系吗?这个完全可以做成一个静态公共方法随便放到一个 Utils 类里面就可以了呀。

private static string[] GetEnumNames(Type type)
{
    if (type.IsEnum)
    {
        return Enum.GetNames(type);
    }
    throw new ArgumentException("type is not enum");
}
static void Main(string[] args)
{
    foreach (var item in GetEnumNames(typeof(StringComparison)))
    {
        Console.WriteLine(item);
    }
    Console.ReadKey();
}
全栈极简 2013-11-30
  • 打赏
  • 举报
回复
A:
public static void NewMethod(Type t)
        {
            Console.WriteLine("The kinds of the Sex:");
            foreach (string s in Enum.GetNames(t))
            {
                Console.WriteLine(s);
            }
        }
B:
  public enum SexKind
        {
            Male,
            Female
        }
Main:
A.NewMethod(typeof(ConsoleApplication1.B.SexKind));
jsnjlhb 2013-11-30
  • 打赏
  • 举报
回复

//枚举类
public class ClassB
	{
		public ClassB()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}
		public  enum SexKind 
		{ 
			Male, 
			Female 
		}

	}
//遍历类
	public class GetEnumValues 
	{
		public GetEnumValues ()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}

		public static void Main() 
		{ 
			Type Sexs = typeof(SexKind); 
			Console.WriteLine("The kinds of the Sex:"); 
			ClassB mClassEnum=new ClassB (); 
			foreach (string s in mClassEnum.Enum.GetNames(Sexs)) 
			{ 
				Console.WriteLine(s); 
			} 
		} 

	}
devmiao 2013-11-30
  • 打赏
  • 举报
回复
B.SexKind 其余一样。

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧