当前位置:编程学习 > C#/ASP.NET >>

怎么样动态返回实体?

--------------------编程问答-------------------- 除非你的类都继承一个基类
比如 A:B   C:B
然后返回父类
public B Fa(){
swich(枚举)
case:1
 return new A
case:2
 return new B
} --------------------编程问答-------------------- 可以用字典来映射枚举和类名,然后用反射。 --------------------编程问答--------------------
引用 2 楼 caozhy 的回复:
可以用字典来映射枚举和类名,然后用反射。


比如呢? --------------------编程问答--------------------

Dictionary<string, string> openWith =
          new Dictionary<string, string>();

      // Add some elements to the dictionary. There are no 
      // duplicate keys, but some of the values are duplicates.
      openWith.Add("txt", "notepad.exe");
      openWith.Add("bmp", "paint.exe");
      openWith.Add("dib", "paint.exe");
      openWith.Add("rtf", "wordpad.exe");


像这样使用字典,自己吧string,string 传承enum,object就可以了
使用的时候可以是用openWith[你的枚举]然后做一次强转就可以了
传送门
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,