1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
.Builder
.CodeBuilder
18 using System
.Reflection
;
19 using System
.Reflection
.Emit
;
21 using Castle
.DynamicProxy
.Builder
.CodeBuilder
.SimpleAST
;
22 using Castle
.DynamicProxy
.Builder
.CodeBuilder
.Utils
;
25 /// Summary description for EasyMethod.
28 public class EasyMethod
: IEasyMember
30 protected MethodBuilder _builder
;
31 protected ArgumentReference
[] _arguments
;
33 private MethodCodeBuilder _codebuilder
;
34 private AbstractEasyType _maintype
;
36 internal EasyMethod( AbstractEasyType maintype
, String name
,
37 ReturnReferenceExpression returnRef
, params ArgumentReference
[] arguments
) :
39 MethodAttributes
.HideBySig
| MethodAttributes
.Virtual
| MethodAttributes
.Public
,
44 internal EasyMethod( AbstractEasyType maintype
, String name
,
45 MethodAttributes attrs
,
46 ReturnReferenceExpression returnRef
, params ArgumentReference
[] arguments
)
49 _arguments
= arguments
;
51 Type returnType
= returnRef
.Type
;
52 Type
[] args
= ArgumentsUtil
.InitializeAndConvert( arguments
);
54 _builder
= maintype
.TypeBuilder
.DefineMethod( name
, attrs
,
58 protected internal EasyMethod()
62 public virtual MethodCodeBuilder CodeBuilder
66 if (_codebuilder
== null)
68 _codebuilder
= new MethodCodeBuilder(
69 _maintype
.BaseType
, _builder
, _builder
.GetILGenerator() );
75 public ArgumentReference
[] Arguments
77 get { return _arguments; }
80 internal MethodBuilder MethodBuilder
82 get { return _builder; }
85 public Type ReturnType
87 get { return _builder.ReturnType; }
90 public MethodBase Member
92 get { return _builder; }
95 public virtual void Generate()
97 _codebuilder
.Generate(this, _builder
.GetILGenerator());
100 public virtual void EnsureValidCodeBlock()
102 if (CodeBuilder
.IsEmpty
)
104 CodeBuilder
.AddStatement( new NopStatement() );
105 CodeBuilder
.AddStatement( new ReturnStatement() );
109 public void DefineParameters(ParameterInfo
[] info
)
111 foreach (ParameterInfo parameterInfo
in info
)
113 _builder
.DefineParameter(parameterInfo
.Position
+1,parameterInfo
.Attributes
, parameterInfo
.Name
);