update MEF to preview 9
[mcs.git] / class / System.ComponentModel.Composition / src / ComponentModel / Microsoft / Internal / ContractServices.cs
blob4a125d61407ed7c1b4df92eaa7717b17cb34bab7
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel.Composition.Hosting;
4 using System.Linq;
5 using System.Text;
6 using System.Threading;
7 using System.ComponentModel.Composition.Primitives;
9 namespace Microsoft.Internal
11 internal class ContractServices
13 public static T Cast<T>(object o)
15 return (T)o;
18 public static bool TryCast(Type contractType, object value, out object result)
20 if (value == null)
22 result = null;
23 return true;
25 if (contractType.IsInstanceOfType(value))
27 result = value;
28 return true;
31 // We couldn't cast see if a delegate works for us.
32 if (typeof(Delegate).IsAssignableFrom(contractType))
34 ExportedDelegate exportedDelegate = value as ExportedDelegate;
35 if (exportedDelegate != null)
37 result = exportedDelegate.CreateDelegate(contractType);
38 return (result != null);
42 result = null;
43 return false;