GetGenericTypeDefinition() public method

public GetGenericTypeDefinition ( ) : Type
return Type
        internal static void GetUnitySerializationInfo(SerializationInfo info, RuntimeType type)
        {
            if (type.GetRootElementType().IsGenericParameter)
            {
                type = AddElementTypes(info, type);
                info.SetType(typeof(UnitySerializationHolder));
                info.AddValue("UnityType", GenericParameterTypeUnity);
                info.AddValue("GenericParameterPosition", type.GenericParameterPosition);
                info.AddValue("DeclaringMethod", type.DeclaringMethod, typeof(MethodBase));
                info.AddValue("DeclaringType", type.DeclaringType, typeof(Type));

                return;
            }

            int unityType = RuntimeTypeUnity;

            if (!type.IsGenericTypeDefinition && type.ContainsGenericParameters)
            {
                // Partial instantiation
                unityType = PartialInstantiationTypeUnity;
                type      = AddElementTypes(info, type);
                info.AddValue("GenericArguments", type.GetGenericArguments(), typeof(Type[]));
                type = (RuntimeType)type.GetGenericTypeDefinition();
            }

            GetUnitySerializationInfo(info, unityType, type.FullName, type.GetRuntimeAssembly());
        }
Beispiel #2
0
 protected virtual MethodInfo GetMethodImpl()
 {
     if ((this._methodBase == null) || !(this._methodBase is MethodInfo))
     {
         IRuntimeMethodInfo method        = this.FindMethodHandle();
         RuntimeType        declaringType = RuntimeMethodHandle.GetDeclaringType(method);
         if ((RuntimeTypeHandle.IsGenericTypeDefinition(declaringType) || RuntimeTypeHandle.HasInstantiation(declaringType)) && ((RuntimeMethodHandle.GetAttributes(method) & MethodAttributes.Static) == MethodAttributes.PrivateScope))
         {
             if (!(this._methodPtrAux == IntPtr.Zero))
             {
                 declaringType = (RuntimeType)base.GetType().GetMethod("Invoke").GetParameters()[0].ParameterType;
             }
             else
             {
                 Type type = this._target.GetType();
                 Type genericTypeDefinition = declaringType.GetGenericTypeDefinition();
                 while (true)
                 {
                     if (type.IsGenericType && (type.GetGenericTypeDefinition() == genericTypeDefinition))
                     {
                         break;
                     }
                     type = type.BaseType;
                 }
                 declaringType = type as RuntimeType;
             }
         }
         this._methodBase = (MethodInfo)RuntimeType.GetMethodBase(declaringType, method);
     }
     return((MethodInfo)this._methodBase);
 }
Beispiel #3
0
        protected virtual MethodInfo GetMethodImpl()
        {
            if ((_methodBase == null) || !(_methodBase is MethodInfo))
            {
                IRuntimeMethodInfo method        = FindMethodHandle();
                RuntimeType        declaringType = RuntimeMethodHandle.GetDeclaringType(method);
                // need a proper declaring type instance method on a generic type
                if (RuntimeTypeHandle.IsGenericTypeDefinition(declaringType) || RuntimeTypeHandle.HasInstantiation(declaringType))
                {
                    bool isStatic = (RuntimeMethodHandle.GetAttributes(method) & MethodAttributes.Static) != (MethodAttributes)0;
                    if (!isStatic)
                    {
                        if (_methodPtrAux == IntPtr.Zero)
                        {
                            // The target may be of a derived type that doesn't have visibility onto the
                            // target method. We don't want to call RuntimeType.GetMethodBase below with that
                            // or reflection can end up generating a MethodInfo where the ReflectedType cannot
                            // see the MethodInfo itself and that breaks an important invariant. But the
                            // target type could include important generic type information we need in order
                            // to work out what the exact instantiation of the method's declaring type is. So
                            // we'll walk up the inheritance chain (which will yield exactly instantiated
                            // types at each step) until we find the declaring type. Since the declaring type
                            // we get from the method is probably shared and those in the hierarchy we're
                            // walking won't be we compare using the generic type definition forms instead.
                            Type currentType = _target.GetType();
                            Type targetType  = declaringType.GetGenericTypeDefinition();
                            while (currentType != null)
                            {
                                if (currentType.IsGenericType &&
                                    currentType.GetGenericTypeDefinition() == targetType)
                                {
                                    declaringType = currentType as RuntimeType;
                                    break;
                                }
                                currentType = currentType.BaseType;
                            }

                            // RCWs don't need to be "strongly-typed" in which case we don't find a base type
                            // that matches the declaring type of the method. This is fine because interop needs
                            // to work with exact methods anyway so declaringType is never shared at this point.
                            Debug.Assert(currentType != null || _target.GetType().IsCOMObject, "The class hierarchy should declare the method");
                        }
                        else
                        {
                            // it's an open one, need to fetch the first arg of the instantiation
                            MethodInfo invoke = this.GetType().GetMethod("Invoke");
                            declaringType = (RuntimeType)invoke.GetParameters()[0].ParameterType;
                        }
                    }
                }
                _methodBase = (MethodInfo)RuntimeType.GetMethodBase(declaringType, method);
            }
            return((MethodInfo)_methodBase);
        }
 internal static void GetUnitySerializationInfo(SerializationInfo info, RuntimeType type)
 {
     if (type.GetRootElementType().IsGenericParameter)
     {
         type = UnitySerializationHolder.AddElementTypes(info, type);
         info.SetType(typeof(UnitySerializationHolder));
         info.AddValue("UnityType", 7);
         info.AddValue("GenericParameterPosition", type.GenericParameterPosition);
         info.AddValue("DeclaringMethod", (object)type.DeclaringMethod, typeof(MethodBase));
         info.AddValue("DeclaringType", (object)type.DeclaringType, typeof(Type));
     }
     else
     {
         int unityType = 4;
         if (!type.IsGenericTypeDefinition && type.ContainsGenericParameters)
         {
             unityType = 8;
             type      = UnitySerializationHolder.AddElementTypes(info, type);
             info.AddValue("GenericArguments", (object)type.GetGenericArguments(), typeof(Type[]));
             type = (RuntimeType)type.GetGenericTypeDefinition();
         }
         UnitySerializationHolder.GetUnitySerializationInfo(info, unityType, type.FullName, type.GetRuntimeAssembly());
     }
 }
Beispiel #5
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static Attribute[] GetCustomAttributes(RuntimeType type, RuntimeType caType, bool includeSecCa, out int count)
        {
            Contract.Requires(type != null);
            Contract.Requires(caType != null);

            count = 0;

            bool all = caType == (RuntimeType)typeof(object) || caType == (RuntimeType)typeof(Attribute);
            if (!all && s_pca.GetValueOrDefault(caType) == null && !IsSecurityAttribute(caType))
                return new Attribute[0];

            List<Attribute> pcas = new List<Attribute>();
            Attribute pca = null;

#if FEATURE_SERIALIZATION
            if (all || caType == (RuntimeType)typeof(SerializableAttribute))
            {
                pca = SerializableAttribute.GetCustomAttribute(type);
                if (pca != null) pcas.Add(pca);
            }
#endif 
            if (all || caType == (RuntimeType)typeof(ComImportAttribute))
            {
                pca = ComImportAttribute.GetCustomAttribute(type);
                if (pca != null) pcas.Add(pca);
            }
            if (includeSecCa && (all || IsSecurityAttribute(caType)))
            {
                if (!type.IsGenericParameter && type.GetElementType() == null)
                {
                    if (type.IsGenericType)
                        type = (RuntimeType)type.GetGenericTypeDefinition();

                    object[] securityAttributes;
                    GetSecurityAttributes(type.Module.ModuleHandle.GetRuntimeModule(), type.MetadataToken, false, out securityAttributes);
                    if (securityAttributes != null)
                        foreach (object a in securityAttributes)
                            if (caType == a.GetType() || a.GetType().IsSubclassOf(caType))
                                pcas.Add((Attribute)a);
                }
            }

            count = pcas.Count;
            return pcas.ToArray();
        }
Beispiel #6
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static Object[] GetCustomAttributes(RuntimeType type, RuntimeType caType, bool inherit)
        {
            Contract.Requires(type != null);
            Contract.Requires(caType != null);

            if (type.GetElementType() != null) 
                return (caType.IsValueType) ? EmptyArray<Object>.Value : CreateAttributeArrayHelper(caType, 0);

            if (type.IsGenericType && !type.IsGenericTypeDefinition)
                type = type.GetGenericTypeDefinition() as RuntimeType;

            int pcaCount = 0;
            Attribute[] pca = PseudoCustomAttribute.GetCustomAttributes(type, caType, true, out pcaCount);

            // if we are asked to go up the hierarchy chain we have to do it now and regardless of the
            // attribute usage for the specific attribute because a derived attribute may override the usage...           
            // ... however if the attribute is sealed we can rely on the attribute usage
            if (!inherit || (caType.IsSealed && !CustomAttribute.GetAttributeUsage(caType).Inherited))
            {
                object[] attributes = GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, pcaCount, caType, !AllowCriticalCustomAttributes(type));
                if (pcaCount > 0) Array.Copy(pca, 0, attributes, attributes.Length - pcaCount, pcaCount);
                return attributes;
            }

            List<object> result = new List<object>();
            bool mustBeInheritable = false;
            bool useObjectArray = (caType == null || caType.IsValueType || caType.ContainsGenericParameters);
            Type arrayType = useObjectArray ? typeof(object) : caType;

            while (pcaCount > 0)
                result.Add(pca[--pcaCount]);

            while (type != (RuntimeType)typeof(object) && type != null)
            {
                object[] attributes = GetCustomAttributes(type.GetRuntimeModule(), type.MetadataToken, 0, caType, mustBeInheritable, result, !AllowCriticalCustomAttributes(type));
                mustBeInheritable = true;
                for (int i = 0; i < attributes.Length; i++)
                    result.Add(attributes[i]);

                type = type.BaseType as RuntimeType;
            }

            object[] typedResult = CreateAttributeArrayHelper(arrayType, result.Count);
            Array.Copy(result.ToArray(), 0, typedResult, 0, result.Count);
            return typedResult;
        }
        internal static void GetUnitySerializationInfo(SerializationInfo info, RuntimeType type)
        {
            if (type.GetRootElementType().IsGenericParameter)
            {
                type = AddElementTypes(info, type);
                info.SetType(typeof(UnitySerializationHolder));
                info.AddValue("UnityType", GenericParameterTypeUnity);
                info.AddValue("GenericParameterPosition", type.GenericParameterPosition);
                info.AddValue("DeclaringMethod", type.DeclaringMethod, typeof(MethodBase));
                info.AddValue("DeclaringType", type.DeclaringType, typeof(Type));

                return;
            }

            int unityType = RuntimeTypeUnity;

            if (!type.IsGenericTypeDefinition && type.ContainsGenericParameters)
            {
                // Partial instantiation
                unityType = PartialInstantiationTypeUnity;
                type = AddElementTypes(info, type);
                info.AddValue("GenericArguments", type.GetGenericArguments(), typeof(Type[]));
                type = (RuntimeType)type.GetGenericTypeDefinition();
            }

            GetUnitySerializationInfo(info, unityType, type.FullName, type.GetRuntimeAssembly());
        }
Beispiel #8
0
 internal static object[] GetCustomAttributes(RuntimeType type, RuntimeType caType, bool inherit)
 {
     if (type.GetElementType() != null)
     {
         if (!caType.IsValueType)
         {
             return (object[]) Array.CreateInstance(caType, 0);
         }
         return new object[0];
     }
     if (type.IsGenericType && !type.IsGenericTypeDefinition)
     {
         type = type.GetGenericTypeDefinition() as RuntimeType;
     }
     int count = 0;
     Attribute[] sourceArray = PseudoCustomAttribute.GetCustomAttributes(type, caType, true, out count);
     if (!inherit || (caType.IsSealed && !GetAttributeUsage(caType).Inherited))
     {
         object[] objArray = GetCustomAttributes(type.Module, type.MetadataToken, count, caType);
         if (count > 0)
         {
             Array.Copy(sourceArray, 0, objArray, objArray.Length - count, count);
         }
         return objArray;
     }
     List<object> derivedAttributes = new List<object>();
     bool mustBeInheritable = false;
     Type elementType = (((caType == null) || caType.IsValueType) || caType.ContainsGenericParameters) ? typeof(object) : caType;
     while (count > 0)
     {
         derivedAttributes.Add(sourceArray[--count]);
     }
     while ((type != typeof(object)) && (type != null))
     {
         object[] objArray2 = GetCustomAttributes(type.Module, type.MetadataToken, 0, caType, mustBeInheritable, derivedAttributes);
         mustBeInheritable = true;
         for (int i = 0; i < objArray2.Length; i++)
         {
             derivedAttributes.Add(objArray2[i]);
         }
         type = type.BaseType as RuntimeType;
     }
     object[] destinationArray = Array.CreateInstance(elementType, derivedAttributes.Count) as object[];
     Array.Copy(derivedAttributes.ToArray(), 0, destinationArray, 0, derivedAttributes.Count);
     return destinationArray;
 }
        internal static Attribute[] GetCustomAttributes(RuntimeType type, Type caType, bool includeSecCa, out int count) 
        {
            ASSERT.PRECONDITION(type != null);
            ASSERT.PRECONDITION(caType != null);

            count = 0;

            bool all = caType == typeof(object) || caType == typeof(Attribute);
            if (!all && s_pca[caType]  == null && !IsSecurityAttribute(caType))
                return new Attribute[0];

            List<Attribute> pcas = new List<Attribute>();
            Attribute pca = null;

            if (all || caType == typeof(SerializableAttribute))
            {               
                pca = SerializableAttribute.GetCustomAttribute(type);
                if (pca != null) pcas.Add(pca);
            }
            if (all || caType == typeof(ComImportAttribute))
            {               
                pca = ComImportAttribute.GetCustomAttribute(type);
                if (pca != null) pcas.Add(pca);
            }
            if (includeSecCa && (all || IsSecurityAttribute(caType)))
            {               
                if (!type.IsGenericParameter)
                {
                    if (type.IsGenericType)
                        type = (RuntimeType)type.GetGenericTypeDefinition();
                    
                    object[] securityAttributes;
                    GetSecurityAttributes(type.Module.ModuleHandle, type.MetadataToken, out securityAttributes);
                    if (securityAttributes != null)
                        foreach(object a in securityAttributes)
                            if (caType == a.GetType() || a.GetType().IsSubclassOf(caType))
                                pcas.Add((Attribute)a);
                }
            }

            count = pcas.Count;
            return pcas.ToArray();
        }
 internal static Attribute[] GetCustomAttributes(RuntimeType type, RuntimeType caType, bool includeSecCa, out int count)
 {
     count = 0;
     bool flag = (caType == ((RuntimeType) typeof(object))) || (caType == ((RuntimeType) typeof(Attribute)));
     if ((!flag && (s_pca.GetValueOrDefault(caType) == null)) && !IsSecurityAttribute(caType))
     {
         return new Attribute[0];
     }
     List<Attribute> list = new List<Attribute>();
     Attribute item = null;
     if (flag || (caType == ((RuntimeType) typeof(SerializableAttribute))))
     {
         item = SerializableAttribute.GetCustomAttribute(type);
         if (item != null)
         {
             list.Add(item);
         }
     }
     if (flag || (caType == ((RuntimeType) typeof(ComImportAttribute))))
     {
         item = ComImportAttribute.GetCustomAttribute(type);
         if (item != null)
         {
             list.Add(item);
         }
     }
     if ((includeSecCa && (flag || IsSecurityAttribute(caType))) && (!type.IsGenericParameter && (type.GetElementType() == null)))
     {
         object[] objArray;
         if (type.IsGenericType)
         {
             type = (RuntimeType) type.GetGenericTypeDefinition();
         }
         GetSecurityAttributes(type.Module.ModuleHandle.GetRuntimeModule(), type.MetadataToken, false, out objArray);
         if (objArray != null)
         {
             foreach (object obj2 in objArray)
             {
                 if ((caType == obj2.GetType()) || obj2.GetType().IsSubclassOf(caType))
                 {
                     list.Add((Attribute) obj2);
                 }
             }
         }
     }
     count = list.Count;
     return list.ToArray();
 }