Vâng, đó là một hạn chế; không có cấu trúc ngôn ngữ cho điều đó.
Đề xuất của tôi trong trường hợp này là tạo một số được nhập ủy quyền cho hàm tạo cho mỗi loại; bộ nhớ cache mà đại biểu (thường là trong một trường tĩnh của một loại chung, để thuận tiện) và tái sử dụng nó. Tôi có thể cung cấp một ví dụ sau - nhưng tôi không thể làm điều đó từ iPod;)
Tôi tin rằng Tôi đã cam kết một số mã này vào thư viện MiscUtil của Jon Skeet; vì vậy bạn cũng có thể nhìn ở đó.
Theo yêu cầu (bình luận), đây là một cách để làm điều này - trong trường hợp này bằng cách sử dụng Expression
API. Lưu ý đặc biệt việc sử dụng các lớp generic lồng nhau để đảm bảo chúng ta làm sự phản ánh/biên soạn cùng một lúc nhất cho mỗi kiểu kết hợp:
using System;
using System.Linq.Expressions;
class Program {
static void Main() {
var ctor = TypeFactory.GetCtor<int, string, DemoType>();
var obj = ctor(123, "abc");
Console.WriteLine(obj.I);
Console.WriteLine(obj.S);
}
}
class DemoType {
public int I { get; private set; }
public string S { get; private set; }
public DemoType(int i, string s) {
I = i; S = s;
}
}
static class TypeFactory {
public static Func<T> GetCtor<T>() { return Cache<T>.func; }
public static Func<TArg1, T> GetCtor<TArg1, T>() { return Cache<T, TArg1>.func; }
public static Func<TArg1, TArg2, T> GetCtor<TArg1, TArg2, T>() { return Cache<T, TArg1, TArg2>.func; }
private static Delegate CreateConstructor(Type type, params Type[] args) {
if(type == null) throw new ArgumentNullException("type");
if(args == null) args = Type.EmptyTypes;
ParameterExpression[] @params = Array.ConvertAll(args, Expression.Parameter);
return Expression.Lambda(Expression.New(type.GetConstructor(args), @params), @params).Compile();
}
private static class Cache<T> {
public static readonly Func<T> func = (Func<T>)TypeFactory.CreateConstructor(typeof(T));
}
private static class Cache<T, TArg1> {
public static readonly Func<TArg1, T> func = (Func<TArg1, T>)TypeFactory.CreateConstructor(typeof(T), typeof(TArg1));
}
private static class Cache<T, TArg1, TArg2> {
public static readonly Func<TArg1, TArg2, T> func = (Func<TArg1, TArg2, T>)TypeFactory.CreateConstructor(typeof(T), typeof(TArg1), typeof(TArg2));
}
}
thể trùng lặp của [Tạo thể hiện của loại generic ?] (http://stackoverflow.com/questions/731452/create-instance-of-generic-type) – nawfal