1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
5 using System.Collections.Generic;
\r
6 using System.Diagnostics.CodeAnalysis;
\r
8 using System.Reflection;
\r
9 using System.Runtime.InteropServices;
\r
11 namespace Microsoft.Internal
\r
13 internal static class ReflectionServices
\r
15 public static Assembly Assembly(this MemberInfo member)
\r
17 Type type = member as Type;
\r
20 return type.Assembly;
\r
23 return member.DeclaringType.Assembly;
\r
26 public static bool IsVisible(this ConstructorInfo constructor)
\r
28 return constructor.DeclaringType.IsVisible && constructor.IsPublic;
\r
31 public static bool IsVisible(this FieldInfo field)
\r
33 return field.DeclaringType.IsVisible && field.IsPublic;
\r
36 public static bool IsVisible(this MethodInfo method)
\r
38 if (!method.DeclaringType.IsVisible)
\r
41 if (!method.IsPublic)
\r
44 if (method.IsGenericMethod)
\r
46 // Check type arguments, for example if we're passed 'Activator.CreateInstance<SomeMefInternalType>()'
\r
47 foreach (Type typeArgument in method.GetGenericArguments())
\r
49 if (!typeArgument.IsVisible)
\r
57 public static string GetDisplayName(Type declaringType, string name)
\r
59 Assumes.NotNull(declaringType);
\r
61 return declaringType.GetDisplayName() + "." + name;
\r
64 public static string GetDisplayName(this MemberInfo member)
\r
66 Assumes.NotNull(member);
\r
68 switch (member.MemberType)
\r
70 case MemberTypes.TypeInfo:
\r
71 case MemberTypes.NestedType:
\r
72 return ((Type)member).FullName;
\r
75 return GetDisplayName(member.DeclaringType, member.Name);
\r
78 internal static bool TryGetGenericInterfaceType(Type instanceType, Type targetOpenInterfaceType, out Type targetClosedInterfaceType)
\r
80 // The interface must be open
\r
81 Assumes.IsTrue(targetOpenInterfaceType.IsInterface);
\r
82 Assumes.IsTrue(targetOpenInterfaceType.IsGenericTypeDefinition);
\r
83 Assumes.IsTrue(!instanceType.IsGenericTypeDefinition);
\r
85 // if instanceType is an interface, we must first check it directly
\r
86 if (instanceType.IsInterface &&
\r
87 instanceType.IsGenericType &&
\r
88 instanceType.GetGenericTypeDefinition() == targetOpenInterfaceType)
\r
90 targetClosedInterfaceType = instanceType;
\r
96 // Purposefully not using FullName here because it results in a significantly
\r
97 // more expensive implementation of GetInterface, this does mean that we're
\r
98 // takign the chance that there aren't too many types which implement multiple
\r
99 // interfaces by the same name...
\r
100 Type targetInterface = instanceType.GetInterface(targetOpenInterfaceType.Name, false);
\r
101 if (targetInterface != null &&
\r
102 targetInterface.GetGenericTypeDefinition() == targetOpenInterfaceType)
\r
104 targetClosedInterfaceType = targetInterface;
\r
108 catch (AmbiguousMatchException)
\r
110 // If there are multiple with the same name we should not pick any
\r
113 targetClosedInterfaceType = null;
\r
117 internal static IEnumerable<PropertyInfo> GetAllProperties(this Type type)
\r
119 return type.GetInterfaces().Concat(new Type[] { type }).SelectMany(itf => itf.GetProperties());
\r