📄 typemanager.cs
字号:
return container.MemberCache; } return TypeHandle.GetMemberCache (t); } public static MemberCache LookupBaseInterfacesCache (Type t) { Type [] ifaces = t.GetInterfaces (); if (ifaces != null && ifaces.Length == 1) return LookupMemberCache (ifaces [0]); // TODO: the builder_to_member_cache should be indexed by 'ifaces', not 't' MemberCache cache = builder_to_member_cache [t] as MemberCache; if (cache != null) return cache; cache = new MemberCache (ifaces); builder_to_member_cache.Add (t, cache); return cache; } public static TypeContainer LookupInterface (Type t) { TypeContainer tc = (TypeContainer) builder_to_declspace [t]; if ((tc == null) || (tc.Kind != Kind.Interface)) return null; return tc; } public static Delegate LookupDelegate (Type t) { return builder_to_declspace [t] as Delegate; } public static Class LookupClass (Type t) { return (Class) builder_to_declspace [t]; } // // We use this hash for multiple kinds of constructed types: // // (T, "&") Given T, get T & // (T, "*") Given T, get T * // (T, "[]") Given T and a array dimension, get T [] // (T, X) Given a type T and a simple name X, get the type T+X // // Accessibility tests, if necessary, should be done by the user // static DoubleHash type_hash = new DoubleHash (); // // Gets the reference to T version of the Type (T&) // public static Type GetReferenceType (Type t) { return GetConstructedType (t, "&"); } // // Gets the pointer to T version of the Type (T*) // public static Type GetPointerType (Type t) { return GetConstructedType (t, "*"); } public static Type GetConstructedType (Type t, string dim) { object ret = null; if (!type_hash.Lookup (t, dim, out ret)) { ret = t.Module.GetType (t.ToString () + dim); type_hash.Insert (t, dim, ret); } return (Type) ret; } public static Type GetNestedType (Type t, string name) { object ret = null; if (!type_hash.Lookup (t, name, out ret)) { ret = t.GetNestedType (name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); type_hash.Insert (t, name, ret); } return (Type) ret; } /// <summary> /// Fills static table with exported types from all referenced assemblies. /// This information is required for CLS Compliance tests. /// </summary> public static void LoadAllImportedTypes () { AllClsTopLevelTypes = new Hashtable (1500); foreach (Assembly a in RootNamespace.Global.Assemblies) { foreach (Type t in a.GetExportedTypes ()) { AllClsTopLevelTypes [t.FullName.ToLower (System.Globalization.CultureInfo.InvariantCulture)] = null; } } } public static bool NamespaceClash (string name, Location loc) { if (! RootNamespace.Global.IsNamespace (name)) return false; Report.Error (519, loc, String.Format ("`{0}' clashes with a predefined namespace", name)); return true; } /// <summary> /// Returns the C# name of a type if possible, or the full type name otherwise /// </summary> static public string CSharpName (Type t) { return Regex.Replace (t.FullName, @"^System\." + @"(Int32|UInt32|Int16|UInt16|Int64|UInt64|" + @"Single|Double|Char|Decimal|Byte|SByte|Object|" + @"Boolean|String|Void|Null)" + @"(\W+|\b)", new MatchEvaluator (CSharpNameMatch)).Replace ('+', '.'); } static public string CSharpName (Type[] types) { StringBuilder sb = new StringBuilder (); foreach (Type t in types) { sb.Append (CSharpName (t)); sb.Append (','); } sb.Remove (sb.Length - 1, 1); return sb.ToString (); } static String CSharpNameMatch (Match match) { string s = match.Groups [1].Captures [0].Value; return s.ToLower (). Replace ("int32", "int"). Replace ("uint32", "uint"). Replace ("int16", "short"). Replace ("uint16", "ushort"). Replace ("int64", "long"). Replace ("uint64", "ulong"). Replace ("single", "float"). Replace ("boolean", "bool") + match.Groups [2].Captures [0].Value; } /// <summary> /// Returns the signature of the method with full namespace classification /// </summary> static public string GetFullNameSignature (MemberInfo mi) { return (mi is MethodBase) ? CSharpSignature (mi as MethodBase) : CSharpName (mi.DeclaringType) + '.' + mi.Name; } /// <summary> /// When we need to report accessors as well /// </summary> static public string CSharpSignature (MethodBase mb) { return CSharpSignature (mb, false); } /// <summary> /// Returns the signature of the method /// </summary> static public string CSharpSignature (MethodBase mb, bool show_accessor) { StringBuilder sig = new StringBuilder (CSharpName (mb.DeclaringType)); sig.Append ('.'); ParameterData iparams = GetParameterData (mb); string parameters = iparams.GetSignatureForError (); string accessor = ""; // Is property if (mb.IsSpecialName) { Operator.OpType ot = Operator.GetOperatorType (mb.Name); if (ot != Operator.OpType.TOP) { sig.Append ("operator "); sig.Append (Operator.GetName (ot)); sig.Append (parameters); return sig.ToString (); } if (mb.Name.StartsWith ("get_") || mb.Name.StartsWith ("set_")) { accessor = mb.Name.Substring (0, 3); } } // Is indexer if (mb.IsSpecialName && !mb.IsConstructor) { if (iparams.Count > 1) { sig.Append ("this["); if (show_accessor) { sig.Append (parameters.Substring (1, parameters.Length - 2)); } else { int before_ret_val = parameters.LastIndexOf (','); sig.Append (parameters.Substring (1, before_ret_val - 1)); } sig.Append (']'); } else { sig.Append (mb.Name.Substring (4)); } } else { if (mb.Name == ".ctor") sig.Append (mb.DeclaringType.Name); else sig.Append (mb.Name); sig.Append (parameters); } if (show_accessor && accessor.Length > 0) { sig.Append ('.'); sig.Append (accessor); } return sig.ToString (); } static public string CSharpSignature (EventInfo ei) { return CSharpName (ei.DeclaringType) + '.' + ei.Name; } /// <summary> /// Looks up a type, and aborts if it is not found. This is used /// by types required by the compiler /// </summary> static Type CoreLookupType (string ns_name, string name) { Namespace ns = RootNamespace.Global.GetNamespace (ns_name, true); FullNamedExpression fne = ns.Lookup (RootContext.Tree.Types, name, Location.Null); Type t = fne == null ? null : fne.Type; if (t == null) Report.Error (518, "The predefined type `" + name + "' is not defined or imported"); return t; } /// <summary> /// Returns the MethodInfo for a method named `name' defined /// in type `t' which takes arguments of types `args' /// </summary> static MethodInfo GetMethod (Type t, string name, Type [] args, bool is_private, bool report_errors) { MemberList list; Signature sig; BindingFlags flags = instance_and_static | BindingFlags.Public; sig.name = name; sig.args = args; if (is_private) flags |= BindingFlags.NonPublic; list = FindMembers (t, MemberTypes.Method, flags, signature_filter, sig); if (list.Count == 0) { if (report_errors) Report.Error (-19, "Can not find the core function `" + name + "'"); return null; } MethodInfo mi = list [0] as MethodInfo; if (mi == null) { if (report_errors) Report.Error (-19, "Can not find the core function `" + name + "'"); return null; } return mi; } static MethodInfo GetMethod (Type t, string name, Type [] args, bool report_errors) { return GetMethod (t, name, args, false, report_errors); } static MethodInfo GetMethod (Type t, string name, Type [] args) { return GetMethod (t, name, args, true); } /// <summary> /// Returns the PropertyInfo for a property named `name' defined /// in type `t' /// </summary> static PropertyInfo GetProperty (Type t, string name) { MemberList list = FindMembers (t, MemberTypes.Property, BindingFlags.Public | BindingFlags.Instance, Type.FilterName, name); if (list.Count == 0) { Report.Error (-19, "Can not find the core property `" + name + "'"); return null; } PropertyInfo pi = list [0] as PropertyInfo; if (pi == null) { Report.Error (-19, "Can not find the core function `" + name + "'"); return null; } return pi; } /// <summary> /// Returns the ConstructorInfo for "args" /// </summary> public static ConstructorInfo GetConstructor (Type t, Type [] args) { MemberList list; Signature sig; sig.name = ".ctor"; sig.args = args; list = FindMembers (t, MemberTypes.Constructor, instance_and_static | BindingFlags.Public | BindingFlags.DeclaredOnly, signature_filter, sig); if (list.Count == 0){ Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'"); return null; } ConstructorInfo ci = list [0] as ConstructorInfo; if (ci == null){ Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'"); return null; } return ci; } public static void InitEnumUnderlyingTypes () { int32_type = CoreLookupType ("System", "Int32"); int64_type = CoreLookupType ("System", "Int64"); uint32_type = CoreLookupType ("System", "UInt32"); uint64_type = CoreLookupType ("System", "UInt64"); byte_type = CoreLookupType ("System", "Byte"); sbyte_type = CoreLookupType ("System", "SByte"); short_type = CoreLookupType ("System", "Int16"); ushort_type = CoreLookupType ("System", "UInt16"); } /// <remarks> /// The types have to be initialized after the initial /// population of the type has happened (for example, to /// bootstrap the corlib.dll /// </remarks> public static void InitCoreTypes () { object_type = CoreLookupType ("System", "Object"); value_type = CoreLookupType ("System", "ValueType"); InitEnumUnderlyingTypes (); char_type = CoreLookupType ("System", "Char"); string_type = CoreLookupType ("System", "String"); float_type = CoreLookupType ("System", "Single"); double_type = CoreLookupType ("System", "Double"); char_ptr_type = GetPointerType (char_type); decimal_type = CoreLookupType ("System", "Decimal"); bool_type = CoreLookupType ("System", "Boolean"); enum_type = CoreLookupType ("System", "Enum"); multicast_delegate_type = CoreLookupType ("System", "MulticastDelegate"); delegate_type = CoreLookupType ("System", "Delegate"); array_type = CoreLookupType ("System", "Array"); void_type = CoreLookupType ("System", "Void"); type_type = CoreLookupType ("System", "Type"); runtime_field_handle_type = CoreLookupType ("System", "RuntimeFieldHandle"); runtime_argument_handle_type = CoreLookupType ("System", "RuntimeArgumentHandle"); runtime_helpers_type = CoreLookupType ("System.Runtime.CompilerServices", "RuntimeHelpers"); default_member_type = CoreLookupType ("System.Reflection", "DefaultMemberAttribute"); runtime_handle_type = CoreLookupType ("System", "RuntimeTypeHandle"); asynccallback_type = CoreLookupType ("System", "AsyncCallback"); iasyncresult_type = CoreLookupType ("System", "IAsyncResult"); ienumerator_type = CoreLookupType ("System.Collections", "IEnumerator"); ienumerable_type = CoreLookupType ("System.Collections", "IEnumerable"); idisposable_type = CoreLookupType ("System", "IDisposable"); icloneable_type = CoreLookupType ("System", "ICloneable"); iconvertible_type = CoreLookupType ("System", "IConvertible"); monitor_type = CoreLookupType ("System.Threading", "Monitor"); intptr_type = CoreLookupType ("System", "IntPtr"); attribute_type = CoreLookupType ("System", "Attribute"); attribute_usage_type = CoreLookupType ("System", "AttributeUsageAttribute"); dllimport_type = CoreLookupType ("System.Runtime.InteropServices", "DllImportAttribute"); methodimpl_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "MethodImplAttribute"); marshal_as_attr_type = CoreLookupType ("System.Runtime.InteropServices", "MarshalAsAttribute"); param_array_type = CoreLookupType ("System", "ParamArrayAttribute"); in_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "InAttribute"); out_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "OutAttribute"); typed_reference_type = CoreLookupType ("System", "TypedReference"); arg_iterator_type = CoreLookupType ("System", "ArgIterator"); mbr_type = CoreLookupType ("System", "MarshalByRefObject"); decimal_constant_attribute_type = CoreLookupType ("System.Runtime.CompilerServices", "DecimalConstantAttribute"); unverifiable_code_type= CoreLookupType ("System.Security", "UnverifiableCodeAttribute"); void_ptr_type = GetPointerType (void_type); indexer_name_type = CoreLookupType ("System.Runtime.CompilerServices", "IndexerNameAttribute"); exception_type = CoreLookupType ("System", "Exception"); invalid_operation_exception_type = CoreLookupType ("System", "InvalidOperationException"); not_supported_exception_type = CoreLookupType ("System", "NotSupportedException"); // // Attribute types // obsolete_attribute_type = CoreLookupType ("System", "ObsoleteAttribute"); conditional_attribute_type = CoreLookupType ("System.Diagnostics", "ConditionalAttribute"); cls_compliant_attribute_type = CoreLookupType ("System", "CLSCompliantAttribute"); struct_layout_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "StructLayoutAttribute"); field_offset_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "FieldOffsetAttribute");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -