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.
16 namespace Castle
.DynamicProxy
.Tests
19 using Castle
.Core
.Interceptor
;
20 using Castle
.DynamicProxy
.Tests
.Interceptors
;
21 using NUnit
.Framework
;
24 public class OutRefParams
26 private ProxyGenerator generator
;
31 generator
= new ProxyGenerator();
35 public void CanCreateProxyOfInterfaceWithOutParameter()
37 KeepDataInterceptor interceptor
= new KeepDataInterceptor();
38 object proxy
= generator
.CreateInterfaceProxyWithoutTarget(typeof(WithOut
), interceptor
);
39 Assert
.IsNotNull(proxy
);
43 public void CanCallMethodWithOutParameter()
46 InvocatingInterceptor interceptor
= new InvocatingInterceptor(delegate { }
);
47 WithOut proxy
= (WithOut
) generator
.CreateInterfaceProxyWithoutTarget(typeof(WithOut
), interceptor
);
52 public void CanAffectValueOfOutParameter()
55 InvocatingInterceptor interceptor
=
56 new InvocatingInterceptor(delegate(IInvocation invocation
) { invocation.Arguments[0] = 5; }
);
57 WithOut proxy
= (WithOut
) generator
.CreateInterfaceProxyWithoutTarget(typeof(WithOut
), interceptor
);
59 Assert
.AreEqual(5, i
);
63 public void CanCreateProxyWithRefParam()
66 InvocatingInterceptor interceptor
=
67 new InvocatingInterceptor(delegate(IInvocation invocation
) { invocation.Arguments[0] = 5; }
);
68 WithOut proxy
= (WithOut
) generator
.CreateInterfaceProxyWithoutTarget(typeof(WithOut
), interceptor
);
70 Assert
.AreEqual(5, i
);
75 public void CanCreateComplexOutRefProxyOnClass()
80 InvocatingInterceptor interceptor
= new InvocatingInterceptor(delegate(IInvocation invocation
)
82 invocation
.Arguments
[0] = 5;
83 invocation
.Arguments
[1] = "aaa";
84 invocation
.Arguments
[3] = "bbb";
86 MyClass proxy
= (MyClass
) generator
.CreateClassProxy(typeof(MyClass
), interceptor
);
87 proxy
.MyMethod(out i
, ref s1
, 1, out s2
);
88 Assert
.AreEqual(5, i
);
89 Assert
.AreEqual(s1
, "aaa");
90 Assert
.AreEqual(s2
, "bbb");
94 public void CanCreateProxyWithStructRefParam()
96 MyStruct s
= new MyStruct(10);
97 MyClass proxy
= (MyClass
)generator
.CreateClassProxy(typeof(MyClass
), new StandardInterceptor());
98 proxy
.MyMethodWithStruct(ref s
);
99 Assert
.AreEqual(20, s
.Value
);
102 public struct MyStruct
105 public MyStruct(int value)
111 public interface WithOut
120 public virtual void MyMethod(out int i
, ref string s
, int i1
, out string s2
)
122 throw new NotImplementedException();
125 public virtual void MyMethodWithStruct(ref MyStruct s
)
127 s
.Value
= 2 * s
.Value
;
131 public class InvocatingInterceptor
: IInterceptor
133 public delegate void Invoked(IInvocation invocation
);
135 private Invoked invoked
;
137 public InvocatingInterceptor(Invoked invoked
)
139 this.invoked
= invoked
;
143 public void Intercept(IInvocation invocation
)