HasElementType() public static méthode

This method is not implemented by MS runtime for dynamic types
public static HasElementType ( System.TypeSpec t ) : bool
t System.TypeSpec
Résultat bool
Exemple #1
0
        public void SymbolRelatedToPreviousError(Type type)
        {
            if (reporting_disabled > 0 || !printer.HasRelatedSymbolSupport)
            {
                return;
            }

            type = TypeManager.DropGenericTypeArguments(type);

            if (TypeManager.IsGenericParameter(type))
            {
                TypeParameter tp = TypeManager.LookupTypeParameter(type);
                if (tp != null)
                {
                    SymbolRelatedToPreviousError(tp.Location, "");
                    return;
                }
            }

            if (type is TypeBuilder)
            {
                DeclSpace temp_ds = TypeManager.LookupDeclSpace(type);
                SymbolRelatedToPreviousError(temp_ds.Location, TypeManager.CSharpName(type));
            }
            else if (TypeManager.HasElementType(type))
            {
                SymbolRelatedToPreviousError(TypeManager.GetElementType(type));
            }
            else
            {
                SymbolRelatedToPreviousError(type.Assembly.Location, TypeManager.CSharpName(type));
            }
        }
Exemple #2
0
        //
        // Checks whether the type P is as accessible as this member
        //
        public bool IsAccessibleAs(TypeSpec p)
        {
            //
            // if M is private, its accessibility is the same as this declspace.
            // we already know that P is accessible to T before this method, so we
            // may return true.
            //
            if ((mod_flags & Modifiers.PRIVATE) != 0)
            {
                return(true);
            }

            while (TypeManager.HasElementType(p))
            {
                p = TypeManager.GetElementType(p);
            }

            if (p.IsGenericParameter)
            {
                return(true);
            }

            for (TypeSpec p_parent; p != null; p = p_parent)
            {
                p_parent = p.DeclaringType;

                if (p.IsGeneric)
                {
                    foreach (TypeSpec t in p.TypeArguments)
                    {
                        if (!IsAccessibleAs(t))
                        {
                            return(false);
                        }
                    }
                }

                var pAccess = p.Modifiers & Modifiers.AccessibilityMask;
                if (pAccess == Modifiers.PUBLIC)
                {
                    continue;
                }

                bool same_access_restrictions = false;
                for (MemberCore mc = this; !same_access_restrictions && mc != null && mc.Parent != null; mc = mc.Parent)
                {
                    var al = mc.ModFlags & Modifiers.AccessibilityMask;
                    switch (pAccess)
                    {
                    case Modifiers.INTERNAL:
                        if (al == Modifiers.PRIVATE || al == Modifiers.INTERNAL)
                        {
                            same_access_restrictions = p.MemberDefinition.IsInternalAsPublic(mc.Module.DeclaringAssembly);
                        }

                        break;

                    case Modifiers.PROTECTED:
                        if (al == Modifiers.PROTECTED)
                        {
                            same_access_restrictions = mc.Parent.PartialContainer.IsBaseTypeDefinition(p_parent);
                            break;
                        }

                        if (al == Modifiers.PRIVATE)
                        {
                            //
                            // When type is private and any of its parents derives from
                            // protected type then the type is accessible
                            //
                            while (mc.Parent != null && mc.Parent.PartialContainer != null)
                            {
                                if (mc.Parent.PartialContainer.IsBaseTypeDefinition(p_parent))
                                {
                                    same_access_restrictions = true;
                                }
                                mc = mc.Parent;
                            }
                        }

                        break;

                    case Modifiers.PROTECTED | Modifiers.INTERNAL:
                        if (al == Modifiers.INTERNAL)
                        {
                            same_access_restrictions = p.MemberDefinition.IsInternalAsPublic(mc.Module.DeclaringAssembly);
                        }
                        else if (al == (Modifiers.PROTECTED | Modifiers.INTERNAL))
                        {
                            same_access_restrictions = mc.Parent.PartialContainer.IsBaseTypeDefinition(p_parent) && p.MemberDefinition.IsInternalAsPublic(mc.Module.DeclaringAssembly);
                        }
                        else
                        {
                            goto case Modifiers.PROTECTED;
                        }

                        break;

                    case Modifiers.PRIVATE:
                        //
                        // Both are private and share same parent
                        //
                        if (al == Modifiers.PRIVATE)
                        {
                            var decl = mc.Parent;
                            do
                            {
                                same_access_restrictions = decl.CurrentType.MemberDefinition == p_parent.MemberDefinition;
                            } while (!same_access_restrictions && !decl.PartialContainer.IsTopLevel && (decl = decl.Parent) != null);
                        }

                        break;

                    default:
                        throw new InternalErrorException(al.ToString());
                    }
                }

                if (!same_access_restrictions)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #3
0
        public void ApplyAttributeBuilder(Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
        {
            if (a.IsValidSecurityAttribute())
            {
                a.ExtractSecurityPermissionSet(ctor, ref declarative_security);
                return;
            }

            if (a.Type == pa.AssemblyCulture)
            {
                string value = a.GetString();
                if (value == null || value.Length == 0)
                {
                    return;
                }

                if (Compiler.Settings.Target == Target.Exe)
                {
                    a.Error_AttributeEmitError("The executables cannot be satelite assemblies, remove the attribute or keep it empty");
                    return;
                }

                if (value == "neutral")
                {
                    value = "";
                }

                if (Compiler.Settings.Target == Target.Module)
                {
                    SetCustomAttribute(ctor, cdata);
                }
                else
                {
                    builder_extra.SetCulture(value, a.Location);
                }

                IsSatelliteAssembly = true;
                return;
            }

            if (a.Type == pa.AssemblyVersion)
            {
                string value = a.GetString();
                if (value == null || value.Length == 0)
                {
                    return;
                }

                var vinfo = IsValidAssemblyVersion(value, true);
                if (vinfo == null)
                {
                    a.Error_AttributeEmitError(string.Format("Specified version `{0}' is not valid", value));
                    return;
                }

                if (Compiler.Settings.Target == Target.Module)
                {
                    SetCustomAttribute(ctor, cdata);
                }
                else
                {
                    builder_extra.SetVersion(vinfo, a.Location);
                }

                return;
            }

            if (a.Type == pa.AssemblyAlgorithmId)
            {
                const int pos = 2;                 // skip CA header
                uint      alg = (uint)cdata [pos];
                alg |= ((uint)cdata [pos + 1]) << 8;
                alg |= ((uint)cdata [pos + 2]) << 16;
                alg |= ((uint)cdata [pos + 3]) << 24;

                if (Compiler.Settings.Target == Target.Module)
                {
                    SetCustomAttribute(ctor, cdata);
                }
                else
                {
                    builder_extra.SetAlgorithmId(alg, a.Location);
                }

                return;
            }

            if (a.Type == pa.AssemblyFlags)
            {
                const int pos   = 2;               // skip CA header
                uint      flags = (uint)cdata[pos];
                flags |= ((uint)cdata [pos + 1]) << 8;
                flags |= ((uint)cdata [pos + 2]) << 16;
                flags |= ((uint)cdata [pos + 3]) << 24;

                // Ignore set PublicKey flag if assembly is not strongnamed
                if ((flags & (uint)AssemblyNameFlags.PublicKey) != 0 && public_key == null)
                {
                    flags &= ~(uint)AssemblyNameFlags.PublicKey;
                }

                if (Compiler.Settings.Target == Target.Module)
                {
                    SetCustomAttribute(ctor, cdata);
                }
                else
                {
                    builder_extra.SetFlags(flags, a.Location);
                }

                return;
            }

            if (a.Type == pa.TypeForwarder)
            {
                TypeSpec t = a.GetArgumentType();
                if (t == null || TypeManager.HasElementType(t))
                {
                    Report.Error(735, a.Location, "Invalid type specified as an argument for TypeForwardedTo attribute");
                    return;
                }

                if (emitted_forwarders == null)
                {
                    emitted_forwarders = new Dictionary <ITypeDefinition, Attribute> ();
                }
                else if (emitted_forwarders.ContainsKey(t.MemberDefinition))
                {
                    Report.SymbolRelatedToPreviousError(emitted_forwarders[t.MemberDefinition].Location, null);
                    Report.Error(739, a.Location, "A duplicate type forward of type `{0}'",
                                 t.GetSignatureForError());
                    return;
                }

                emitted_forwarders.Add(t.MemberDefinition, a);

                if (t.MemberDefinition.DeclaringAssembly == this)
                {
                    Report.SymbolRelatedToPreviousError(t);
                    Report.Error(729, a.Location, "Cannot forward type `{0}' because it is defined in this assembly",
                                 t.GetSignatureForError());
                    return;
                }

                if (t.IsNested)
                {
                    Report.Error(730, a.Location, "Cannot forward type `{0}' because it is a nested type",
                                 t.GetSignatureForError());
                    return;
                }

                builder_extra.AddTypeForwarder(t.GetDefinition(), a.Location);
                return;
            }

            if (a.Type == pa.Extension)
            {
                a.Error_MisusedExtensionAttribute();
                return;
            }

            if (a.Type == pa.InternalsVisibleTo)
            {
                string assembly_name = a.GetString();
                if (assembly_name.Length == 0)
                {
                    return;
                }
#if STATIC
                ParsedAssemblyName  aname;
                ParseAssemblyResult r = Fusion.ParseAssemblyName(assembly_name, out aname);
                if (r != ParseAssemblyResult.OK)
                {
                    Report.Warning(1700, 3, a.Location, "Assembly reference `{0}' is invalid and cannot be resolved",
                                   assembly_name);
                    return;
                }

                if (aname.Version != null || aname.Culture != null || aname.ProcessorArchitecture != ProcessorArchitecture.None)
                {
                    Report.Error(1725, a.Location,
                                 "Friend assembly reference `{0}' is invalid. InternalsVisibleTo declarations cannot have a version, culture or processor architecture specified",
                                 assembly_name);

                    return;
                }

                if (public_key != null && !aname.HasPublicKey)
                {
                    Report.Error(1726, a.Location,
                                 "Friend assembly reference `{0}' is invalid. Strong named assemblies must specify a public key in their InternalsVisibleTo declarations",
                                 assembly_name);
                    return;
                }
#endif
            }
            else if (a.Type == pa.RuntimeCompatibility)
            {
                wrap_non_exception_throws_custom = true;
            }
            else if (a.Type == pa.AssemblyFileVersion)
            {
                string value = a.GetString();
                if (string.IsNullOrEmpty(value) || IsValidAssemblyVersion(value, false) == null)
                {
                    Report.Warning(1607, 1, a.Location, "The version number `{0}' specified for `{1}' is invalid",
                                   value, a.Name);
                    return;
                }
            }


            SetCustomAttribute(ctor, cdata);
        }
Exemple #4
0
        public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
        {
            if (a.IsValidSecurityAttribute())
            {
                if (declarative_security == null)
                {
                    declarative_security = new ListDictionary();
                }

                a.ExtractSecurityPermissionSet(declarative_security);
                return;
            }

            if (a.Type == pa.AssemblyCulture)
            {
                string value = a.GetString();
                if (value == null || value.Length == 0)
                {
                    return;
                }

                if (RootContext.Target == Target.Exe)
                {
                    a.Error_AttributeEmitError("The executables cannot be satelite assemblies, remove the attribute or keep it empty");
                    return;
                }
            }

            if (a.Type == pa.AssemblyVersion)
            {
                string value = a.GetString();
                if (value == null || value.Length == 0)
                {
                    return;
                }

                value = value.Replace('*', '0');

                if (!IsValidAssemblyVersion(value))
                {
                    a.Error_AttributeEmitError(string.Format("Specified version `{0}' is not valid", value));
                    return;
                }
            }

            if (a.Type == pa.InternalsVisibleTo && !CheckInternalsVisibleAttribute(a))
            {
                return;
            }

            if (a.Type == pa.TypeForwarder)
            {
                Type t = a.GetArgumentType();
                if (t == null || TypeManager.HasElementType(t))
                {
                    Report.Error(735, a.Location, "Invalid type specified as an argument for TypeForwardedTo attribute");
                    return;
                }

                t = TypeManager.DropGenericTypeArguments(t);
                if (emitted_forwarders == null)
                {
                    emitted_forwarders = new ListDictionary();
                }
                else if (emitted_forwarders.Contains(t))
                {
                    Report.SymbolRelatedToPreviousError(((Attribute)emitted_forwarders[t]).Location, null);
                    Report.Error(739, a.Location, "A duplicate type forward of type `{0}'",
                                 TypeManager.CSharpName(t));
                    return;
                }

                emitted_forwarders.Add(t, a);

                if (TypeManager.LookupDeclSpace(t) != null)
                {
                    Report.SymbolRelatedToPreviousError(t);
                    Report.Error(729, a.Location, "Cannot forward type `{0}' because it is defined in this assembly",
                                 TypeManager.CSharpName(t));
                    return;
                }

                if (t.DeclaringType != null)
                {
                    Report.Error(730, a.Location, "Cannot forward type `{0}' because it is a nested type",
                                 TypeManager.CSharpName(t));
                    return;
                }

                if (add_type_forwarder == null)
                {
                    add_type_forwarder = typeof(AssemblyBuilder).GetMethod("AddTypeForwarder",
                                                                           BindingFlags.NonPublic | BindingFlags.Instance);

                    if (add_type_forwarder == null)
                    {
                        Report.RuntimeMissingSupport(a.Location, "TypeForwardedTo attribute");
                        return;
                    }
                }

                add_type_forwarder.Invoke(Builder, new object[] { t });
                return;
            }

            if (a.Type == pa.Extension)
            {
                a.Error_MisusedExtensionAttribute();
                return;
            }

            Builder.SetCustomAttribute(cb);
        }