| | | 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 | | |
| | | 17 | | using System; |
| | | 18 | | using System.Collections.Generic; |
| | | 19 | | using System.ComponentModel; |
| | | 20 | | using System.Dynamic; |
| | | 21 | | using System.Linq; |
| | | 22 | | using System.Reflection; |
| | | 23 | | using System.Runtime.CompilerServices; |
| | | 24 | | using System.Runtime.Serialization; |
| | | 25 | | using System.Threading; |
| | | 26 | | using Dynamitey; |
| | | 27 | | using Microsoft.CSharp.RuntimeBinder; |
| | | 28 | | |
| | | 29 | | |
| | | 30 | | |
| | | 31 | | namespace ImpromptuInterface.Optimization |
| | | 32 | | { |
| | | 33 | | /// <summary> |
| | | 34 | | /// Utility Class |
| | | 35 | | /// </summary> |
| | | 36 | | public static class Util |
| | | 37 | | { |
| | | 38 | | /// <summary> |
| | | 39 | | /// Determines whether [is anonymous type] [the specified target]. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="target">The target.</param> |
| | | 42 | | /// <returns> |
| | | 43 | | /// <c>true</c> if [is anonymous type] [the specified target]; otherwise, <c>false</c>. |
| | | 44 | | /// </returns> |
| | | 45 | | public static bool IsAnonymousType(object target) |
| | | 46 | | { |
| | 4 | 47 | | if(target ==null) |
| | 1 | 48 | | return false; |
| | | 49 | | |
| | 3 | 50 | | var type = target as Type ?? target.GetType(); |
| | | 51 | | |
| | 3 | 52 | | return type.IsNotPublic |
| | 3 | 53 | | && Attribute.IsDefined( |
| | 3 | 54 | | type, |
| | 3 | 55 | | typeof (CompilerGeneratedAttribute), |
| | 3 | 56 | | false); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | static Util() |
| | | 60 | | { |
| | 1 | 61 | | IsMono = Type.GetType("Mono.Runtime") != null; |
| | | 62 | | |
| | 1 | 63 | | } |
| | | 64 | | |
| | | 65 | | |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the target context. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="target">The target.</param> |
| | | 71 | | /// <param name="context">The context.</param> |
| | | 72 | | /// <param name="staticContext">if set to <c>true</c> [static context].</param> |
| | | 73 | | /// <returns></returns> |
| | | 74 | | public static object GetTargetContext(this object target, out Type context, out bool staticContext) |
| | | 75 | | { |
| | 129 | 76 | | var tInvokeContext = target as InvokeContext; |
| | 129 | 77 | | staticContext = false; |
| | 129 | 78 | | if (tInvokeContext != null) |
| | | 79 | | { |
| | 1 | 80 | | staticContext = tInvokeContext.StaticContext; |
| | 1 | 81 | | context = tInvokeContext.Context; |
| | 1 | 82 | | context = context.FixContext(); |
| | 1 | 83 | | return tInvokeContext.Target; |
| | | 84 | | } |
| | | 85 | | |
| | 128 | 86 | | context = target as Type ?? target.GetType(); |
| | 128 | 87 | | context = context.FixContext(); |
| | 128 | 88 | | return target; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Fixes the context. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="context">The context.</param> |
| | | 96 | | /// <returns></returns> |
| | | 97 | | public static Type FixContext(this Type context) |
| | | 98 | | { |
| | 385 | 99 | | if (context.IsArray) |
| | | 100 | | { |
| | 1 | 101 | | return typeof (object); |
| | | 102 | | } |
| | 384 | 103 | | return context; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | |
| | | 107 | | |
| | | 108 | | #if !SILVERLIGHT |
| | | 109 | | /// <summary> |
| | | 110 | | /// Gets the value. Conveinence Ext method |
| | | 111 | | /// </summary> |
| | | 112 | | /// <typeparam name="T"></typeparam> |
| | | 113 | | /// <param name="info">The info.</param> |
| | | 114 | | /// <param name="name">The name.</param> |
| | | 115 | | /// <returns></returns> |
| | | 116 | | public static T GetValue<T>(this SerializationInfo info, string name) |
| | | 117 | | { |
| | 0 | 118 | | return (T) info.GetValue(name, typeof (T)); |
| | | 119 | | } |
| | | 120 | | #endif |
| | | 121 | | |
| | | 122 | | |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Is Current Runtime Mono? |
| | | 126 | | /// </summary> |
| | | 127 | | public static readonly bool IsMono; |
| | | 128 | | |
| | | 129 | | |
| | | 130 | | } |
| | | 131 | | } |