PageRenderTime 224ms CodeModel.GetById 71ms app.highlight 106ms RepoModel.GetById 14ms app.codeStats 1ms

/Runtime/Microsoft.Dynamic/Utils/ReflectionUtils.cs

http://github.com/IronLanguages/main
C# | 1924 lines | 1475 code | 315 blank | 134 comment | 421 complexity | df3aab61498b713c845676ba6b43ae56 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/* ****************************************************************************
   2 *
   3 * Copyright (c) Microsoft Corporation. 
   4 *
   5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
   6 * copy of the license can be found in the License.html file at the root of this distribution. If 
   7 * you cannot locate the  Apache License, Version 2.0, please send an email to 
   8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
   9 * by the terms of the Apache License, Version 2.0.
  10 *
  11 * You must not remove this notice, or any other, from this software.
  12 *
  13 *
  14 * ***************************************************************************/
  15
  16#if FEATURE_METADATA_READER
  17using Microsoft.Scripting.Metadata;
  18#endif
  19
  20#if !FEATURE_TYPE_INFO
  21using TypeInfo = System.Type;
  22#endif
  23
  24using System;
  25using System.Collections.Generic;
  26using System.Collections.ObjectModel;
  27using System.Diagnostics;
  28using System.Linq;
  29using System.Reflection;
  30using System.Reflection.Emit;
  31using System.Runtime.CompilerServices;
  32using System.Security;
  33using System.Text;
  34using System.Runtime.InteropServices;
  35using System.Dynamic;
  36using System.Linq.Expressions;
  37
  38using Microsoft.Scripting.Generation;
  39using Microsoft.Scripting.Runtime;
  40using Microsoft.Scripting.Utils;
  41
  42#if WIN8 || WP75 || NETSTANDARD
  43namespace System.Runtime.CompilerServices {
  44    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event)]
  45    public sealed class SpecialNameAttribute : Attribute {
  46        public SpecialNameAttribute() {
  47        }
  48    }
  49}
  50#endif
  51
  52#if WIN8
  53namespace System {
  54    public enum TypeCode {
  55        Empty,
  56        Object,
  57        DBNull,
  58        Boolean,
  59        Char,
  60        SByte,
  61        Byte,
  62        Int16,
  63        UInt16,
  64        Int32,
  65        UInt32,
  66        Int64,
  67        UInt64,
  68        Single,
  69        Double,
  70        Decimal,
  71        DateTime,
  72        String = 18
  73    }
  74}
  75
  76namespace System.Reflection {
  77    [Flags]
  78    public enum BindingFlags {
  79        /// <summary>Specifies that instance members are to be included in the search.</summary>
  80        Instance = 4,
  81        /// <summary>Specifies that static members are to be included in the search.</summary>
  82        Static = 8,
  83        /// <summary>Specifies that public members are to be included in the search.</summary>
  84        Public = 16,
  85        /// <summary>Specifies that non-public members are to be included in the search.</summary>
  86        NonPublic = 32
  87    }
  88}
  89#elif !CLR45
  90namespace System.Reflection {
  91    public static class RuntimeReflectionExtensions {
  92        public static MethodInfo GetRuntimeBaseDefinition(this MethodInfo method) {
  93            return method.GetBaseDefinition();
  94        }
  95
  96        public static IEnumerable<MethodInfo> GetRuntimeMethods(this Type type) {
  97            return type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
  98        }
  99
 100#if !FEATURE_GET_METHOD_INFO
 101        public static MethodInfo GetMethodInfo(this Delegate d) {
 102            return d.Method;
 103        }
 104#endif
 105
 106#if !FEATURE_TYPE_INFO
 107        public static InterfaceMapping GetRuntimeInterfaceMap(this Type typeInfo, Type interfaceType) {
 108            return typeInfo.GetInterfaceMap(interfaceType);
 109        }
 110#endif
 111    }
 112}
 113#endif
 114
 115namespace Microsoft.Scripting.Utils {
 116    // CF doesn't support DefaultParameterValue attribute. Define our own, but not in System.Runtime.InteropServices namespace as that would 
 117    // make C# compiler emit the parameter's default value metadata not the attribute itself. The default value metadata are not accessible on CF.
 118#if !FEATURE_DEFAULT_PARAMETER_VALUE
 119    /// <summary>
 120    /// The Default Parameter Value Attribute.
 121    /// </summary>
 122    public sealed class DefaultParameterValueAttribute : Attribute
 123    {
 124        private readonly object _value;
 125
 126        public object Value
 127        {
 128            get { return _value; }
 129        }
 130
 131        /// <summary>
 132        /// The constructor
 133        /// </summary>
 134        /// <param name="value">The value.</param>
 135        public DefaultParameterValueAttribute(object value)
 136        {
 137            _value = value;
 138        }
 139    }
 140
 141#if !ANDROID
 142    [AttributeUsage(AttributeTargets.Parameter, Inherited = false), ComVisible(true)]
 143    public sealed class OptionalAttribute : Attribute {
 144    }
 145#endif
 146#endif
 147
 148    public static class ReflectionUtils {
 149        #region Accessibility
 150
 151        public static BindingFlags AllMembers = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
 152
 153        public static bool IsPublic(this PropertyInfo property) {
 154            return property.GetGetMethod(nonPublic: false) != null
 155                || property.GetSetMethod(nonPublic: false) != null;
 156        }
 157
 158        public static bool IsStatic(this PropertyInfo property) {
 159            var getter = property.GetGetMethod(nonPublic: true);
 160            var setter = property.GetSetMethod(nonPublic: true);
 161
 162            return getter != null && getter.IsStatic
 163                || setter != null && setter.IsStatic;
 164        }
 165
 166        public static bool IsStatic(this EventInfo evnt) {
 167            var add = evnt.GetAddMethod(nonPublic: true);
 168            var remove = evnt.GetRemoveMethod(nonPublic: true);
 169
 170            return add != null && add.IsStatic
 171                || remove != null && remove.IsStatic;
 172        }
 173
 174        public static bool IsPrivate(this PropertyInfo property) {
 175            var getter = property.GetGetMethod(nonPublic: true);
 176            var setter = property.GetSetMethod(nonPublic: true);
 177
 178            return (getter == null || getter.IsPrivate)
 179                && (setter == null || setter.IsPrivate);
 180        }
 181
 182        public static bool IsPrivate(this EventInfo evnt) {
 183            var add = evnt.GetAddMethod(nonPublic: true);
 184            var remove = evnt.GetRemoveMethod(nonPublic: true);
 185
 186            return (add == null || add.IsPrivate)
 187                && (remove == null || remove.IsPrivate);
 188        }
 189
 190        private static bool MatchesFlags(ConstructorInfo member, BindingFlags flags) {
 191            return
 192                ((member.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic) & flags) != 0 &&
 193                ((member.IsStatic ? BindingFlags.Static : BindingFlags.Instance) & flags) != 0;
 194        }
 195
 196        private static bool MatchesFlags(MethodInfo member, BindingFlags flags) {
 197            return
 198                ((member.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic) & flags) != 0 && 
 199                ((member.IsStatic ? BindingFlags.Static : BindingFlags.Instance) & flags) != 0;
 200        }
 201
 202        private static bool MatchesFlags(FieldInfo member, BindingFlags flags) {
 203            return
 204                ((member.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic) & flags) != 0 &&
 205                ((member.IsStatic ? BindingFlags.Static : BindingFlags.Instance) & flags) != 0;
 206        }
 207
 208        private static bool MatchesFlags(PropertyInfo member, BindingFlags flags) {
 209            return
 210                ((member.IsPublic() ? BindingFlags.Public : BindingFlags.NonPublic) & flags) != 0 &&
 211                ((member.IsStatic() ? BindingFlags.Static : BindingFlags.Instance) & flags) != 0;
 212        }
 213
 214        private static bool MatchesFlags(EventInfo member, BindingFlags flags) {
 215            var add = member.GetAddMethod();
 216            var remove = member.GetRemoveMethod();
 217            var raise = member.GetRaiseMethod();
 218
 219            bool isPublic = add != null && add.IsPublic || remove != null && remove.IsPublic || raise != null && raise.IsPublic;
 220            bool isStatic = add != null && add.IsStatic || remove != null && remove.IsStatic || raise != null && raise.IsStatic;
 221
 222            return
 223                ((isPublic ? BindingFlags.Public : BindingFlags.NonPublic) & flags) != 0 &&
 224                ((isStatic ? BindingFlags.Static : BindingFlags.Instance) & flags) != 0;
 225        }
 226
 227        private static bool MatchesFlags(TypeInfo member, BindingFlags flags) {
 228            // Static/Instance are ignored
 229            return (((member.IsPublic || member.IsNestedPublic) ? BindingFlags.Public : BindingFlags.NonPublic) & flags) != 0;
 230        }
 231
 232        private static bool MatchesFlags(MemberInfo member, BindingFlags flags) {
 233            ConstructorInfo ctor;
 234            MethodInfo method;
 235            FieldInfo field;
 236            EventInfo evnt;
 237            PropertyInfo property;
 238
 239            if ((method = member as MethodInfo) != null) {
 240                return MatchesFlags(method, flags);
 241            }
 242
 243            if ((field = member as FieldInfo) != null) {
 244                return MatchesFlags(field, flags);
 245            }
 246
 247            if ((ctor = member as ConstructorInfo) != null) {
 248                return MatchesFlags(ctor, flags);
 249            }
 250
 251            if ((evnt = member as EventInfo) != null) {
 252                return MatchesFlags(evnt, flags);
 253            }
 254
 255            if ((property = member as PropertyInfo) != null) {
 256                return MatchesFlags(property, flags);
 257            }
 258
 259            return MatchesFlags((TypeInfo)member, flags);
 260        }
 261
 262        private static IEnumerable<T> WithBindingFlags<T>(this IEnumerable<T> members, Func<T, BindingFlags, bool> matchFlags, BindingFlags flags)
 263            where T : MemberInfo {
 264            return members.Where(member => matchFlags(member, flags));
 265        }
 266
 267        public static IEnumerable<MemberInfo> WithBindingFlags(this IEnumerable<MemberInfo> members, BindingFlags flags) {
 268            return members.WithBindingFlags(MatchesFlags, flags);
 269        }
 270
 271        public static IEnumerable<MethodInfo> WithBindingFlags(this IEnumerable<MethodInfo> members, BindingFlags flags) {
 272            return members.WithBindingFlags(MatchesFlags, flags);
 273        }
 274
 275        public static IEnumerable<ConstructorInfo> WithBindingFlags(this IEnumerable<ConstructorInfo> members, BindingFlags flags) {
 276            return members.WithBindingFlags(MatchesFlags, flags);
 277        }
 278
 279        public static IEnumerable<FieldInfo> WithBindingFlags(this IEnumerable<FieldInfo> members, BindingFlags flags) {
 280            return members.WithBindingFlags(MatchesFlags, flags);
 281        }
 282
 283        public static IEnumerable<PropertyInfo> WithBindingFlags(this IEnumerable<PropertyInfo> members, BindingFlags flags) {
 284            return members.WithBindingFlags(MatchesFlags, flags);
 285        }
 286
 287        public static IEnumerable<EventInfo> WithBindingFlags(this IEnumerable<EventInfo> members, BindingFlags flags) {
 288            return members.WithBindingFlags(MatchesFlags, flags);
 289        }
 290
 291        public static IEnumerable<TypeInfo> WithBindingFlags(this IEnumerable<TypeInfo> members, BindingFlags flags) {
 292            return members.WithBindingFlags(MatchesFlags, flags);
 293        }
 294
 295        public static MemberInfo WithBindingFlags(this MemberInfo member, BindingFlags flags) {
 296            return member != null && MatchesFlags(member, flags) ? member : null;
 297        }
 298
 299        public static MethodInfo WithBindingFlags(this MethodInfo member, BindingFlags flags) {
 300            return member != null && MatchesFlags(member, flags) ? member : null;
 301        }
 302
 303        public static ConstructorInfo WithBindingFlags(this ConstructorInfo member, BindingFlags flags) {
 304            return member != null && MatchesFlags(member, flags) ? member : null;
 305        }
 306
 307        public static FieldInfo WithBindingFlags(this FieldInfo member, BindingFlags flags) {
 308            return member != null && MatchesFlags(member, flags) ? member : null;
 309        }
 310
 311        public static PropertyInfo WithBindingFlags(this PropertyInfo member, BindingFlags flags) {
 312            return member != null && MatchesFlags(member, flags) ? member : null;
 313        }
 314
 315        public static EventInfo WithBindingFlags(this EventInfo member, BindingFlags flags) {
 316            return member != null && MatchesFlags(member, flags) ? member : null;
 317        }
 318
 319        public static TypeInfo WithBindingFlags(this TypeInfo member, BindingFlags flags) {
 320            return member != null && MatchesFlags(member, flags) ? member : null;
 321        }
 322
 323        #endregion
 324
 325        #region Signatures
 326
 327        public static IEnumerable<MethodInfo> WithSignature(this IEnumerable<MethodInfo> members, Type[] parameterTypes) {
 328            return members.Where(c => {
 329                var ps = c.GetParameters();
 330                if (ps.Length != parameterTypes.Length) {
 331                    return false;
 332                }
 333
 334                for (int i = 0; i < ps.Length; i++) {
 335                    if (parameterTypes[i] != ps[i].ParameterType) {
 336                        return false;
 337                    }
 338                }
 339
 340                return true;
 341            });
 342        }
 343
 344        public static IEnumerable<ConstructorInfo> WithSignature(this IEnumerable<ConstructorInfo> members, Type[] parameterTypes) {
 345            return members.Where(c => {
 346                var ps = c.GetParameters();
 347                if (ps.Length != parameterTypes.Length) {
 348                    return false;
 349                }
 350
 351                for (int i = 0; i < ps.Length; i++) {
 352                    if (parameterTypes[i] != ps[i].ParameterType) {
 353                        return false;
 354                    }
 355                }
 356
 357                return true;
 358            });
 359        }
 360        
 361        #endregion
 362
 363        #region Member Inheritance
 364
 365        // CLI specification, partition I, 8.10.4: Hiding, overriding, and layout
 366        // ----------------------------------------------------------------------
 367        // While hiding applies to all members of a type, overriding deals with object layout and is applicable only to instance fields 
 368        // and virtual methods. The CTS provides two forms of member overriding, new slot and expect existing slot. A member of a derived 
 369        // type that is marked as a new slot will always get a new slot in the object�s layout, guaranteeing that the base field or method 
 370        // is available in the object by using a qualified reference that combines the name of the base type with the name of the member 
 371        // and its type or signature. A member of a derived type that is marked as expect existing slot will re-use (i.e., share or override) 
 372        // a slot that corresponds to a member of the same kind (field or method), name, and type if one already exists from the base type; 
 373        // if no such slot exists, a new slot is allocated and used.
 374        //
 375        // The general algorithm that is used for determining the names in a type and the layout of objects of the type is roughly as follows:
 376        // - Flatten the inherited names (using the hide by name or hide by name-and-signature rule) ignoring accessibility rules. 
 377        // - For each new member that is marked �expect existing slot�, look to see if an exact match on kind (i.e., field or method), 
 378        //   name, and signature exists and use that slot if it is found, otherwise allocate a new slot. 
 379        // - After doing this for all new members, add these new member-kind/name/signatures to the list of members of this type 
 380        // - Finally, remove any inherited names that match the new members based on the hide by name or hide by name-and-signature rules.
 381        
 382        // NOTE: Following GetXxx only implement overriding, not hiding specified by hide-by-name or hide-by-name-and-signature flags.
 383
 384        public static IEnumerable<MethodInfo> GetInheritedMethods(this Type type, string name = null, bool flattenHierarchy = false) {
 385            while (type.IsGenericParameter) {
 386                type = type.GetBaseType();
 387            }
 388
 389            var baseDefinitions = new HashSet<MethodInfo>(ReferenceEqualityComparer<MethodInfo>.Instance);
 390            foreach (var ancestor in type.Ancestors()) {
 391                foreach (var declaredMethod in ancestor.GetDeclaredMethods(name)) {
 392                    if (declaredMethod != null && IncludeMethod(declaredMethod, type, baseDefinitions, flattenHierarchy)) {
 393                        yield return declaredMethod;
 394                    }
 395                }
 396            }
 397        }
 398
 399        private static bool IncludeMethod(MethodInfo member, Type reflectedType, HashSet<MethodInfo> baseDefinitions, bool flattenHierarchy) {
 400            if (member.IsVirtual) {
 401                if (baseDefinitions.Add(RuntimeReflectionExtensions.GetRuntimeBaseDefinition(member))) {
 402                    return true;
 403                }
 404            } else if (member.DeclaringType == reflectedType) {
 405                return true;
 406            } else if (!member.IsPrivate && (!member.IsStatic || flattenHierarchy)) {
 407                return true;
 408            }
 409
 410            return false;
 411        }
 412
 413        public static IEnumerable<PropertyInfo> GetInheritedProperties(this Type type, string name = null, bool flattenHierarchy = false) {
 414            while (type.IsGenericParameter) {
 415                type = type.GetBaseType();
 416            }
 417
 418            var baseDefinitions = new HashSet<MethodInfo>(ReferenceEqualityComparer<MethodInfo>.Instance);
 419            foreach (var ancestor in type.Ancestors()) {
 420                if (name != null) {
 421                    var declaredProperty = ancestor.GetDeclaredProperty(name);
 422                    if (declaredProperty != null && IncludeProperty(declaredProperty, type, baseDefinitions, flattenHierarchy)) {
 423                        yield return declaredProperty;
 424                    }
 425                } else {
 426                    foreach (var declaredProperty in ancestor.GetDeclaredProperties()) {
 427                        if (IncludeProperty(declaredProperty, type, baseDefinitions, flattenHierarchy)) {
 428                            yield return declaredProperty;
 429                        }
 430                    }
 431                }
 432            }
 433        }
 434
 435        // CLI spec 22.34 Properties
 436        // -------------------------
 437        // [Note: The CLS (see Partition I) refers to instance, virtual, and static properties.  
 438        // The signature of a property (from the Type column) can be used to distinguish a static property, 
 439        // since instance and virtual properties will have the �HASTHIS� bit set in the signature (�23.2.1)
 440        // while a static property will not.  The distinction between an instance and a virtual property 
 441        // depends on the signature of the getter and setter methods, which the CLS requires to be either 
 442        // both virtual or both instance. end note]
 443        private static bool IncludeProperty(PropertyInfo member, Type reflectedType, HashSet<MethodInfo> baseDefinitions, bool flattenHierarchy) {
 444            var getter = member.GetGetMethod(nonPublic: true);
 445            var setter = member.GetSetMethod(nonPublic: true);
 446
 447            MethodInfo virtualAccessor;
 448            if (getter != null && getter.IsVirtual) {
 449                virtualAccessor = getter;
 450            } else if (setter != null && setter.IsVirtual) {
 451                virtualAccessor = setter;
 452            } else {
 453                virtualAccessor = null;
 454            }
 455
 456            if (virtualAccessor != null) {
 457                if (baseDefinitions.Add(RuntimeReflectionExtensions.GetRuntimeBaseDefinition(virtualAccessor))) {
 458                    return true;
 459                }
 460            } else if (member.DeclaringType == reflectedType) {
 461                return true;
 462            } else if (!member.IsPrivate() && (!member.IsStatic() || flattenHierarchy)) {
 463                return true;
 464            }
 465
 466            return false;
 467        }
 468
 469        public static IEnumerable<EventInfo> GetInheritedEvents(this Type type, string name = null, bool flattenHierarchy = false) {
 470            while (type.IsGenericParameter) {
 471                type = type.GetBaseType();
 472            }
 473
 474            var baseDefinitions = new HashSet<MethodInfo>(ReferenceEqualityComparer<MethodInfo>.Instance);
 475            foreach (var ancestor in type.Ancestors()) {
 476                if (name != null) {
 477                    var declaredEvent = ancestor.GetDeclaredEvent(name);
 478                    if (declaredEvent != null && IncludeEvent(declaredEvent, type, baseDefinitions, flattenHierarchy)) {
 479                        yield return declaredEvent;
 480                    }
 481                } else {
 482                    foreach (var declaredEvent in ancestor.GetDeclaredEvents()) {
 483                        if (IncludeEvent(declaredEvent, type, baseDefinitions, flattenHierarchy)) {
 484                            yield return declaredEvent;
 485                        }
 486                    }
 487                }
 488            }
 489        }
 490
 491        private static bool IncludeEvent(EventInfo member, Type reflectedType, HashSet<MethodInfo> baseDefinitions, bool flattenHierarchy) {
 492            var add = member.GetAddMethod(nonPublic: true);
 493            var remove = member.GetRemoveMethod(nonPublic: true);
 494
 495            // TOOD: fire method?
 496
 497            MethodInfo virtualAccessor;
 498            if (add != null && add.IsVirtual) {
 499                virtualAccessor = add;
 500            } else if (remove != null && remove.IsVirtual) {
 501                virtualAccessor = remove;
 502            } else {
 503                virtualAccessor = null;
 504            }
 505
 506            if (virtualAccessor != null) {
 507                if (baseDefinitions.Add(RuntimeReflectionExtensions.GetRuntimeBaseDefinition(virtualAccessor))) {
 508                    return true;
 509                }
 510            } else if (member.DeclaringType == reflectedType) {
 511                return true;
 512            } else if (!member.IsPrivate() && (!member.IsStatic() || flattenHierarchy)) {
 513                return true;
 514            }
 515
 516            return false;
 517        }
 518
 519        public static IEnumerable<FieldInfo> GetInheritedFields(this Type type, string name = null, bool flattenHierarchy = false) {
 520            while (type.IsGenericParameter) {
 521                type = type.GetBaseType();
 522            }
 523
 524            foreach (var ancestor in type.Ancestors()) {
 525                if (name != null) {
 526                    var declaredField = ancestor.GetDeclaredField(name);
 527                    if (declaredField != null && IncludeField(declaredField, type, flattenHierarchy)) {
 528                        yield return declaredField;
 529                    }
 530                } else {
 531                    foreach (var declaredField in ancestor.GetDeclaredFields()) {
 532                        if (IncludeField(declaredField, type, flattenHierarchy)) {
 533                            yield return declaredField;
 534                        }
 535                    }
 536                }
 537            }
 538        }
 539        
 540        private static bool IncludeField(FieldInfo member, Type reflectedType, bool flattenHierarchy) {
 541            if (member.DeclaringType == reflectedType) {
 542                return true;
 543            } else if (!member.IsPrivate && (!member.IsStatic || flattenHierarchy)) {
 544                return true;
 545            }
 546
 547            return false;
 548        }
 549
 550        public static IEnumerable<MemberInfo> GetInheritedMembers(this Type type, string name = null, bool flattenHierarchy = false) {
 551            var result =
 552                type.GetInheritedMethods(name, flattenHierarchy).Cast<MethodInfo, MemberInfo>().Concat(
 553                type.GetInheritedProperties(name, flattenHierarchy).Cast<PropertyInfo, MemberInfo>().Concat(
 554                type.GetInheritedEvents(name, flattenHierarchy).Cast<EventInfo, MemberInfo>().Concat(
 555                type.GetInheritedFields(name, flattenHierarchy).Cast<FieldInfo, MemberInfo>())));
 556
 557            if (name == null) {
 558                return result.Concat<MemberInfo>(
 559                    type.GetDeclaredConstructors().Cast<ConstructorInfo, MemberInfo>().Concat(
 560                    type.GetDeclaredNestedTypes().Cast<TypeInfo, MemberInfo>()));
 561            }
 562
 563            var nestedType = type.GetDeclaredNestedType(name);
 564            return (nestedType != null) ? result.Concat(new[] { nestedType }) : result;
 565        }
 566
 567        #endregion
 568
 569        #region Declared Members
 570
 571        public static IEnumerable<ConstructorInfo> GetDeclaredConstructors(this Type type) {
 572#if FEATURE_TYPE_INFO
 573            return type.GetTypeInfo().DeclaredConstructors;
 574#else
 575            return type.GetConstructors(BindingFlags.DeclaredOnly | AllMembers);
 576#endif
 577        }
 578
 579#if WIN8
 580        public static ConstructorInfo GetConstructor(this Type type, Type[] parameterTypes) {
 581            return type.GetDeclaredConstructors().Where(ci => !ci.IsStatic && ci.IsPublic).WithSignature(parameterTypes).SingleOrDefault();
 582        }
 583#endif
 584
 585        public static IEnumerable<MethodInfo> GetDeclaredMethods(this Type type, string name = null) {
 586#if FEATURE_TYPE_INFO
 587            if (name == null) {
 588                return type.GetTypeInfo().DeclaredMethods;
 589            } else {
 590                return type.GetTypeInfo().GetDeclaredMethods(name);
 591            }
 592#else
 593            if (name == null) {
 594                return type.GetMethods(BindingFlags.DeclaredOnly | AllMembers);
 595            } else {
 596                return type.GetMember(name, MemberTypes.Method, BindingFlags.DeclaredOnly | AllMembers).OfType<MethodInfo>();
 597            }
 598#endif
 599        }
 600
 601        public static IEnumerable<PropertyInfo> GetDeclaredProperties(this Type type) {
 602#if FEATURE_TYPE_INFO
 603            return type.GetTypeInfo().DeclaredProperties;
 604#else
 605            return type.GetProperties(BindingFlags.DeclaredOnly | AllMembers);
 606#endif
 607        }
 608
 609        public static PropertyInfo GetDeclaredProperty(this Type type, string name) {
 610            Debug.Assert(name != null);
 611#if FEATURE_TYPE_INFO
 612            return type.GetTypeInfo().GetDeclaredProperty(name);
 613#else
 614            return type.GetProperty(name, BindingFlags.DeclaredOnly | AllMembers);
 615#endif
 616        }
 617
 618        public static IEnumerable<EventInfo> GetDeclaredEvents(this Type type) {
 619#if FEATURE_TYPE_INFO
 620            return type.GetTypeInfo().DeclaredEvents;
 621#else
 622            return type.GetEvents(BindingFlags.DeclaredOnly | AllMembers);
 623#endif
 624        }
 625
 626        public static EventInfo GetDeclaredEvent(this Type type, string name) {
 627            Debug.Assert(name != null);
 628#if FEATURE_TYPE_INFO
 629            return type.GetTypeInfo().GetDeclaredEvent(name);
 630#else
 631            return type.GetEvent(name, BindingFlags.DeclaredOnly | AllMembers);
 632#endif
 633        }
 634
 635        public static IEnumerable<FieldInfo> GetDeclaredFields(this Type type) {
 636#if FEATURE_TYPE_INFO
 637            return type.GetTypeInfo().DeclaredFields;
 638#else
 639            return type.GetFields(BindingFlags.DeclaredOnly | AllMembers);
 640#endif
 641        }
 642
 643        public static FieldInfo GetDeclaredField(this Type type, string name) {
 644            Debug.Assert(name != null);
 645#if FEATURE_TYPE_INFO
 646            return type.GetTypeInfo().GetDeclaredField(name);
 647#else
 648            return type.GetField(name, BindingFlags.DeclaredOnly | AllMembers);
 649#endif
 650        }
 651
 652        public static IEnumerable<TypeInfo> GetDeclaredNestedTypes(this Type type) {
 653#if FEATURE_TYPE_INFO
 654            return type.GetTypeInfo().DeclaredNestedTypes;
 655#else
 656            return type.GetNestedTypes(BindingFlags.DeclaredOnly | AllMembers);
 657#endif
 658        }
 659
 660        public static TypeInfo GetDeclaredNestedType(this Type type, string name) {
 661            Debug.Assert(name != null);
 662#if FEATURE_TYPE_INFO
 663            return type.GetTypeInfo().GetDeclaredNestedType(name);
 664#else
 665            return type.GetNestedType(name, BindingFlags.DeclaredOnly | AllMembers);
 666#endif
 667        }
 668
 669        public static IEnumerable<MemberInfo> GetDeclaredMembers(this Type type, string name = null) {
 670#if WIN8
 671            var info = type.GetTypeInfo();
 672            if (name == null) {
 673                return info.DeclaredMembers;
 674            } else {
 675                return GetDeclaredMembersWithName(info, name);
 676            }
 677#else
 678            if (name == null) {
 679                return type.GetMembers(BindingFlags.DeclaredOnly | AllMembers);
 680            } else {
 681                return type.GetMember(name, BindingFlags.DeclaredOnly | AllMembers);
 682            }
 683#endif
 684        }
 685
 686#if WIN8
 687        private static IEnumerable<MemberInfo> GetDeclaredMembersWithName(TypeInfo info, string name) {
 688            MemberInfo member;
 689
 690            if ((member = info.GetDeclaredMethod(name)) != null) {
 691                yield return member;
 692            }
 693
 694            if ((member = info.GetDeclaredField(name)) != null) {
 695                yield return member;
 696            }
 697
 698            if ((member = info.GetDeclaredProperty(name)) != null) {
 699                yield return member;
 700            }
 701
 702            if ((member = info.GetDeclaredEvent(name)) != null) {
 703                yield return member;
 704            }
 705
 706            if ((member = info.GetDeclaredNestedType(name)) != null) {
 707                yield return member;
 708            }
 709        }
 710#endif
 711
 712        #endregion
 713
 714        #region Win8
 715#if WIN8
 716        public static TypeCode GetTypeCode(this Enum e) {
 717            return GetTypeCode(Enum.GetUnderlyingType(e.GetType()));
 718        }
 719
 720        // TODO: reduce to numeric types?
 721        public static TypeCode GetTypeCode(this Type type) {
 722            if (type == typeof(int)) {
 723                return TypeCode.Int32;
 724            }
 725            if (type == typeof(sbyte)) {
 726                return TypeCode.SByte;
 727            }
 728            if (type == typeof(short)) {
 729                return TypeCode.Int16;
 730            }
 731            if (type == typeof(long)) {
 732                return TypeCode.Int64;
 733            }
 734            if (type == typeof(uint)) {
 735                return TypeCode.UInt32;
 736            }
 737            if (type == typeof(byte)) {
 738                return TypeCode.Byte;
 739            }
 740            if (type == typeof(ushort)) {
 741                return TypeCode.UInt16;
 742            }
 743            if (type == typeof(ulong)) {
 744                return TypeCode.UInt64;
 745            }
 746            if (type == typeof(bool)) {
 747                return TypeCode.Boolean;
 748            }
 749            if (type == typeof(char)) {
 750                return TypeCode.Char;
 751            }
 752
 753            // TODO: do we need this?
 754            if (type == typeof(string)) {
 755                return TypeCode.String;
 756            }
 757            if (type == typeof(bool)) {
 758                return TypeCode.Boolean;
 759            }
 760            if (type == typeof(double)) {
 761                return TypeCode.Double;
 762            }
 763            if (type == typeof(float)) {
 764                return TypeCode.Single;
 765            }
 766            if (type == typeof(decimal)) {
 767                return TypeCode.Decimal;
 768            }
 769            if (type == typeof(DateTime)) {
 770                return TypeCode.DateTime;
 771            }
 772            return TypeCode.Object;
 773        }
 774        public static IEnumerable<Type> GetImplementedInterfaces(this Type type) {
 775            return type.GetTypeInfo().ImplementedInterfaces;
 776        }
 777
 778#if !NETSTANDARD
 779        public static MethodInfo GetGetMethod(this PropertyInfo propertyInfo, bool nonPublic = false) {
 780            var accessor = propertyInfo.GetMethod;
 781            return nonPublic || accessor == null || accessor.IsPublic ? accessor : null;
 782        }
 783
 784        public static MethodInfo GetSetMethod(this PropertyInfo propertyInfo, bool nonPublic = false) {
 785            var accessor = propertyInfo.SetMethod;
 786            return nonPublic || accessor == null || accessor.IsPublic ? accessor : null;
 787        }
 788
 789        public static MethodInfo GetAddMethod(this EventInfo eventInfo, bool nonPublic = false) {
 790            var accessor = eventInfo.AddMethod;
 791            return nonPublic || accessor == null || accessor.IsPublic ? accessor : null;
 792        }
 793
 794        public static MethodInfo GetRemoveMethod(this EventInfo eventInfo, bool nonPublic = false) {
 795            var accessor = eventInfo.RemoveMethod;
 796            return nonPublic || accessor == null || accessor.IsPublic ? accessor : null;
 797        }
 798
 799        public static MethodInfo GetRaiseMethod(this EventInfo eventInfo, bool nonPublic = false) {
 800            var accessor = eventInfo.RaiseMethod;
 801            return nonPublic || accessor == null || accessor.IsPublic ? accessor : null;
 802        }
 803
 804        public static MethodInfo GetMethod(this Type type, string name) {
 805            return type.GetTypeInfo().GetDeclaredMethod(name);
 806        }
 807
 808        // TODO: FlattenHierarchy
 809        // TODO: inherited!
 810        public static MethodInfo GetMethod(this Type type, string name, Type[] parameterTypes) {
 811            return type.GetTypeInfo().GetDeclaredMethods(name).WithSignature(parameterTypes).Single();
 812        }
 813
 814        public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags) {
 815            return type.GetMethods(name, bindingFlags).Single();
 816        }
 817#endif
 818
 819        private static IEnumerable<MethodInfo> GetMethods(this Type type, string name, BindingFlags bindingFlags) {
 820            return type.GetTypeInfo().GetDeclaredMethods(name).WithBindingFlags(bindingFlags);
 821        }
 822
 823
 824        public static Type CreateType(this TypeBuilder builder) {
 825            return builder.CreateTypeInfo().AsType();
 826        }
 827
 828#if !NETSTANDARD
 829        public static object GetRawConstantValue(this FieldInfo field) {
 830            return ((dynamic)field).GetRawConstantValue();
 831        }
 832
 833        public static int GetMetadataToken(this MemberInfo member) {
 834            return ((dynamic)member).MetadataToken;
 835        }
 836#endif
 837
 838        public static Module GetModule(this MemberInfo member) {
 839            return member.Module;
 840        }
 841
 842#if !NETSTANDARD
 843        public static Type[] GetGenericArguments(this Type type) {
 844            return type.GetTypeInfo().GenericTypeArguments;
 845        }
 846
 847        public static bool IsAssignableFrom(this Type type, Type other) {
 848            return type.GetTypeInfo().IsAssignableFrom(other.GetTypeInfo());
 849        }
 850#endif
 851
 852        public static Type[] GetGenericParameterConstraints(this Type type) {
 853            return type.GetTypeInfo().GetGenericParameterConstraints();
 854        }
 855
 856        public static bool IsSubclassOf(this Type type, Type other) {
 857            return type.GetTypeInfo().IsSubclassOf(other);
 858        }
 859
 860#if !NETSTANDARD
 861        public static IEnumerable<Type> GetInterfaces(this Type type) {
 862            return type.GetTypeInfo().ImplementedInterfaces;
 863        }
 864#endif
 865
 866        public static Type[] GetRequiredCustomModifiers(this ParameterInfo parameter) {
 867            return EmptyTypes;
 868        }
 869
 870        public static Type[] GetOptionalCustomModifiers(this ParameterInfo parameter) {
 871            return EmptyTypes;
 872        }
 873
 874        public static IEnumerable<Module> GetModules(this Assembly assembly) {
 875            return assembly.Modules;
 876        }
 877
 878        private static string GetDefaultMemberName(this Type type) {
 879            foreach (var ancestor in type.Ancestors()) {
 880                var attr = ancestor.GetTypeInfo().GetCustomAttributes<DefaultMemberAttribute>().SingleOrDefault();
 881                if (attr != null) {
 882                    return attr.MemberName;
 883                }
 884            }
 885
 886            return null;
 887        }
 888
 889#if !NETSTANDARD
 890        public static IEnumerable<MemberInfo> GetDefaultMembers(this Type type) {
 891            string defaultMemberName = type.GetDefaultMemberName();
 892            if (defaultMemberName != null) {
 893                return type.GetInheritedMembers(defaultMemberName).WithBindingFlags(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
 894            }
 895
 896            return Enumerable.Empty<MemberInfo>();
 897        }
 898#endif
 899#else
 900
 901        public static IEnumerable<Module> GetModules(this Assembly assembly) {
 902            return assembly.GetModules();
 903        }
 904
 905        public static IEnumerable<Type> GetImplementedInterfaces(this Type type) {
 906            return type.GetInterfaces();
 907        }
 908
 909        public static TypeCode GetTypeCode(this Type type) {
 910            return Type.GetTypeCode(type);
 911        }
 912
 913        public static int GetMetadataToken(this MemberInfo member) {
 914            return member.MetadataToken;
 915        }
 916
 917        public static Module GetModule(this MemberInfo member) {
 918            return member.Module;
 919        }
 920
 921        public static bool IsDefined(this Assembly assembly, Type attributeType) {
 922            return assembly.IsDefined(attributeType, false);
 923        }
 924
 925#if !CLR45
 926        public static T GetCustomAttribute<T>(this Assembly assembly, bool inherit = false) where T : Attribute {
 927            return (T)Attribute.GetCustomAttribute(assembly, typeof(T), inherit);
 928        }
 929
 930        public static T GetCustomAttribute<T>(this MemberInfo member, bool inherit = false) where T : Attribute {
 931            return (T)Attribute.GetCustomAttribute(member, typeof(T), inherit);
 932        }
 933
 934        public static IEnumerable<T> GetCustomAttributes<T>(this Assembly assembly, bool inherit = false) where T : Attribute {
 935            return Attribute.GetCustomAttributes(assembly, typeof(T), inherit).Cast<T>();
 936        }
 937
 938        public static IEnumerable<T> GetCustomAttributes<T>(this MemberInfo member, bool inherit = false) where T : Attribute {
 939            return Attribute.GetCustomAttributes(member, typeof(T), inherit).Cast<T>();
 940        }
 941#endif
 942#endif
 943
 944        public static MethodInfo GetMethod(this Delegate d) {
 945            return d.GetMethodInfo();
 946        }
 947
 948        public static bool ContainsGenericParameters(this Type type) {
 949            return type.GetTypeInfo().ContainsGenericParameters;
 950        }
 951
 952        public static bool IsInterface(this Type type) {
 953            return type.GetTypeInfo().IsInterface;
 954        }
 955
 956        public static bool IsClass(this Type type) {
 957            return type.GetTypeInfo().IsClass;
 958        }
 959
 960        public static bool IsGenericType(this Type type) {
 961            return type.GetTypeInfo().IsGenericType;
 962        }
 963
 964        public static bool IsGenericTypeDefinition(this Type type) {
 965            return type.GetTypeInfo().IsGenericTypeDefinition;
 966        }
 967
 968        public static bool IsSealed(this Type type) {
 969            return type.GetTypeInfo().IsSealed;
 970        }
 971
 972        public static bool IsAbstract(this Type type) {
 973            return type.GetTypeInfo().IsAbstract;
 974        }
 975
 976        public static bool IsPublic(this Type type) {
 977            return type.GetTypeInfo().IsPublic;
 978        }
 979
 980        public static bool IsVisible(this Type type) {
 981            return type.GetTypeInfo().IsVisible;
 982        }
 983        
 984        public static Type GetBaseType(this Type type) {
 985            return type.GetTypeInfo().BaseType;
 986        }
 987
 988        public static bool IsValueType(this Type type) {
 989            return type.GetTypeInfo().IsValueType;
 990        }
 991
 992        public static bool IsEnum(this Type type) {
 993            return type.GetTypeInfo().IsEnum;
 994        }
 995
 996        public static bool IsPrimitive(this Type type) {
 997            return type.GetTypeInfo().IsPrimitive;
 998        }
 999
1000        public static GenericParameterAttributes GetGenericParameterAttributes(this Type type) {
1001            return type.GetTypeInfo().GenericParameterAttributes;
1002        }
1003        
1004        public static Type[] EmptyTypes = new Type[0];
1005
1006        #endregion
1007
1008#if FEATURE_REFEMIT
1009#if FEATURE_ASSEMBLYBUILDER_DEFINEDYNAMICASSEMBLY
1010        public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access) {
1011            return AssemblyBuilder.DefineDynamicAssembly(name, access);
1012        }
1013#else
1014        public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access) {
1015            return AppDomain.CurrentDomain.DefineDynamicAssembly(name, access);
1016        }
1017#endif
1018#if !FEATURE_PDBEMIT
1019        public static ModuleBuilder DefineDynamicModule(this AssemblyBuilder assembly, string name, bool emitDebugInfo) {
1020            // ignore the flag
1021            return assembly.DefineDynamicModule(name);
1022        }
1023#endif
1024#endif
1025
1026        #region Signature and Type Formatting
1027
1028        // Generic type names have the arity (number of generic type paramters) appended at the end. 
1029        // For eg. the mangled name of System.List<T> is "List`1". This mangling is done to enable multiple 
1030        // generic types to exist as long as they have different arities.
1031        public const char GenericArityDelimiter = '`';
1032
1033#if !WIN8
1034        public static StringBuilder FormatSignature(StringBuilder result, MethodBase method) {
1035            return FormatSignature(result, method, (t) => t.FullName);
1036        }
1037
1038        public static StringBuilder FormatSignature(StringBuilder result, MethodBase method, Func<Type, string> nameDispenser) {
1039            ContractUtils.RequiresNotNull(result, "result");
1040            ContractUtils.RequiresNotNull(method, "method");
1041            ContractUtils.RequiresNotNull(nameDispenser, "nameDispenser");
1042
1043            MethodInfo methodInfo = method as MethodInfo;
1044            if (methodInfo != null) {
1045                FormatTypeName(result, methodInfo.ReturnType, nameDispenser);
1046                result.Append(' ');
1047            }
1048
1049#if FEATURE_REFEMIT && !NETSTANDARD
1050            MethodBuilder builder = method as MethodBuilder;
1051            if (builder != null) {
1052                result.Append(builder.Signature);
1053                return result;
1054            }
1055
1056            ConstructorBuilder cb = method as ConstructorBuilder;
1057            if (cb != null) {
1058                result.Append(cb.Signature);
1059                return result;
1060            }
1061#endif
1062            FormatTypeName(result, method.DeclaringType, nameDispenser);
1063            result.Append("::");
1064            result.Append(method.Name);
1065
1066            if (!method.IsConstructor) {
1067                FormatTypeArgs(result, method.GetGenericArguments(), nameDispenser);
1068            }
1069
1070            result.Append("(");
1071
1072            if (!method.ContainsGenericParameters) {
1073                ParameterInfo[] ps = method.GetParameters();
1074                for (int i = 0; i < ps.Length; i++) {
1075                    if (i > 0) result.Append(", ");
1076                    FormatTypeName(result, ps[i].ParameterType, nameDispenser);
1077                    if (!System.String.IsNullOrEmpty(ps[i].Name)) {
1078                        result.Append(" ");
1079                        result.Append(ps[i].Name);
1080                    }
1081                }
1082            } else {
1083                result.Append("?");
1084            }
1085
1086            result.Append(")");
1087            return result;
1088        }
1089#endif
1090
1091        public static StringBuilder FormatTypeName(StringBuilder result, Type type) {
1092            return FormatTypeName(result, type, (t) => t.FullName);
1093        }
1094
1095        public static StringBuilder FormatTypeName(StringBuilder result, Type type, Func<Type, string> nameDispenser) {
1096            ContractUtils.RequiresNotNull(result, "result");
1097            ContractUtils.RequiresNotNull(type, "type");
1098            ContractUtils.RequiresNotNull(nameDispenser, "nameDispenser");
1099            
1100            if (type.IsGenericType()) {
1101                Type genType = type.GetGenericTypeDefinition();
1102                string genericName = nameDispenser(genType).Replace('+', '.');
1103                int tickIndex = genericName.IndexOf('`');
1104                result.Append(tickIndex != -1 ? genericName.Substring(0, tickIndex) : genericName);
1105
1106                Type[] typeArgs = type.GetGenericArguments();
1107                if (type.IsGenericTypeDefinition()) {
1108                    result.Append('<');
1109                    result.Append(',', typeArgs.Length - 1);
1110                    result.Append('>');
1111                } else {
1112                    FormatTypeArgs(result, typeArgs, nameDispenser);
1113                }
1114            } else if (type.IsGenericParameter) {
1115                result.Append(type.Name);
1116            } else {
1117                // cut namespace off:
1118                result.Append(nameDispenser(type).Replace('+', '.'));
1119            }
1120            return result;
1121        }
1122
1123        public static StringBuilder FormatTypeArgs(StringBuilder result, Type[] types) {
1124            return FormatTypeArgs(result, types, (t) => t.FullName);
1125        }
1126
1127        public static StringBuilder FormatTypeArgs(StringBuilder result, Type[] types, Func<Type, string> nameDispenser) {
1128            ContractUtils.RequiresNotNull(result, "result");
1129            ContractUtils.RequiresNotNullItems(types, "types");
1130            ContractUtils.RequiresNotNull(nameDispenser, "nameDispenser");
1131            
1132            if (types.Length > 0) {
1133                result.Append("<");
1134
1135                for (int i = 0; i < types.Length; i++) {
1136                    if (i > 0) result.Append(", ");
1137                    FormatTypeName(result, types[i], nameDispenser);
1138                }
1139
1140                result.Append(">");
1141            }
1142            return result;
1143        }
1144
1145        internal static string ToValidTypeName(string str) {
1146            if (String.IsNullOrEmpty(str)) {
1147                return "_";
1148            }
1149
1150            StringBuilder sb = new StringBuilder(str);
1151            for (int i = 0; i < str.Length; i++) {
1152                if (str[i] == '\0' || str[i] == '.' || str[i] == '*' || str[i] == '+' || str[i] == '[' || str[i] == ']' || str[i] == '\\') {
1153                    sb[i] = '_';
1154                }
1155            }
1156            return sb.ToString();
1157        }
1158
1159        public static string GetNormalizedTypeName(Type type) {
1160            string name = type.Name;
1161            if (type.IsGenericType()) {
1162                return GetNormalizedTypeName(name);
1163            }
1164            return name;
1165        }
1166
1167        public static string GetNormalizedTypeName(string typeName) {
1168            Debug.Assert(typeName.IndexOf('.') == -1); // This is the simple name, not the full name
1169            int backtick = typeName.IndexOf(ReflectionUtils.GenericArityDelimiter);
1170            if (backtick != -1) return typeName.Substring(0, backtick);
1171            return typeName;
1172        }
1173
1174        #endregion
1175
1176        #region Delegates and Dynamic Methods
1177
1178#if WP75
1179        /// <summary>
1180        /// Creates an open delegate for the given (dynamic)method.
1181        /// </summary>
1182        public static Delegate CreateDelegate(this MethodInfo methodInfo, Type delegateType) {
1183            return CreateDelegate(methodInfo, delegateType, null);
1184        }
1185
1186        /// <summary>
1187        /// Creates a closed delegate for the given (dynamic)method.
1188        /// </summary>
1189        public static Delegate CreateDelegate(this MethodInfo methodInfo, Type delegateType, object target) {
1190            return Delegate.CreateDelegate(delegateType, target, methodInfo);
1191        }
1192#elif !WIN8 && !NETSTANDARD
1193        /// <summary>
1194        /// Creates an open delegate for the given (dynamic)method.
1195        /// </summary>
1196        public static Delegate CreateDelegate(this MethodInfo methodInfo, Type delegateType) {
1197            return CreateDelegate(methodInfo, delegateType, null);
1198        }
1199
1200        /// <summary>
1201        /// Creates a closed delegate for the given (dynamic)method.
1202        /// </summary>
1203        public static Delegate CreateDelegate(this MethodInfo methodInfo, Type delegateType, object target) {
1204            DynamicMethod dm = methodInfo as DynamicMethod;
1205            if (dm != null) {
1206                return dm.CreateDelegate(delegateType, target);
1207            } else {
1208                return Delegate.CreateDelegate(delegateType, target, methodInfo);
1209            }
1210        }
1211#endif
1212
1213        public static bool IsDynamicMethod(MethodBase method) {
1214            return !PlatformAdaptationLayer.IsCompactFramework && IsDynamicMethodInternal(method);
1215        }
1216
1217        [MethodImpl(MethodImplOptions.NoInlining)]
1218        private static bool IsDynamicMethodInternal(MethodBase method) {
1219            return method is DynamicMethod;
1220        }
1221
1222        public static void GetDelegateSignature(Type delegateType, out ParameterInfo[] parameterInfos, out ParameterInfo returnInfo) {
1223            ContractUtils.RequiresNotNull(delegateType, "delegateType");
1224
1225            MethodInfo invokeMethod = delegateType.GetMethod("Invoke");
1226            ContractUtils.Requires(invokeMethod != null, "delegateType", Strings.InvalidDelegate);
1227
1228            parameterInfos = invokeMethod.GetParameters();
1229            returnInfo = invokeMethod.ReturnParameter;
1230        }
1231
1232        /// <summary>
1233        /// Gets a Func of CallSite, object * paramCnt, object delegate type
1234        /// that's suitable for use in a non-strongly typed call site.
1235        /// </summary>
1236        public static Type GetObjectCallSiteDelegateType(int paramCnt) {
1237            switch (paramCnt) {
1238                case 0: return typeof(Func<CallSite, object, object>);
1239                case 1: return typeof(Func<CallSite, object, object, object>);
1240                case 2: return typeof(Func<CallSite, object, object, object, object>);
1241                case 3: return typeof(Func<CallSite, object, object, object, object, object>);
1242                case 4: return typeof(Func<CallSite, object, object, object, object, object, object>);
1243                case 5: return typeof(Func<CallSite, object, object, object, object, object, object, object>);
1244                case 6: return typeof(Func<CallSite, object, object, object, object, object, object, object, object>);
1245                case 7: return typeof(Func<CallSite, object, object, object, object, object, object, object, object, object>);
1246                case 8: return typeof(Func<CallSite, object, object, object, object, object, object, object, object, object, object>);
1247                case 9: return typeof(Func<CallSite, object, object, object, object, object, object, object, object, object, object, object>);
1248                case 10: return typeof(Func<CallSite, object, object, object, object, object, object, object, object, object, object, object, object>);
1249                case 11: return typeof(Func<CallSite, object, object, object, object, object, object, object, o

Large files files are truncated, but you can click here to view the full file