| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Dynamic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Reflection.Emit; |
| | | 6 | | using System.Runtime.Serialization; |
| | | 7 | | using System.Text; |
| | | 8 | | using ImpromptuInterface.Build; |
| | | 9 | | |
| | | 10 | | namespace ImpromptuInterface |
| | | 11 | | { |
| | | 12 | | |
| | | 13 | | public class ActLikeCaster: DynamicObject |
| | | 14 | | { |
| | | 15 | | public object Target { get; } |
| | | 16 | | private List<Type> _interfaceTypes; |
| | | 17 | | |
| | | 18 | | public ActLikeMaker Maker {get;set;} = BuildProxy.DefaultProxyMaker; |
| | | 19 | | |
| | | 20 | | public override bool TryConvert(System.Dynamic.ConvertBinder binder, out object result) |
| | | 21 | | { |
| | 26 | 22 | | result = null; |
| | | 23 | | |
| | 26 | 24 | | if (binder.Type.IsInterface) |
| | | 25 | | { |
| | 24 | 26 | | _interfaceTypes.Insert(0, binder.Type); |
| | 24 | 27 | | result = Maker.DynamicActLike(Target, _interfaceTypes.ToArray()); |
| | 24 | 28 | | return true; |
| | | 29 | | } |
| | | 30 | | |
| | 2 | 31 | | if(binder.Type.IsInstanceOfType(Target)) |
| | | 32 | | { |
| | 1 | 33 | | result = Target; |
| | | 34 | | } |
| | | 35 | | |
| | 2 | 36 | | return false; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | |
| | 25 | 40 | | public ActLikeCaster(object target, IEnumerable<Type> types) |
| | | 41 | | { |
| | 25 | 42 | | Target = target; |
| | 25 | 43 | | _interfaceTypes = types.ToList(); |
| | 25 | 44 | | } |
| | | 45 | | |
| | | 46 | | } |
| | | 47 | | } |