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
.Generators
.Emitters
18 using System
.Reflection
;
19 using System
.Reflection
.Emit
;
20 using Castle
.DynamicProxy
.Generators
.Emitters
.SimpleAST
;
22 public class PropertyEmitter
: IMemberEmitter
24 private PropertyBuilder builder
;
25 private AbstractTypeEmitter parentTypeEmitter
;
26 private MethodEmitter getMethod
;
27 private MethodEmitter setMethod
;
28 // private ParameterInfo[] indexParameters;
30 public PropertyEmitter(AbstractTypeEmitter parentTypeEmitter
, String name
, PropertyAttributes attributes
,
33 this.parentTypeEmitter
= parentTypeEmitter
;
35 builder
= parentTypeEmitter
.TypeBuilder
.DefineProperty(name
, attributes
, propertyType
, new Type
[0]);
38 public MethodEmitter GetMethod
40 get { return getMethod; }
41 set { getMethod = value; }
44 public MethodEmitter SetMethod
46 get { return setMethod; }
47 set { setMethod = value; }
50 public MethodEmitter
CreateGetMethod()
52 return CreateGetMethod(MethodAttributes
.Public
|
53 MethodAttributes
.Virtual
|
54 MethodAttributes
.SpecialName
);
57 public MethodEmitter
CreateGetMethod(MethodAttributes attrs
, params Type
[] parameters
)
59 if (getMethod
!= null)
61 throw new InvalidOperationException("A getMethod exists");
64 if (parameters
.Length
== 0)
66 getMethod
= new MethodEmitter(parentTypeEmitter
, "get_" + builder
.Name
, attrs
);
70 getMethod
= new MethodEmitter(parentTypeEmitter
, "get_" + builder
.Name
,
79 public MethodEmitter
CreateSetMethod()
81 return CreateSetMethod(MethodAttributes
.Public
|
82 MethodAttributes
.Virtual
|
83 MethodAttributes
.SpecialName
);
86 public MethodEmitter
CreateSetMethod(Type arg
)
88 return CreateSetMethod(MethodAttributes
.Public
|
89 MethodAttributes
.Virtual
|
90 MethodAttributes
.SpecialName
, arg
);
93 public MethodEmitter
CreateSetMethod(MethodAttributes attrs
, params Type
[] parameters
)
95 if (setMethod
!= null)
97 throw new InvalidOperationException("A setMethod exists");
100 if (parameters
.Length
== 0)
102 setMethod
= new MethodEmitter(parentTypeEmitter
, "set_" + builder
.Name
, attrs
);
106 setMethod
= new MethodEmitter(parentTypeEmitter
, "set_" + builder
.Name
,
114 public MemberInfo Member
119 public Type ReturnType
121 get { return builder.PropertyType; }
124 public void Generate()
126 if (setMethod
!= null)
128 setMethod
.Generate();
129 builder
.SetSetMethod(setMethod
.MethodBuilder
);
132 if (getMethod
!= null)
134 getMethod
.Generate();
135 builder
.SetGetMethod(getMethod
.MethodBuilder
);
139 public void EnsureValidCodeBlock()
141 if (setMethod
!= null)
143 setMethod
.EnsureValidCodeBlock();
146 if (getMethod
!= null)
148 getMethod
.EnsureValidCodeBlock();