C# 查找具有特定属性值的类
假设我有这些课程C# 查找具有特定属性值的类,c#,class,C#,Class,假设我有这些课程 class BaseClass { public int Variable {get; set;} } class Class1:BaseClass { public Class1() { Variable=1; } } class Class2:BaseClass { public Class2() { Variable=2; } } 在另一个地方,我想这样做: public BaseC
class BaseClass
{
public int Variable {get; set;}
}
class Class1:BaseClass
{
public Class1()
{
Variable=1;
}
}
class Class2:BaseClass
{
public Class2()
{
Variable=2;
}
}
在另一个地方,我想这样做:
public BaseClass MyMethod(int i)
{
//I want if i is 1 this method returns Class1
//And if i is 2 this method returns Class2.
}
解决方案是使用switch
语句。但是我的命名空间有很多类,使用switch会产生很多代码行。你的评论“但是我的命名空间有很多类,使用switch会产生很多代码行。”告诉我你做错了什么
这是典型的问题,工厂模式很容易解决,在这里使用开关将是最好的解决方案:
switch(num) {
case 1: return new Class1();
case 2: return new Class2();
default: throw new ArgumentException();
}
也许你应该拆分你的命名空间
使用的另一个解决方案有点难看,因为您将丢失编译时检查,即使用Activator:
return (BaseClass)Activator.CreateInstance("AssemblyName", "Class" + num)
根据评论,100个类,必须选择一个
public static class AmazingFactory {
private static IDictionary<int, Type> _num2Type;
private static void InitializeFactory() {
var type = typeof(BaseClass);
// get all subclasses of BaseClass
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p));
foreach(var type in types) {
int numberAtTheEnd = int.Parse(Regex.Match(type.Name, @"\d+$").Value);
_num2Type[numberAtTheEnd] = type;
}
}
public static BaseClass Create(int num) {
if (_num2Type == null)
InitializeFactory();
return (BaseClass)Activator.CreateInstance(_num2Type[num]);
}
}
公共静态类AmazingFactory{
私有静态IDictionary _num2类型;
私有静态void InitializeFactory(){
变量类型=类型(基类);
//获取基类的所有子类
var types=AppDomain.CurrentDomain.GetAssemblys()
.SelectMany(s=>s.GetTypes())
其中(p=>type.IsAssignableFrom(p));
foreach(类型中的变量类型){
int numberAtTheEnd=int.Parse(Regex.Match(type.Name,@“\d+$”).Value);
_num2Type[numberAtTheEnd]=类型;
}
}
公共静态基类创建(int num){
如果(_num2Type==null)
初始化属性();
return(BaseClass)Activator.CreateInstance(_num2Type[num]);
}
}
您的评论“但是我的命名空间有很多类,使用switch会产生很多代码行。”告诉我您做错了什么
这是典型的问题,工厂模式很容易解决,在这里使用开关将是最好的解决方案:
switch(num) {
case 1: return new Class1();
case 2: return new Class2();
default: throw new ArgumentException();
}
也许你应该拆分你的命名空间
使用的另一个解决方案有点难看,因为您将丢失编译时检查,即使用Activator:
return (BaseClass)Activator.CreateInstance("AssemblyName", "Class" + num)
根据评论,100个类,必须选择一个
public static class AmazingFactory {
private static IDictionary<int, Type> _num2Type;
private static void InitializeFactory() {
var type = typeof(BaseClass);
// get all subclasses of BaseClass
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p));
foreach(var type in types) {
int numberAtTheEnd = int.Parse(Regex.Match(type.Name, @"\d+$").Value);
_num2Type[numberAtTheEnd] = type;
}
}
public static BaseClass Create(int num) {
if (_num2Type == null)
InitializeFactory();
return (BaseClass)Activator.CreateInstance(_num2Type[num]);
}
}
公共静态类AmazingFactory{
私有静态IDictionary _num2类型;
私有静态void InitializeFactory(){
变量类型=类型(基类);
//获取基类的所有子类
var types=AppDomain.CurrentDomain.GetAssemblys()
.SelectMany(s=>s.GetTypes())
其中(p=>type.IsAssignableFrom(p));
foreach(类型中的变量类型){
int numberAtTheEnd=int.Parse(Regex.Match(type.Name,@“\d+$”).Value);
_num2Type[numberAtTheEnd]=类型;
}
}
公共静态基类创建(int num){
如果(_num2Type==null)
初始化属性();
return(BaseClass)Activator.CreateInstance(_num2Type[num]);
}
}
看起来您不想返回类,而是想返回类的实例。下一个问题是您想从哪里获取对象?如果它们存储在某个集合中,如
items = new List<BaseClass>();
items.add(new Class1());
items.add(new Class2());
如果您想在每次调用
MyMethod
时创建一个新实例,则必须使用switch\If
(或使用Reflection
,但这不是推荐的方法)看起来您不想返回类,而是返回类的实例。下一个问题是您想从哪里获取对象?如果它们存储在某个集合中,如
items = new List<BaseClass>();
items.add(new Class1());
items.add(new Class2());
如果您想在每次调用
MyMethod
时创建一个新实例,那么您必须使用switch\If
(或者使用Reflection
,但这不是一种推荐的方法)您可以使用Reflection执行此操作,但这将需要大量的工作,因为您必须解析方法体,并且可能容易出错。您考虑使用自定义属性吗?类似于[Identifier(1)]
这将很容易实现。您是否需要每次创建新类,或者您可能会多次使用现有类?您可以通过反射来实现这一点,但这将需要大量工作,因为您必须解析方法体,并且可能容易出错。您考虑使用自定义属性吗?像[Identifier(1)]
那样很容易实现。您需要每次创建新类,还是可以多次使用现有类?我有大约100个类,每次编写新类时,我都必须编辑switch语句。这是我的问题。@穆罕默德-在你的情况下,也许你应该考虑使用DI容器像Castle.WindsorI有大约100类,每次我写一个新的类我必须编辑开关语句。这是我的问题。“穆罕默德-在你的情况下,也许你应该考虑使用DI容器像城堡。温莎。