类名作为参数? [英] class name as parameter?

查看:63
本文介绍了类名作为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个具有相同方法组的单例类。

I have 3 singleton classes with same set of methods.

在我的调用函数中,我写的是:

in my calling function, I am writing:

十进制结果= 0;

开关(myParam)

switch (myParam)

{

案例值1:

result = MyClass1.Instance.GetData(); break;

result = MyClass1.Instance.GetData(); break;

case valu2:

case valu2:

result = MyClass2.Instance.GetData(); break;

result = MyClass2.Instance.GetData(); break;

默认值:

result = MyClass3.Instance.GetData();休息;

result = MyClass3.Instance.GetData(); break;

}

返回结果;

我想用一个语句替换上面的开关,并将className作为参数传递给我上面的调用函数。以下代码是否适用于单身人士?如果没有,我该怎么做?

I want to replace the above switch with a single statement and pass className as parameter to my above calling function. Will the below code work for a singleton? If not, how would I do it?

object myObject = Activator.CreateInstance(className);

object myObject = Activator.CreateInstance(className);

decimal result = myObject.GetData();

decimal result = myObject.GetData();

谢谢。

推荐答案

C#不是脚本语言。 

C# is not a scripting language. 

你想要什么是脚本的典型代表。如果你在互联网上搜索,你会发现它被问过一千次。 

What you want is typical for scripting. If you search on Internet you can find it asked thousand times. 

然而,一个课程不是一个对象,所以你永远找不到真正令人满意的答案。 

However, a class is not an object so you will never find a real satisfying answer. 

请记住,C#类需要始终被实例化(它只是模板)。在编译时静态或使用"新"静态运行时的关键字。 

Keep in mind C# classes needs always to be instanced to be used (it are just templates). Either static at compile time or with the "new" keyword at runtime. 


这篇关于类名作为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆