GenoTipController必须根据枚举类型生成类。我有3个班级:_公司,_Muayene,_Radyoloji。我还有CompanyView类GetPersonel方法。如果你看GenoTipController,我的代码需要重构。你能听懂我说话吗?我需要一个根据ewnum类的类必须我生产类。例如; case DataModelType.Radyoloji它必须返回radyoloji = new Radyoloji。一切都必须是一个开关盒?
public abstract class _Company
{
public abstract List<Personel> GetPersonel();
public abstract List<Prim> GetPrim();
public abstract List<Finans> GetFinans();
}
public abstract class _Radyoloji
{
public abstract List<string> GetRadyoloji();
}
public abstract class _Satis
{
public abstract List<string> GetSatis();
}
public abstract class _Muayene
{
public abstract List<string> GetMuayene();
}
public class Company: _Company
{
public override List<Personel> GetPersonel()
{
throw new NotImplementedException();
}
public override List<Prim> GetPrim()
{
throw new NotImplementedException();
}
public override List<Finans> GetFinans()
{
throw new NotImplementedException();
}
}
public class Radyoloji : _Radyoloji
{
public override List<string> GetRadyoloji()
{
throw new NotImplementedException();
}
}
public class Satis : _Satis
{
public override List<string> GetSatis()
{
throw new NotImplementedException();
}
}
public class Muayene : _Muayene
{
public override List<string> GetMuayene()
{
throw new NotImplementedException();
}
}
public class GenoTipController
{
public _Company GenerateCompany(DataModelType modeltype)
{
_Company company = null;
switch (modeltype)
{
case DataModelType.Radyoloji:
break;
case DataModelType.Satis:
break;
case DataModelType.Muayene:
break;
case DataModelType.Company:
company = new Company();
break;
default:
break;
}
return company;
}
public _Muayene GenerateMuayene(DataModelType modeltype)
{
_Muayene muayene = null;
switch (modeltype)
{
case DataModelType.Radyoloji:
break;
case DataModelType.Satis:
break;
case DataModelType.Muayene:
muayene = new Muayene();
break;
case DataModelType.Company:
break;
default:
break;
}
return muayene;
}
public _Radyoloji GenerateRadyoloji(DataModelType modeltype)
{
_Radyoloji radyoloji = null;
switch (modeltype)
{
case DataModelType.Radyoloji:
radyoloji = new Radyoloji();
break;
case DataModelType.Satis:
break;
case DataModelType.Muayene:
break;
case DataModelType.Company:
break;
default:
break;
}
return radyoloji;
}
}
public class CompanyView
{
public static List<Personel> GetPersonel()
{
GenoTipController controller = new GenoTipController();
_Company company = controller.GenerateCompany(DataModelType.Company);
return company.GetPersonel();
}
}
public enum DataModelType
{
Radyoloji,
Satis,
Muayene,
Company
}
}
public class GenoTipController
{
public object CreateByEnum(DataModelType modeltype)
{
string enumText = modeltype.ToString(); // will return for example "Company"
Type classType = Type.GetType(enumText); // the Type for Company class
object t = Activator.CreateInstance(classType); // create an instance of Company class
return t;
}
}
public class CompanyView
{
public static List<Personel> GetPersonel()
{
GenoTipController controller = new GenoTipController();
_Company company = controller.CreateByEnum(DataModelType.Company);
return company.GetPersonel();
}
}
public enum DataModelType
{
Radyoloji,
Satis,
Muayene,
Company
}
alt text http://i40.tinypic.com/14sdqpw.png
答案 0 :(得分:1)
查看factory pattern,我认为您正在寻找。
示例代码。
public interface IPizza
{
decimal Price { get; }
}
public class HamAndMushroomPizza : IPizza
{
decimal IPizza.Price
{
get
{
return 8.5m;
}
}
}
public class DeluxePizza : IPizza
{
decimal IPizza.Price
{
get
{
return 10.5m;
}
}
}
public class HawaiianPizza : IPizza
{
decimal IPizza.Price
{
get
{
return 11.5m;
}
}
}
public class PizzaFactory
{
public enum PizzaType
{
HamMushroom,
Deluxe,
Hawaiian
}
public static IPizza CreatePizza(PizzaType pizzaType)
{
IPizza ret = null;
switch (pizzaType)
{
case PizzaType.HamMushroom:
ret = new HamAndMushroomPizza();
break;
case PizzaType.Deluxe:
ret = new DeluxePizza();
break;
case PizzaType.Hawaiian:
ret = new HawaiianPizza();
break;
default:
throw new ArgumentException("The pizza type " + pizzaType + " is not recognized.");
}
return ret;
}
}
public class PizzaLover
{
public static void Main(string[] args)
{
Dictionary<PizzaFactory.PizzaType, IPizza> pizzas = new Dictionary<PizzaFactory.PizzaType, IPizza>();
foreach (PizzaFactory.PizzaType pizzaType in Enum.GetValues(typeof(PizzaFactory.PizzaType)))
{
pizzas.Add(pizzaType, PizzaFactory.CreatePizza(pizzaType));
}
foreach (PizzaFactory.PizzaType pizzaType in pizzas.Keys)
{
System.Console.WriteLine("Price of {0} is {1}", pizzaType, pizzas[pizzaType].Price);
}
}
}
Output:
Price of HamMushroom is 8.5
Price of Deluxe is 10.5
Price of Hawaiian is 11.5
答案 1 :(得分:0)
您可以对枚举名称和类名称使用命名约定,并通过反射创建实例:
namespace MyNamespace
{
public enum DataModelType { Company, Person }
public class Company
{
}
public class Person
{
}
public class ObjectFactory
{
public static object CreateByEnum(DataModelType modelType)
{
string enumText = modelType.ToString(); // will return for example "Company"
Type classType = Type.GetType("MyNamespace." + enumText); // the Type for Company class
object t = Activator.CreateInstance(classType); // create an instance of Company class
return t;
}
}
}
答案 2 :(得分:0)
使用编辑后描述的方法,需要将对象强制转换为_Company:
_Company company = controller.CreateByEnum(DataModelType.Company) as _Company;
或
_Company company = (_Company)controller.CreateByEnum(DataModelType.Company);
虽然我建议不要使用该方法并建议使用switch语句,因为这样可以避免运行时错误。
也许我从你的例子中看不到它(或者可能是晚上太晚了)但是如果这些项中没有一个继承自相同的基类或实现相同的接口那么你将必须知道你正在使用的类当您实例化它以访问其任何成员时。