1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
5 using System.ComponentModel.Composition;
\r
6 using System.ComponentModel.Composition.Factories;
\r
7 using System.ComponentModel.Composition.Hosting;
\r
8 using System.ComponentModel.Composition.Primitives;
\r
10 using System.UnitTesting;
\r
11 using Microsoft.VisualStudio.TestTools.UnitTesting;
\r
13 namespace Tests.Integration
\r
16 public class DelegateCompositionTests
\r
18 public delegate int SimpleDelegate();
\r
19 public delegate object DoWorkDelegate(int i, ref object o, out string s);
\r
21 public class MethodExporter
\r
24 public int SimpleMethod()
\r
29 [Export(typeof(DoWorkDelegate))]
\r
30 public object DoWork(int i, ref object o, out string s)
\r
36 [Export("ActionWith8Arguments")]
\r
37 [Export("ActionWith8Arguments", typeof(Delegate))]
\r
38 public void Action(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8)
\r
42 [Export("FunctionWith8Arguments")]
\r
43 [Export("FunctionWith8Arguments", typeof(Delegate))]
\r
44 public int Function(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8)
\r
46 return i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8;
\r
49 [Export("ActionWith9Arguments")]
\r
50 [Export("ActionWith9Arguments", typeof(Delegate))]
\r
51 public void Action(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9)
\r
55 [Export("FunctionWith9Arguments")]
\r
56 [Export("FunctionWith9Arguments", typeof(Delegate))]
\r
57 public int Function(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9)
\r
59 return i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9;
\r
62 #if CLR40 && !SILVERLIGHT
\r
63 [Export("FunctionWithDefaultValue")]
\r
64 public int FunctionWithDefaultValue(int i, string s = "")
\r
72 public void Export_SimpleCustomDelegate_ShouldWork()
\r
74 var container = ContainerFactory.CreateWithAttributedCatalog(
\r
75 typeof(MethodExporter));
\r
77 var contractName = AttributedModelServices.GetContractName(typeof(SimpleDelegate));
\r
79 var export1 = container.GetExportedValue<SimpleDelegate>();
\r
80 Assert.AreEqual(1, export1());
\r
82 var export2 = container.GetExportedValue<Func<int>>();
\r
83 Assert.AreEqual(1, export1());
\r
85 var export3 = (ExportedDelegate)container.GetExportedValue<object>(contractName);
\r
86 var export4 = (SimpleDelegate)export3.CreateDelegate(typeof(SimpleDelegate));
\r
87 Assert.AreEqual(1, export4());
\r
91 public void Export_CustomDelegateWithOutRefParams_ShouldWork()
\r
93 var container = ContainerFactory.CreateWithAttributedCatalog(
\r
94 typeof(MethodExporter));
\r
96 var export1 = container.GetExportedValue<DoWorkDelegate>();
\r
99 object o = new object();
\r
102 export1(i, ref o, out s);
\r
106 public void Export_FunctionWith8Arguments_ShouldWorkFine()
\r
108 var container = ContainerFactory.CreateWithAttributedCatalog(
\r
109 typeof(MethodExporter));
\r
111 #if CLR40 && !SILVERLIGHT
\r
112 Assert.IsNotNull(container.GetExportedValue<Delegate>("FunctionWith8Arguments"));
\r
114 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
115 container.GetExportedValue<Delegate>("FunctionWith8Arguments"));
\r
119 public delegate int FuncWith9Args(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9);
\r
122 public void Export_FuncWith9Arguments_ShouldThrowContractMismatch()
\r
124 var container = ContainerFactory.CreateWithAttributedCatalog(
\r
125 typeof(MethodExporter));
\r
127 // Cannot Create a standard delegate that takes 9 arguments
\r
128 // so the generic Delegate type will not work
\r
129 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
130 container.GetExportedValue<Delegate>("FunctionWith9Arguments"));
\r
132 // If a matching custom delegate type is used then it will work
\r
133 Assert.IsNotNull(container.GetExportedValue<FuncWith9Args>("FunctionWith9Arguments"));
\r
137 public void Export_ActionWith8Arguments_ShouldWorkFine()
\r
139 var container = ContainerFactory.CreateWithAttributedCatalog(
\r
140 typeof(MethodExporter));
\r
142 #if CLR40 && !SILVERLIGHT
\r
143 Assert.IsNotNull(container.GetExportedValue<Delegate>("ActionWith8Arguments"));
\r
145 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
146 container.GetExportedValue<Delegate>("ActionWith8Arguments"));
\r
150 public delegate void ActionWith9Args(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9);
\r
153 public void Export_ActionWith9Arguments_ShouldThrowContractMismatch()
\r
155 var container = ContainerFactory.CreateWithAttributedCatalog(
\r
156 typeof(MethodExporter));
\r
158 // Cannot Create a standard delegate that takes 9 arguments
\r
159 // so the generic Delegate type will not work
\r
160 ExceptionAssert.Throws<CompositionContractMismatchException>(() =>
\r
161 container.GetExportedValue<Delegate>("ActionWith9Arguments"));
\r
163 // If a matching custom delegate type is used then it will work
\r
164 Assert.IsNotNull(container.GetExportedValue<ActionWith9Args>("ActionWith9Arguments"));
\r
167 #if CLR40 && !SILVERLIGHT
\r
169 public void Export_FunctionWithDefaultValue_ShouldWorkFine()
\r
171 var container = ContainerFactory.CreateWithAttributedCatalog(
\r
172 typeof(MethodExporter));
\r
174 var export = container.GetExportedValue<Func<int, string, int>>("FunctionWithDefaultValue");
\r
175 Assert.AreEqual(3, export(3, "a"));
\r
177 // Even though the string argument is optional it still cannot be cast to Func<int, int>.
\r
178 var export2 = (ExportedDelegate)container.GetExportedValue<object>("FunctionWithDefaultValue");
\r
179 var export3 = export2.CreateDelegate(typeof(Func<int, int>));
\r
181 Assert.IsNull(export3);
\r
186 public delegate int DelegateOneArg(int i);
\r
187 public delegate int DelegateTwoArgs(int i, int j);
\r
189 public class CustomExportedDelegate : ExportedDelegate
\r
191 private Func<int, int, int> _func;
\r
193 public CustomExportedDelegate(Func<int, int, int> func)
\r
198 public override Delegate CreateDelegate(Type delegateType)
\r
200 if (delegateType == typeof(DelegateOneArg))
\r
202 return (DelegateOneArg)((i) => this._func(i, 0));
\r
204 else if (delegateType == typeof(DelegateTwoArgs))
\r
206 return (DelegateTwoArgs)((i, j) => this._func(i, j));
\r
213 public class ExportCustomExportedDelegates
\r
215 [Export("CustomExportedDelegate", typeof(DelegateOneArg))]
\r
216 [Export("CustomExportedDelegate", typeof(DelegateTwoArgs))]
\r
217 public ExportedDelegate MyExportedDelegate
\r
221 return new CustomExportedDelegate(DoWork);
\r
225 public int DoWork(int i, int j)
\r
232 public class ImportCustomExportedDelegates
\r
234 [Import("CustomExportedDelegate")]
\r
235 public DelegateOneArg DelegateOneArg { get; set; }
\r
237 [Import("CustomExportedDelegate")]
\r
238 public DelegateTwoArgs DelegateTwoArgs { get; set; }
\r
243 public void CustomExportedDelegate_ShouldWork()
\r
245 var container = ContainerFactory.CreateWithAttributedCatalog(
\r
246 typeof(ExportCustomExportedDelegates),
\r
247 typeof(ImportCustomExportedDelegates));
\r
249 var importer = container.GetExportedValue<ImportCustomExportedDelegates>();
\r
251 Assert.AreEqual(1, importer.DelegateOneArg(1));
\r
252 Assert.AreEqual(2, importer.DelegateTwoArgs(1, 1));
\r
255 public delegate void GetRef(ref int i);
\r
256 public delegate void GetOut(out int i);
\r
258 public class RefOutMethodExporter
\r
261 public void MyGetOut(out int i)
\r
268 public void MethodWithOutParam_ShouldWorkWithRefParam()
\r
270 var container = ContainerFactory.CreateWithAttributedCatalog(
\r
271 typeof(RefOutMethodExporter));
\r
275 var export1 = container.GetExportedValue<GetRef>();
\r
278 Assert.AreEqual(29, i);
\r
281 var export2 = container.GetExportedValue<GetOut>();
\r
284 Assert.AreEqual(29, i);
\r