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
: BasePEVerifyTestCase
27 public void CanCreateProxyOfInterfaceWithOutParameter()
29 KeepDataInterceptor interceptor
= new KeepDataInterceptor();
30 object proxy
= generator
.CreateInterfaceProxyWithoutTarget(typeof(WithOut
), interceptor
);
31 Assert
.IsNotNull(proxy
);
35 public void CanCallMethodWithOutParameter()
38 InvocatingInterceptor interceptor
= new InvocatingInterceptor(delegate { }
);
39 WithOut proxy
= (WithOut
) generator
.CreateInterfaceProxyWithoutTarget(typeof(WithOut
), interceptor
);
44 public void CanAffectValueOfOutParameter()
47 InvocatingInterceptor interceptor
=
48 new InvocatingInterceptor(delegate(IInvocation invocation
) { invocation.Arguments[0] = 5; }
);
49 WithOut proxy
= (WithOut
) generator
.CreateInterfaceProxyWithoutTarget(typeof(WithOut
), interceptor
);
51 Assert
.AreEqual(5, i
);
55 public void CanCreateProxyWithRefParam()
58 InvocatingInterceptor interceptor
=
59 new InvocatingInterceptor(delegate(IInvocation invocation
) { invocation.Arguments[0] = 5; }
);
60 WithOut proxy
= (WithOut
) generator
.CreateInterfaceProxyWithoutTarget(typeof(WithOut
), interceptor
);
62 Assert
.AreEqual(5, i
);
67 public void CanCreateComplexOutRefProxyOnClass()
72 InvocatingInterceptor interceptor
= new InvocatingInterceptor(delegate(IInvocation invocation
)
74 invocation
.Arguments
[0] = 5;
75 invocation
.Arguments
[1] = "aaa";
76 invocation
.Arguments
[3] = "bbb";
78 MyClass proxy
= (MyClass
) generator
.CreateClassProxy(typeof(MyClass
), interceptor
);
79 proxy
.MyMethod(out i
, ref s1
, 1, out s2
);
80 Assert
.AreEqual(5, i
);
81 Assert
.AreEqual(s1
, "aaa");
82 Assert
.AreEqual(s2
, "bbb");
86 public void CanCreateProxyWithStructRefParam()
88 MyStruct s
= new MyStruct(10);
89 MyClass proxy
= (MyClass
) generator
.CreateClassProxy(typeof(MyClass
), new StandardInterceptor());
90 proxy
.MyMethodWithStruct(ref s
);
91 Assert
.AreEqual(20, s
.Value
);
94 public struct MyStruct
98 public MyStruct(int value)
104 public interface WithOut
113 public virtual void MyMethod(out int i
, ref string s
, int i1
, out string s2
)
115 throw new NotImplementedException();
118 public virtual void MyMethodWithStruct(ref MyStruct s
)
120 s
.Value
= 2 * s
.Value
;
124 public class InvocatingInterceptor
: IInterceptor
126 public delegate void Invoked(IInvocation invocation
);
128 private Invoked invoked
;
130 public InvocatingInterceptor(Invoked invoked
)
132 this.invoked
= invoked
;
136 public void Intercept(IInvocation invocation
)