带有类型参数的方法,用于返回所述类型的数组

时间:2011-07-15 02:32:09

标签: c# generics constructor activator

我正在尝试在C#中创建一个返回指定类型数组的泛型方法。这就是我的方法目前的样子:

private T[] GetKeys<T>(string key, Dictionary<string, object> obj) where T : new()
{
    if (obj.ContainsKey(key))
    {
        object[] objs = (object[])obj[key];
        T[] list = new T[objs.Length];
        for (int i = 0; i < objs.Length; i++)
        {
            list[i] = (T)Activator.CreateInstance(
                             typeof(T),
                             new object[] {
                                 (Dictionary<string, object>)objs[i] 
                             });
        }
        return list;
    }
    else
    {
        return null;
    }
}

由于此类在内部使用,并且使用库无法使用,因此我已经知道将在<T>中放入哪些类。所有类在其构造函数中都具有相同的参数。但是在这段代码甚至编译之前,我不得不给它们一个没有参数的公共构造函数。现在当我到达Activator.CreateInstance行时,我收到错误Constructor on type 'MyNamespace.MyClass+MyOtherClass' not foundMyClass是包含上述方法的类。 MyOtherClass是以T传递的类。

任何帮助将不胜感激,谢谢!

3 个答案:

答案 0 :(得分:2)

只要您的构造函数如下所示,这应该对您有用:

public MyType (Dictionary<string,object> dict)
{
}

如果你的构造函数是非公共的,你需要改变GetConstructor以传入BindingFlags.NonPublic。

        private T[] GetKeys<T>(string key, Dictionary<string, object> obj)
        // Since you're not using the default constructor don't need this:
        //   where T : new()
        {
            if (obj.ContainsKey(key))
            {
                object[] objs = (object[])obj[key];
                T[] list = new T[objs.Length];
                for (int i = 0; i < objs.Length; i++)
                {
                    list[i] = (T)typeof(T).GetConstructor(new [] {typeof(Dictionary<string,object>)}).Invoke (new object[] {
                                         (Dictionary<string, object>)objs[i] 
                                     });
                }
                return list;
            }
            else
            {
                return null;
            }
        }

答案 1 :(得分:1)

你的GetKeys&lt; T&gt;需要T must have a public parameterless constructor

private T[] GetKeys<T>(...) where T : new()
                                       ↑

这个约束在T上允许你在方法体中编写这样的代码:

T t = new T();

由于您没有使用它,但期望某个其他构造函数(不能像公共无参数构造函数那样强制执行),只需删除约束即可。它可以工作。

答案 2 :(得分:0)

由于您为了这个方法而在类中构建相似性,因此最好只让它们扩展相同的接口(或继承相同的基类,适用于您的应用程序)。然后,您只需要使用传入的构造函数参数来构建基类列表。然后在调用代码中,您可以根据需要强制转换列表项。