1

我有多个父类“command_functions”

例子

class Empty_Command : command_functions

每个命令类都覆盖一个值

public override string command_display_name { get { return "Empty"; } }

无论如何要搜索 command_functions 的类型,以查找将 command_display_name 设置为匹配字符串的位置并返回它。

所以我可以像这样使用它

command_functions find = FindCommand("Empty");
if(find != null)
{
    new find();
}
4

4 回答 4

2

使用泛型可以做到这一点。据我所知,您有一组从类 Empty_Command 继承的类(我假设是抽象的),并且您想根据命令名称查找要执行的特定类。

我创建了以下示例,它假设所有继承的类型都在同一个程序集中。如果它们跨多个程序集,只是您的负载不同,这没问题。

public abstract class Empty_Command
{
    /// <summary>
    /// Find command
    /// </summary>
    /// <param name="commandName">the command name</param>
    /// <returns></returns>
    public static Empty_Command FindCommand(string commandName)
    {
        //get all the types that are inherited from the Empty_Command class and are not abstract (skips empty commad)
        var types = Assembly.GetExecutingAssembly().GetTypes().Where(x => typeof(Empty_Command).IsAssignableFrom(x) && !x.IsAbstract);
        //enuerate all types
        foreach (var type in types)
        {
            //create an instance of empty command from the type
            var item = Activator.CreateInstance(type) as Empty_Command;
            if (item == null)
                continue;
            //test the display name
            if(item.command_display_name.Equals(commandName))
                return item;
        }
        return null;
    }
    public abstract string command_display_name { get; }
}

我注释了一些代码以提供帮助。但这是我的完整测试存根。

using System;
using System.Linq;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var cmd = Empty_Command.FindCommand("command_2");
            if (cmd != null)
                Console.WriteLine(cmd.command_display_name);
            Console.ReadKey();


        }
    }

    public abstract class Empty_Command
    {
        /// <summary>
        /// Find command
        /// </summary>
        /// <param name="commandName">the command name</param>
        /// <returns></returns>
        public static Empty_Command FindCommand(string commandName)
        {
            //get all the types that are inherited from the Empty_Command class and are not abstract (skips empty commad)
            var types = Assembly.GetExecutingAssembly().GetTypes().Where(x => typeof(Empty_Command).IsAssignableFrom(x) && !x.IsAbstract);
            //enuerate all types
            foreach (var type in types)
            {
                //create an instance of empty command from the type
                var item = Activator.CreateInstance(type) as Empty_Command;
                if (item == null)
                    continue;
                //test the display name
                if(item.command_display_name.Equals(commandName))
                    return item;
            }
            return null;
        }
        public abstract string command_display_name { get; }
    }

    public class Command1 : Empty_Command
    {
        public override string command_display_name
        {
            get { return "command_1"; }
        }
    }

    public class Command2 : Empty_Command
    {
        public override string command_display_name
        {
            get { return "command_2"; }
        }
    }

    public class Command3 : Empty_Command
    {
        public override string command_display_name
        {
            get { return "command_3"; }
        }
    }
}

希望这可以帮助...

于 2013-09-19T10:33:35.757 回答
1

要获取非静态属性的值,您需要实例化类型,因此第一个条件是您可以实例化您希望检查的每种类型。

否则就是使用反射来获取类型列表、按基类型过滤、实例化和调用属性获取方法的问题。

于 2013-09-19T10:22:19.993 回答
0

您可以使用以下模式来做到这一点

interface IGroup2
        {
            string SearchOption { get; set; }
            List<Projects> GetProjects();
        }

        public class GroupHead : IGroup2
        {
            public string SearchOption { get; set; }

            public GroupHead()
            {
                SearchOption = "GroupHead";
            }

            public List<Projects> GetProjects()
            {   
                //Code here
                return null;
            }
        }

        public class ProjectIncharge : IGroup2
        {
            public string SearchOption { get; set; }

            public ProjectIncharge()
            {
                SearchOption = "ProjectIncharge";
            }

            public List<Projects> GetProjects()
            {
                //Code here
                return null;
            }
        }

        public class ProjectManager : IGroup2
        {
            public string SearchOption { get; set; }

            public ProjectManager()
            {
                SearchOption = "ProjectManager";
            }

            public List<Projects> GetProjects()
            {
                //Code here
                return null;
            }
        }

public class Test
{
        private static List<IGroup2> searchRuleList = new List<IGroup2>()
        {
          new GroupHead(),
          new ProjectIncharge(),
          new ProjectManager(),
        };

        public static void Main(string[] args)
        {
         IGroup2 searchOptionRule = searchRuleList.Find(delegate(IGroup2 searchRule) { return searchRule.SearchOption.Equals(args[0]); });

        } 
}
于 2013-09-19T10:53:52.663 回答
0

您正在寻找工厂设计模式。(http://en.wikipedia.org/wiki/Factory_method_pattern

在这个工厂中,您可以创建一个类似的函数

command_functions findCommand(string commandText)
{
  if (commandText == "empty") return new Empty_Command();
  if (...) etc
}
于 2013-09-19T10:21:32.067 回答