📄 class.cs
字号:
{ if (al == null) return; foreach (MemberCore mc in al) { if ((mc.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE) continue; if (!mc.IsUsed) { Report.Warning (169, mc.Location, "The private {0} `{1}' is never used", member_type, mc.GetSignatureForError ()); } } } public virtual void VerifyMembers () { // // Check for internal or private fields that were never assigned // if (RootContext.WarningLevel >= 3) { CheckMemberUsage (properties, "property"); CheckMemberUsage (methods, "method"); CheckMemberUsage (constants, "constant"); if (fields != null){ foreach (FieldMember f in fields) { if ((f.ModFlags & Modifiers.Accessibility) != Modifiers.PRIVATE) continue; if (!f.IsUsed){ if ((f.caching_flags & Flags.IsAssigned) == 0) Report.Warning (169, 3, f.Location, "The private field `{0}' is never used", f.GetSignatureForError ()); else {#if NET_2_0 const int error_code = 414;#else const int error_code = 169;#endif Report.Warning (error_code, 3, f.Location, "The private field `{0}' is assigned but its value is never used", f.GetSignatureForError ()); } continue; } // // Only report 649 on level 4 // if (RootContext.WarningLevel < 4) continue; if ((f.caching_flags & Flags.IsAssigned) != 0) continue; Report.Warning (649, f.Location, "Field `{0}' is never assigned to, and will always have its default value `{1}'", f.GetSignatureForError (), f.Type.Type.IsValueType ? Activator.CreateInstance (f.Type.Type).ToString() : "null"); } } } } /// <summary> /// Emits the code, this step is performed after all /// the types, enumerations, constructors /// </summary> public virtual void EmitType () { if (OptAttributes != null) OptAttributes.Emit (ec, this); // // Structs with no fields need to have at least one byte. // The right thing would be to set the PackingSize in a DefineType // but there are no functions that allow interfaces *and* the size to // be specified. // if (Kind == Kind.Struct && first_nonstatic_field == null){ FieldBuilder fb = TypeBuilder.DefineField ("$PRIVATE$", TypeManager.byte_type, FieldAttributes.Private); if (HasExplicitLayout){ object [] ctor_args = new object [1]; ctor_args [0] = 0; CustomAttributeBuilder cba = new CustomAttributeBuilder ( TypeManager.field_offset_attribute_ctor, ctor_args); fb.SetCustomAttribute (cba); } } Emit (); if (instance_constructors != null) { if (TypeBuilder.IsSubclassOf (TypeManager.attribute_type) && RootContext.VerifyClsCompliance && IsClsCompliaceRequired (this)) { bool has_compliant_args = false; foreach (Constructor c in instance_constructors) { c.Emit (); if (has_compliant_args) continue; has_compliant_args = c.HasCompliantArgs; } if (!has_compliant_args) Report.Error (3015, Location, "`{0}' has no accessible constructors which use only CLS-compliant types", GetSignatureForError ()); } else { foreach (Constructor c in instance_constructors) c.Emit (); } } // Can not continue if constants are broken EmitConstants (); if (Report.Errors > 0) return; if (default_static_constructor != null) default_static_constructor.Emit (); if (methods != null) foreach (Method m in methods) m.Emit (); if (operators != null) foreach (Operator o in operators) o.Emit (); if (properties != null) foreach (Property p in properties) p.Emit (); if (indexers != null){ indexers.Emit (); } if (fields != null) foreach (FieldMember f in fields) f.Emit (); if (events != null){ foreach (Event e in Events) e.Emit (); } if (delegates != null) { foreach (Delegate d in Delegates) { d.Emit (); } } if (enums != null) { foreach (Enum e in enums) { e.Emit (); } } if (parts != null) { foreach (ClassPart part in parts) part.EmitType (); } if ((Pending != null) && !(this is ClassPart)) if (Pending.VerifyPendingMethods ()) return; if (iterators != null) foreach (Iterator iterator in iterators) iterator.EmitType (); // if (types != null)// foreach (TypeContainer tc in types)// tc.Emit (); } public override void CloseType () { if ((caching_flags & Flags.CloseTypeCreated) != 0) return; try { caching_flags |= Flags.CloseTypeCreated; TypeBuilder.CreateType (); } catch (TypeLoadException){ // // This is fine, the code still created the type //// Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name);// Console.WriteLine (e.Message); } catch { Console.WriteLine ("In type: " + Name); throw; } if (Enums != null) foreach (Enum en in Enums) en.CloseType (); if (Types != null){ foreach (TypeContainer tc in Types) if (tc.Kind == Kind.Struct) tc.CloseType (); foreach (TypeContainer tc in Types) if (tc.Kind != Kind.Struct) tc.CloseType (); } if (Delegates != null) foreach (Delegate d in Delegates) d.CloseType (); if (Iterators != null) foreach (Iterator i in Iterators) i.CloseType (); types = null; properties = null; enums = null; delegates = null; fields = null; initialized_fields = null; initialized_static_fields = null; constants = null; interfaces = null; methods = null; events = null; indexers = null; operators = null; iterators = null; ec = null; default_constructor = null; default_static_constructor = null; type_bases = null; OptAttributes = null; ifaces = null; base_cache = null; member_cache = null; } // // Performs the validation on a Method's modifiers (properties have // the same properties). // public bool MethodModifiersValid (MemberCore mc) { const int vao = (Modifiers.VIRTUAL | Modifiers.ABSTRACT | Modifiers.OVERRIDE); const int va = (Modifiers.VIRTUAL | Modifiers.ABSTRACT); const int nv = (Modifiers.NEW | Modifiers.VIRTUAL); bool ok = true; int flags = mc.ModFlags; // // At most one of static, virtual or override // if ((flags & Modifiers.STATIC) != 0){ if ((flags & vao) != 0){ Report.Error (112, mc.Location, "A static member `{0}' cannot be marked as override, virtual or abstract", mc.GetSignatureForError ()); ok = false; } } if (Kind == Kind.Struct){ if ((flags & va) != 0){ Modifiers.Error_InvalidModifier (mc.Location, "virtual or abstract"); ok = false; } } if ((flags & Modifiers.OVERRIDE) != 0 && (flags & nv) != 0){ Report.Error (113, mc.Location, "A member `{0}' marked as override cannot be marked as new or virtual", mc.GetSignatureForError ()); ok = false; } // // If the declaration includes the abstract modifier, then the // declaration does not include static, virtual or extern // if ((flags & Modifiers.ABSTRACT) != 0){ if ((flags & Modifiers.EXTERN) != 0){ Report.Error ( 180, mc.Location, "`{0}' cannot be both extern and abstract", mc.GetSignatureForError ()); ok = false; } if ((flags & Modifiers.SEALED) != 0) { Report.Error (502, mc.Location, "`{0}' cannot be both abstract and sealed", mc.GetSignatureForError ()); ok = false; } if ((flags & Modifiers.VIRTUAL) != 0){ Report.Error (503, mc.Location, "The abstract method `{0}' cannot be marked virtual", mc.GetSignatureForError ()); ok = false; } if ((ModFlags & Modifiers.ABSTRACT) == 0){ Report.Error (513, mc.Location, "`{0}' is abstract but it is contained in nonabstract class", mc.GetSignatureForError ()); ok = false; } } if ((flags & Modifiers.PRIVATE) != 0){ if ((flags & vao) != 0){ Report.Error (621, mc.Location, "`{0}': virtual or abstract members cannot be private", mc.GetSignatureForError ()); ok = false; } } if ((flags & Modifiers.SEALED) != 0){ if ((flags & Modifiers.OVERRIDE) == 0){ Report.Error (238, mc.Location, "`{0}' cannot be sealed because it is not an override", mc.GetSignatureForError ()); ok = false; } } return ok; } public bool UserDefinedStaticConstructor { get { return default_static_constructor != null; } } public Constructor DefaultStaticConstructor { get { return default_static_constructor; } } protected override bool VerifyClsCompliance (DeclSpace ds) { if (!base.VerifyClsCompliance (ds)) return false; VerifyClsName (); Type base_type = TypeBuilder.BaseType; if (base_type != null && !AttributeTester.IsClsCompliant (base_type)) { Report.Error (3009, Location, "`{0}': base type `{1}' is not CLS-compliant", GetSignatureForError (), TypeManager.CSharpName (base_type)); } if (!Parent.IsClsCompliaceRequired (ds)) { Report.Error (3018, Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'", GetSignatureForError (), Parent.GetSignatureForError ()); } return true; } /// <summary> /// Checks whether container name is CLS Compliant /// </summary> void VerifyClsName () { Hashtable base_members = base_cache == null ? new Hashtable () : base_cache.GetPublicMembers (); Hashtable this_members = new Hashtable (); foreach (DictionaryEntry entry in defined_names) { MemberCore mc = (MemberCore)entry.Value; if (!mc.IsClsCompliaceRequired (mc.Parent)) continue; string name = (string) entry.Key; string basename = name.Substring (name.LastIndexOf ('.') + 1); string lcase = basename.ToLower (System.Globalization.CultureInfo.InvariantCulture); object found = base_members [lcase]; if (found == null) { found = this_members [lcase]; if (found == null) { this_members.Add (lcase, mc); continue; } } if ((mc.ModFlags & Modifiers.OVERRIDE) != 0) continue; if (found is MemberInfo) { if (basename == ((MemberInfo) found).Name) continue; Report.SymbolRelatedToPreviousError ((MemberInfo) found); } else { Report.SymbolRelatedToPreviousError ((MemberCore) found); } Report.Error (3005, mc.Location, "Identifier `{0}' differing only in case is not CLS-compliant", mc.GetSignatureForError ()); } } /// <summary> /// Performs checks for an explicit interface implementation. First it /// checks whether the `interface_type' is a base inteface implementation. /// Then it checks whether `name' exists in the interface type. /// </summary> public virtual bool VerifyImplements (MemberBase mb) { if (ifaces != null) { foreach (Type t in ifaces){ if (t == mb.InterfaceType) return true; } } Report.Error (540, mb.Location, "`{0}': containing type does not implement interface `{1}'", mb.GetSignatureForError (), TypeManager.CSharpName (mb.InterfaceType)); return false; } // // IMemberContainer // string IMemberContainer.Name { get { return Name; } } Type IMemberContainer.Type { get { return TypeBuilder; } } MemberCache IMemberContainer.MemberCache { get { return member_cache; } } bool IMemberContainer.IsInterface { get { return Kind == Kind.Interface; } } MemberList IMemberContainer.GetMembers (MemberTypes mt, BindingFlags bf) { return FindMembers (mt, bf | BindingFlags.DeclaredOnly, null, null); } // // Generates xml doc comments (if any), and if required, // handle warning report. // internal override void GenerateDocComment (DeclSpace ds) { DocUtil.GenerateTypeDocComment (this, ds); } public override string DocCommentHeader { get { return "T:"; } } public virtual MemberCache BaseCache { get { if (base_cache != null) return base_cache; if (TypeBuilder.BaseType != null) base_cache = TypeManager.LookupMemberCache (TypeBuilder.BaseType); if (TypeBuilder.IsInterface) base_cache = TypeManager.LookupBaseInterfacesCache (TypeBuilder); return base_cache; } } } public class PartialContainer : TypeContainer { public readonly Namespace Namespace; public readonly int OriginalModFlags; public readonly int AllowedModifiers; public readonly TypeAttributes DefaultTypeAttributes; public ListDictionary DeclarativeSecurity; static PartialContainer Create (NamespaceEntry ns, TypeContainer parent, MemberName member_name, int mod_flags, Kind kind) { if (!CheckModFlags (0, mod_flags, member_name)) return null; PartialContainer pc = RootContext.Tree.GetDecl (member_name) as PartialContainer; if (pc != null) { if (pc.Kind != kind) { Report.Error ( 261, member_name.Location, "Partial declarations of `{0}' " + "must be all classes, all structs or " + "all interfaces", member_name.GetPartialName ()); return null; } if (!CheckModFlags (pc.OriginalModFlags, mod_flags, member_name)) return null; pc.ModFlags |= (mod_flags & pc.AllowedModifiers); return pc; } if (parent is ClassPart) parent = ((ClassPart) parent).PartialContainer; pc = new PartialContainer (ns.NS, parent, member_name, mod_flags, kind); if (kind == Kind.Interface) { if (!parent.AddInterface (pc)) return null; } else if (kind == Kind.Class || kind == Kind.Struct) { if (!parent.AddClassOrStruct (pc)) return null; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -