1 // Copyright 2004-2008 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
.Tests
18 using Castle
.Core
.Interceptor
;
19 using Castle
.DynamicProxy
.Tests
.Interceptors
;
20 using NUnit
.Framework
;
23 public class OutRefParams
: BasePEVerifyTestCase
26 public void CanCreateProxyOfInterfaceWithOutParameter()
28 KeepDataInterceptor interceptor
= new KeepDataInterceptor();
29 object proxy
= generator
.CreateInterfaceProxyWithoutTarget(typeof(WithOut
), interceptor
);
30 Assert
.IsNotNull(proxy
);
34 public void CanCallMethodWithOutParameter()
37 InvocatingInterceptor interceptor
= new InvocatingInterceptor(delegate { }
);
38 WithOut proxy
= (WithOut
) generator
.CreateInterfaceProxyWithoutTarget(typeof(WithOut
), interceptor
);
43 public void CanAffectValueOfOutParameter()
46 InvocatingInterceptor interceptor
=
47 new InvocatingInterceptor(delegate(IInvocation invocation
) { invocation.Arguments[0] = 5; }
);
48 WithOut proxy
= (WithOut
) generator
.CreateInterfaceProxyWithoutTarget(typeof(WithOut
), interceptor
);
50 Assert
.AreEqual(5, i
);
54 public void CanCreateProxyWithRefParam()
57 InvocatingInterceptor interceptor
=
58 new InvocatingInterceptor(delegate(IInvocation invocation
) { invocation.Arguments[0] = 5; }
);
59 WithOut proxy
= (WithOut
) generator
.CreateInterfaceProxyWithoutTarget(typeof(WithOut
), interceptor
);
61 Assert
.AreEqual(5, i
);
66 public void CanCreateComplexOutRefProxyOnClass()
71 InvocatingInterceptor interceptor
= new InvocatingInterceptor(delegate(IInvocation invocation
)
73 invocation
.Arguments
[0] = 5;
74 invocation
.Arguments
[1] = "aaa";
75 invocation
.Arguments
[3] = "bbb";
77 MyClass proxy
= (MyClass
) generator
.CreateClassProxy(typeof(MyClass
), interceptor
);
78 proxy
.MyMethod(out i
, ref s1
, 1, out s2
);
79 Assert
.AreEqual(5, i
);
80 Assert
.AreEqual(s1
, "aaa");
81 Assert
.AreEqual(s2
, "bbb");
85 public void CanCreateProxyWithStructRefParam()
87 MyStruct s
= new MyStruct(10);
88 MyClass proxy
= (MyClass
) generator
.CreateClassProxy(typeof(MyClass
), new StandardInterceptor());
89 proxy
.MyMethodWithStruct(ref s
);
90 Assert
.AreEqual(20, s
.Value
);
93 public struct MyStruct
97 public MyStruct(int value)
103 public interface WithOut
112 public virtual void MyMethod(out int i
, ref string s
, int i1
, out string s2
)
114 throw new NotImplementedException();
117 public virtual void MyMethodWithStruct(ref MyStruct s
)
119 s
.Value
= 2 * s
.Value
;
123 public class InvocatingInterceptor
: IInterceptor
125 public delegate void Invoked(IInvocation invocation
);
127 private Invoked invoked
;
129 public InvocatingInterceptor(Invoked invoked
)
131 this.invoked
= invoked
;
135 public void Intercept(IInvocation invocation
)