우선 Activator.CreateInstance() 은 올바른 방법입니다.
그러나, 인 더 재미있는 방법이 : 빨리
그냥 생성자 호출 표현식을 작성 TargetInvocationException에 예외를 포장하지 마십시오가 :
public static Func<object[], object> CreateConstructorDelegate(ConstructorInfo method)
{
var args = Expression.Parameter(typeof(object[]), "args");
var parameters = new List<Expression>();
var methodParameters = method.GetParameters().ToList();
for (var i = 0; i < methodParameters.Count; i++)
{
parameters.Add(Expression.Convert(
Expression.ArrayIndex(args, Expression.Constant(i)),
methodParameters[i].ParameterType));
}
var call = Expression.Convert(Expression.New(method, parameters), typeof(object));
Expression body = call;
var callExpression = Expression.Lambda<Func<object[], object>>(body, args);
var result = callExpression.Compile();
return result;
}
성능 테스트 :
public void activator()
{
var stopwatch = new Stopwatch();
const int times = 10000000;
stopwatch.Start();
for (int i = 0; i < times; i++)
{
var v = Activator.CreateInstance(typeof (C));
}
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds + "ms with activator");
var del = CreateConstructorDelegate(typeof(C).GetConstructor(new Type[0]));
stopwatch = new Stopwatch();
stopwatch.Start();
var args = new object[0];
for (int i = 0; i < times; i++)
{
var v = del(args);
}
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds + "ms with expression");
}
출력 :
1569ms with activator
134ms with expression
그러나 :
- C# 3.0 만
- 컴파일해야()는 그냥 호기심에 대한 장기 실행 작업
입니다 .
JMSA, 관심있는 단어, __if__ 이것은 DAL/순환 의존성 문제에 관한 것입니다. 다른 질문은 도로에서 더 멀리 나아가고 있습니다. –
@Henk Holterman, 누군가 나에게이 사실을 알리라는 것을 알고있었습니다. 하지만 내가 뭘 할 수 있을까? – anonymous
@Henk Holteman, http://stackoverflow.com/questions/1415911/unable-to-get-data-from-da-layer-what-to-do이 문제를 해결할 다른 방법은 없습니다. – anonymous