| | | 1 | | // |
| | | 2 | | // Copyright 2010 Ekon Benefits |
| | | 3 | | // |
| | | 4 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
| | | 5 | | // you may not use this file except in compliance with the License. |
| | | 6 | | // You may obtain a copy of the License at |
| | | 7 | | // |
| | | 8 | | // http://www.apache.org/licenses/LICENSE-2.0 |
| | | 9 | | // |
| | | 10 | | // Unless required by applicable law or agreed to in writing, software |
| | | 11 | | // distributed under the License is distributed on an "AS IS" BASIS, |
| | | 12 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | | 13 | | // See the License for the specific language governing permissions and |
| | | 14 | | // limitations under the License. |
| | | 15 | | |
| | | 16 | | using System.Collections.Generic; |
| | | 17 | | using System.Reflection.Emit; |
| | | 18 | | using ImpromptuInterface.Build; |
| | | 19 | | |
| | | 20 | | namespace ImpromptuInterface |
| | | 21 | | { |
| | | 22 | | using System; |
| | | 23 | | |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Main API |
| | | 27 | | /// </summary> |
| | | 28 | | public static class Impromptu |
| | | 29 | | { |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Extension Method that Wraps an existing object with an Explicit interface definition |
| | | 33 | | /// </summary> |
| | | 34 | | /// <typeparam name="TInterface">The type of the interface.</typeparam> |
| | | 35 | | /// <param name="originalDynamic">The original object can be annoymous type, System.DynamicObject as well as any |
| | | 36 | | /// <param name="otherInterfaces">Optional other interfaces.</param> |
| | | 37 | | /// <returns></returns> |
| | | 38 | | public static TInterface ActLike<TInterface>(this object originalDynamic, params Type[] otherInterfaces) where T |
| | 79 | 39 | | => BuildProxy.DefaultProxyMaker.ActLike<TInterface>(originalDynamic, otherInterfaces); |
| | | 40 | | |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Unwraps the act like proxy (if wrapped). |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="proxiedObject">The proxied object.</param> |
| | | 46 | | /// <returns></returns> |
| | | 47 | | public static dynamic UndoActLike(this object proxiedObject) |
| | | 48 | | { |
| | | 49 | | |
| | 3 | 50 | | var actLikeProxy = proxiedObject as IActLikeProxy; |
| | 3 | 51 | | if (actLikeProxy != null) |
| | | 52 | | { |
| | 2 | 53 | | object original = actLikeProxy.Original; |
| | | 54 | | // Where the stand-in would be handed out rather than used; Initialize |
| | | 55 | | // guards the way back in. |
| | 2 | 56 | | Build.NoTarget.ThrowIfAbsent(original); |
| | 1 | 57 | | return original; |
| | | 58 | | } |
| | 1 | 59 | | return proxiedObject; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Extension Method that Wraps an existing object with an Interface of what it is implicitly assigned to. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="originalDynamic">The original dynamic.</param> |
| | | 67 | | /// <param name="otherInterfaces">The other interfaces.</param> |
| | | 68 | | /// <returns></returns> |
| | | 69 | | public static dynamic ActLike(this object originalDynamic, params Type[] otherInterfaces) |
| | 10 | 70 | | => BuildProxy.DefaultProxyMaker.ActLike(originalDynamic, otherInterfaces); |
| | | 71 | | |
| | | 72 | | |
| | | 73 | | public static TInterface Create<TTarget, TInterface>() where TTarget : new() where TInterface : class |
| | 0 | 74 | | => BuildProxy.DefaultProxyMaker.Create<TTarget, TInterface>(); |
| | | 75 | | |
| | | 76 | | public static TInterface Create<TTarget, TInterface>(params object[] args) where TInterface : class |
| | 12 | 77 | | => BuildProxy.DefaultProxyMaker.Create<TTarget, TInterface>(args); |
| | | 78 | | |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Makes static methods for the passed in property spec, designed to be used with old api's that reflect proper |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="originalDynamic">The original dynamic.</param> |
| | | 84 | | /// <param name="propertySpec">The property spec.</param> |
| | | 85 | | /// <returns></returns> |
| | | 86 | | public static dynamic ActLikeProperties(this object originalDynamic, IDictionary<string, Type> propertySpec) |
| | 1 | 87 | | => BuildProxy.DefaultProxyMaker.ActLikeProperties(originalDynamic, propertySpec); |
| | | 88 | | |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Chainable Linq to Objects Method, allows you to wrap a list of objects with an Explict interface defintion |
| | | 92 | | /// </summary> |
| | | 93 | | /// <typeparam name="TInterface">The type of the interface.</typeparam> |
| | | 94 | | /// <param name="originalDynamic">The original dynamic.</param> |
| | | 95 | | /// <param name="otherInterfaces">The other interfaces.</param> |
| | | 96 | | /// <returns></returns> |
| | | 97 | | public static IEnumerable<TInterface> AllActLike<TInterface>(this IEnumerable<object> originalDynamic, params Ty |
| | 1 | 98 | | => BuildProxy.DefaultProxyMaker.AllActLike<TInterface>(originalDynamic, otherInterfaces); |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// |
| | | 102 | | /// Static Method that wraps an existing dynamic object with a explicit interface type |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="originalDynamic">The original dynamic.</param> |
| | | 105 | | /// <param name="otherInterfaces">The other interfaces.</param> |
| | | 106 | | /// <returns></returns> |
| | | 107 | | public static dynamic DynamicActLike(object originalDynamic, params Type[] otherInterfaces) |
| | 7 | 108 | | => BuildProxy.DefaultProxyMaker.DynamicActLike(originalDynamic, otherInterfaces); |
| | | 109 | | |
| | | 110 | | |
| | | 111 | | |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | } |