在C#中创建通用方法

时间:2010-10-24 12:54:50

标签: c#

我有以下结构:

public enum MyTypes
{
    Type1 = 1, 
    Type2 = 2,
    Type3 = 3
}

public abstract class class1
{
   int id;
   string name;
   MyType type;
}

public class class2 : class1
{

}


public class class3 : class1
{

}

public class class4 : class1
{

}

现在我要做的是创建一个泛型方法,我想给它类型对象说类3,它将从类3创建对象并定义它的变量并返回它以便能够将它添加到class1的列表

像那样

private class1 myFunction (MyType t , int id , string name)
{
    T obj = new T();
    obj.type = t ;
    obj.id = id ;
    obj.name = name;
    return obj;
}

如何创建这种通用方法?

请尽快帮助我

先谢谢

5 个答案:

答案 0 :(得分:13)

正如Danny Chen在他的回答中所说,你必须稍微修改你的课程定义才能发挥作用,然后你可以做如下的事情:

public T myFunction<T>(int id, string name) where T : class1, new()
{
    T obj = new T();
    obj.id = id;
    obj.name = name;
    return obj;
}

这种通用方法要求类型参数T派生自class1,并且还需要一个无参数构造函数 - 这就是where T : class1, new()的含义。

由于idname属性是通过class1基类定义的,因此您可以将这些属性设置为通过其参数传递到myFunction的任何内容。


有关class1的更多注意事项:

  • 考虑将class1设为接口而不是抽象类,因为它不包含任何功能。
  • idnametype如果您确实希望能够访问它们,则需要公开。
  • 通常,字段实际上不会显示为public。考虑使用属性代替。

答案 1 :(得分:2)

不,这是不可能的,因为所有这些类都是abstract(无法实例化)。我们无法创建抽象类的实例。

答案 2 :(得分:0)

如果我理解正确,您就会问如何创建Factory pattern。这正是此模式的设计目的:指定所需对象的类型并将其返回给您。

或者,您可以使用功能中的类型开关。

private class1 myFunction (MyType t , int id , string name)
{
    class1 obj;
    switch(t)
    {
        case MyTypes.Type1:
           obj = new Class1();
        ...
    }

    obj.type = t ;
    obj.id = id ;
    obj.name = name;
    return obj;
}

第三种方法是使用泛型。

private T myFunction<T>(MyType t , int id , string name) where T : class1, new()
{
    T obj = new T();
    obj.type = t ;
    obj.id = id ;
    obj.name = name;
    return obj;
}

你会这样称呼:

var obj = myFunction<Class3>(MyTypes.Type3, 5, "some name");

答案 3 :(得分:0)

您正在寻找的是基本的抽象工厂设计模式。 http://en.wikipedia.org/wiki/Abstract_factory_pattern

执行以下

之类的操作
private class1 createSpecializedType<T>(MyType t, int id, string name) where T: class1, new()
{
    T obj = new T();
    obj.type = t ;
    obj.id = id ;
    obj.name = name;
    return obj;
}

public class1 createMyType(MyType t, int id, string name)
{
    switch(t)
    {
         case Type1:
              return this.createSpecializedType<class2>(t, id, name);
         case Type2:
              return this.createSpecializedType<class3>(t, id, name);
         case Type3:
              return this.createSpecializedType<class4>(t, id, name);
    }
}

代码未经过测试,应被视为伪代码。

这将允许您调用createMyType,它将根据参数确定您想要的类,并返回正确的对象。

PS:当然这些类是抽象的,无法实现。但我怀疑这是在复制/重写代码时引入的错误。

答案 4 :(得分:0)

如果我理解正确,MyTypes和类的实际类型表达同样的事情。在这种情况下,您需要从MyTypes到实际类的某种映射,例如:

static Dictionary<MyTypes, Type> typeMapping = new Dictionary<MyTypes, Type>
  {
    { MyTypes.Type1, typeof(class2) },
    { MyTypes.Type2, typeof(class3) },
    { MyTypes.Type2, typeof(class4) },
  };

然后,您可以使用反射创建指定类型的实例:

private class1 myFunction(MyType t, int id, string name)
{
    class1 obj = (class1)typeMapping[t].GetConstructor(Type.EmptyTypes).Invoke(null);
    obj.type = t ;
    obj.id = id ;
    obj.name = name;
    return obj;
}
相关问题