2

目前,我有这样的代码:

    static void Main()
    {
        int whichClass = 0;
        if (whichClass == 0)
        {
            //repeat code
            TestAbstract clTest = new ClassA();
            clTest.MainFunc();
        }
        else if (whichClass == 1)
        {
            //repeat code
            TestAbstract clTest = new ClassB();
            clTest.MainFunc();
        }
        else if (whichClass == 10)
        {
            //repeat code
            TestAbstract clTest = new ClassX();
            clTest.MainFunc();
        }
    }

如您所见,我必须为 3 个不同类的初始和调用函数编写 3 次代码。

我想要的是我们只用动态类调用 1 次。怎么可能?

4

7 回答 7

11

好吧,你可以有一个Dictionary<int, Type>,或一个Dictionary<int, Func<TestAbstract>

var typeMapping = new Dictionary<int, Type>
{
    { 0, typeof(ClassA) },
    { 1, typeof(ClassB) },
    { 10, typeof(ClassX) }
};

...
Type type;
if (typeMapping.TryGetValue(whichClass, out type))
{
    TestAbstract test = (TestAbstract) Activator.CreateInstance(type);
    test.MainFunc();
}

使用 aFunc<TestAbstract>将为您创建TestAbstract实例的方式提供更大的灵活性,并提供更多的编译时类型安全性,但更冗长:

var factoryMapping = new Dictionary<int, Func<TestAbstract>>
{
    { 0, () => new ClassA() },
    { 1, () => new ClassB() },
    { 10, () => new ClassX() }
};

...
Func<TestAbstract> factory;
if (factoryMapping.TryGetValue(whichClass, out factory))
{
    TestAbstract test = factory();
    test.MainFunc();
}

顺便说一下,尚不清楚整数值的来源 - 你可能需要一个枚举。哎呀,枚举名称甚至可以是类型的名称:

TestClass whichClass = TestClass.ClassA;

...

Type type = Type.GetType("SomeNamespace." + whichClass);
TestAbstract test = (TestAbstract) Activator.CreateInstance(type);
于 2013-01-18T09:05:59.570 回答
5

您可以创建一个工厂类,它将根据传递的参数返回对象。

public static TestAbstractFactory
{
  public static TestAbstract GetTestAbstract(int whichClass)
  { 
      switch(whichClass)
      {
      case 0:
        return new ClassA();
      case 1:
        return new ClassB();
      case 10:
        return new ClassX();
      default: 
        return null;
      }
  }
}
于 2013-01-18T09:06:10.197 回答
3
static void Main()
{
        int whichClass = 0;
        TestAbstract clTest = null;
        if (whichClass == 0)
            clTest = new ClassA();
        else if (whichClass == 1)
             clTest = new ClassB();
        else if (whichClass == 10)
             clTest = new ClassX();
        if(clTest != null)
             clTest.MainFunc();

}

于 2013-01-18T09:07:17.050 回答
2

Assuming ClassA to C derives from TestAbstract and assuming MainFunc is a virtual method on TestAbstract, you can use the following code

static void Main()
{
    int whichClass = 0; // 0 to 2
    Type type = new[] { typeof(ClassA), typeof(ClassB), typeof(ClassC) } [ whichClass ];        
    TestAbstract clTest = (TestAbstract) Activator.CreateInstance(type);
    clTest.MainFunc();
}
于 2013-01-18T09:08:24.847 回答
0
static void Main()
{
  int whichClass = 0;
  TestAbstract clTest ;
  if (whichClass == 0)
  {
   //repeat code
   clTest = new ClassA();
  }
  else if (whichClass == 1)
  {
   //repeat code
   clTest = new ClassB();
  }
 else if (whichClass == 10)
 {
  //repeat code
   clTest = new ClassX();        
  }

  clTest.MainFunc();
}
于 2013-01-18T09:06:42.660 回答
0

静态无效主要()

{
    int whichClass = 0;
    TestAbstract clTest;

    if (whichClass == 0)
    {
        //repeat code
         clTest = new ClassA();
    }
    else if (whichClass == 1)
    {
        //repeat code
        clTest = new ClassB();
    }
    else if (whichClass == 10)
    {
        //repeat code
        clTest = new ClassX();
    }
    if(clTest != null)
        clTest.MainFunc();
}
于 2013-01-18T09:07:48.337 回答
0

you can use reflection.

string whichClass = "className";
Type t = Type.GetType(whichClass);
TestAbstract o = (TestAbstract)Activator.CreateInstance(t);
o.MainFunc();
于 2013-01-18T09:09:27.823 回答