当我不想知道子类的名称时创建子类的新实例

发布于 2024-11-03 13:30:40 字数 2331 浏览 1 评论 0原文

(我对 C# 还很陌生,所以请耐心等待,2 天前开始)

好的,这是一般结构和想法,

namespace Tokenize{
    abstract class Token{
        public Base(object id){
            this.id = id;
        }
        private object id;
        public object Id
        { 
            get{...};
            set{...};
        }
        public abstract object PerformOperation(object a, object b);

    }

    class NumberNine: Base{
        public NumberNine(int num) : base(num){ }
        public override int PerformOperation(int a, int b);
    }

    class LetterT: Base{
        public LetterT(char letter) : base(letter){ }
        public override string PerformOperation(string a, char b);
    }
}
using Tokenize;
namespace Program{
    class MyProg{
         static void Main(string[] args)
         {
              Token token;
              Stack stack;
              //.....
              //Read some input
              string s = "T";
              //.....
              if(s==anonymousDerivedClassId){
                  //How do I come up with class LetterT and keep it anonymous?
                  Token token = new anonymousDerivedClass(); // 
                  stack.Push(token)
              }
              //.....
              //do some stuff
              //.....
              object a;
              object b;
              Token q = stack.Pop(token);
              q.PerformOperation(a,b);

         }
    }
}

我想创建一个我不想要的子类的新实例知道名字吗?

我“甚至不知道”它是否存在?

希望这不是太复杂...

编辑:

我基本上不想跟踪所有子类...

编辑2(新示例):

考虑:

    static void Main(string[] args)
    {
        Operator op;
        string s = "*";
        if(s==MulOperator.Identifier){
           op = new MulOperator();
        }
        else{
           op = new DivOperator();
        }

        perform(op);

    }
    static void perform(Operator op)
    {
        Console.WriteLine("Sum:{0}", op.PerformOperation(2.2,2.0));
    }

我想用更通用的方法来摆脱 new MulOperator()MulOperator.Identifier ,在本例中创建 Operator 的子类>。

编辑3:*

(解决方案) 我要尝试 C# 中的通用工厂< /a>, 看来这就是我想要实现的目标。

(I'm pretty new to C# so bear with me, started 2 days ago)

Ok, here is the general structure and idea

namespace Tokenize{
    abstract class Token{
        public Base(object id){
            this.id = id;
        }
        private object id;
        public object Id
        { 
            get{...};
            set{...};
        }
        public abstract object PerformOperation(object a, object b);

    }

    class NumberNine: Base{
        public NumberNine(int num) : base(num){ }
        public override int PerformOperation(int a, int b);
    }

    class LetterT: Base{
        public LetterT(char letter) : base(letter){ }
        public override string PerformOperation(string a, char b);
    }
}
using Tokenize;
namespace Program{
    class MyProg{
         static void Main(string[] args)
         {
              Token token;
              Stack stack;
              //.....
              //Read some input
              string s = "T";
              //.....
              if(s==anonymousDerivedClassId){
                  //How do I come up with class LetterT and keep it anonymous?
                  Token token = new anonymousDerivedClass(); // 
                  stack.Push(token)
              }
              //.....
              //do some stuff
              //.....
              object a;
              object b;
              Token q = stack.Pop(token);
              q.PerformOperation(a,b);

         }
    }
}

I want to create a new instance of a subclass where i don't want to know the name of it?

I "dont even know" if it exists?

Hopefully this wasn't too complicated...

EDIT:

I don't want to keep track of all the subclasses basically...

EDIT2 (New example):

Consider:

    static void Main(string[] args)
    {
        Operator op;
        string s = "*";
        if(s==MulOperator.Identifier){
           op = new MulOperator();
        }
        else{
           op = new DivOperator();
        }

        perform(op);

    }
    static void perform(Operator op)
    {
        Console.WriteLine("Sum:{0}", op.PerformOperation(2.2,2.0));
    }

Where I want to get rid of the new MulOperator() and MulOperator.Identifier with a more generic method of creating a subclass of in this case Operator.

EDIT 3:*

(Solution)
I'm going to try A Generic Factory in C#,
it seems like that is what I want to achieve.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回梦里 2024-11-10 13:30:41
string s = "T";
string typeName = string.Empty;

// Determine whether s is a letter or digit
if (Char.IsDigit(s[0]))
{
    typeName = "Digit" + s;
}
else if (Char.IsLetter(s[0]))
{
    typeName = "Letter" + s;
}

// Get real type from typeName
Type type = Type.GetType(typeName);

// Create instance of type, using s as argument
object instance = Activator.CreateInstance(type, new object[] { s });

您必须编辑构造函数,以便它们将 string s 作为参数,并进行正确的验证和解析。

string s = "T";
string typeName = string.Empty;

// Determine whether s is a letter or digit
if (Char.IsDigit(s[0]))
{
    typeName = "Digit" + s;
}
else if (Char.IsLetter(s[0]))
{
    typeName = "Letter" + s;
}

// Get real type from typeName
Type type = Type.GetType(typeName);

// Create instance of type, using s as argument
object instance = Activator.CreateInstance(type, new object[] { s });

You'll have to edit your constructors so they take string s as parameter, and do the proper validation and parsing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文