testing commit notification
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy / Generators / Emitters / SimpleAST / NewInstanceExpression.cs
blob27938006d22f21c65fb1bae4d4c24b8ec2a5cd75
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
17 using System;
18 using System.Reflection;
19 using System.Reflection.Emit;
21 public class NewInstanceExpression : Expression
23 private Type type;
24 private Type[] constructor_args;
25 private Expression[] arguments;
26 private ConstructorInfo constructor;
28 public NewInstanceExpression(ConstructorInfo constructor, params Expression[] args)
30 this.constructor = constructor;
31 arguments = args;
34 public NewInstanceExpression(Type target, Type[] constructor_args, params Expression[] args)
36 type = target;
37 this.constructor_args = constructor_args;
38 arguments = args;
41 public override void Emit(IMemberEmitter member, ILGenerator gen)
43 foreach(Expression exp in arguments)
45 exp.Emit(member, gen);
48 if (constructor == null)
50 constructor = type.GetConstructor(constructor_args);
53 if (constructor == null)
55 throw new ApplicationException("Could not find constructor matching specified arguments");
58 gen.Emit(OpCodes.Newobj, constructor);