| | | 1 | | // |
| | | 2 | | // Copyright 2011 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; |
| | | 17 | | using System.Collections.Generic; |
| | | 18 | | using System.Dynamic; |
| | | 19 | | |
| | | 20 | | namespace ImpromptuInterface.Build |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// What a proxy forwards to before it has been given a target: every operation explains |
| | | 24 | | /// that there is nothing to forward to, rather than the proxy binding against null and |
| | | 25 | | /// reporting "Cannot perform runtime binding on a null reference". |
| | | 26 | | /// </summary> |
| | | 27 | | /// <remarks> |
| | | 28 | | /// A stand-in rather than a check on the way in: the emitted members all read the target |
| | | 29 | | /// through <see cref="IActLikeProxy.Original"/> and would be covered by a check there, but |
| | | 30 | | /// <c>Equals</c>, <c>GetHashCode</c>, <c>ToString</c> and <c>GetObjectData</c> read the |
| | | 31 | | /// field directly, and a branch on that property cost about 10% of the time of a forwarded |
| | | 32 | | /// property set. One shared instance, so an un-initialized proxy allocates nothing extra. |
| | | 33 | | /// </remarks> |
| | | 34 | | internal sealed class NoTarget : DynamicObject |
| | | 35 | | { |
| | 1 | 36 | | internal static readonly NoTarget Instance = new NoTarget(); |
| | | 37 | | |
| | 2 | 38 | | private NoTarget() { } |
| | | 39 | | |
| | | 40 | | /// <summary>Throws if <paramref name="target"/> is the stand-in, for a reader that cannot go through it.</summa |
| | | 41 | | internal static void ThrowIfAbsent(object target) |
| | | 42 | | { |
| | 132 | 43 | | if (ReferenceEquals(target, Instance)) |
| | 2 | 44 | | throw Absent(); |
| | 130 | 45 | | } |
| | | 46 | | |
| | | 47 | | private static InvalidOperationException Absent() => |
| | 8 | 48 | | new InvalidOperationException( |
| | 8 | 49 | | "This proxy has no target. A proxy wraps an object and forwards to it, so it cannot be " + |
| | 8 | 50 | | "constructed on its own - Activator.CreateInstance on a proxy type produces one with " + |
| | 8 | 51 | | "nothing to forward to. Use ActLike on the object you want wrapped; for an object with " + |
| | 8 | 52 | | "storage of its own, ActLike over an ExpandoObject."); |
| | | 53 | | |
| | 1 | 54 | | public override bool TryGetMember(GetMemberBinder binder, out object result) => throw Absent(); |
| | 0 | 55 | | public override bool TrySetMember(SetMemberBinder binder, object value) => throw Absent(); |
| | 0 | 56 | | public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) => throw Absen |
| | 0 | 57 | | public override bool TryInvoke(InvokeBinder binder, object[] args, out object result) => throw Absent(); |
| | 0 | 58 | | public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) => throw Absent(); |
| | 0 | 59 | | public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value) => throw Absent(); |
| | 0 | 60 | | public override bool TryDeleteMember(DeleteMemberBinder binder) => throw Absent(); |
| | 0 | 61 | | public override bool TryDeleteIndex(DeleteIndexBinder binder, object[] indexes) => throw Absent(); |
| | 0 | 62 | | public override bool TryConvert(ConvertBinder binder, out object result) => throw Absent(); |
| | 0 | 63 | | public override bool TryUnaryOperation(UnaryOperationBinder binder, out object result) => throw Absent(); |
| | 0 | 64 | | public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result) => throw Ab |
| | 0 | 65 | | public override bool TryCreateInstance(CreateInstanceBinder binder, object[] args, out object result) => throw A |
| | 0 | 66 | | public override IEnumerable<string> GetDynamicMemberNames() => throw Absent(); |
| | | 67 | | |
| | | 68 | | // The proxy's own Equals/GetHashCode/ToString forward to these, so they explain |
| | | 69 | | // themselves too rather than answering for the stand-in. |
| | 1 | 70 | | public override string ToString() => throw Absent(); |
| | 2 | 71 | | public override bool Equals(object obj) => throw Absent(); |
| | 2 | 72 | | public override int GetHashCode() => throw Absent(); |
| | | 73 | | } |
| | | 74 | | } |