📄 pending.cs
字号:
//// pending.cs: Pending method implementation//// Author:// Miguel de Icaza (miguel@gnu.org)//// Licensed under the terms of the GNU GPL//// (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)////using System;using System.Collections;using System.Reflection;using System.Reflection.Emit;namespace Mono.CSharp { struct TypeAndMethods { public Type type; public MethodInfo [] methods; // // Whether it is optional, this is used to allow the explicit/implicit // implementation when a base class already implements an interface. // // For example: // // class X : IA { } class Y : X, IA { IA.Explicit (); } // public bool optional; // Far from ideal, but we want to avoid creating a copy // of methods above. public Type [][] args; //This is used to store the modifiers of arguments public Parameter.Modifier [][] mods; // // This flag on the method says `We found a match, but // because it was private, we could not use the match // public bool [] found; // If a method is defined here, then we always need to // create a proxy for it. This is used when implementing // an interface's indexer with a different IndexerName. public MethodInfo [] need_proxy; // // The name of the indexer (if it exists), precompute set/get, because // they would be recomputed many times inside a loop later on. // public string set_indexer_name; public string get_indexer_name; } public class PendingImplementation { /// <summary> /// The container for this PendingImplementation /// </summary> TypeContainer container; /// <summary> /// This filter is used by FindMembers, and it is used to /// extract only virtual/abstract fields /// </summary> static MemberFilter virtual_method_filter; /// <summary> /// This is the array of TypeAndMethods that describes the pending implementations /// (both interfaces and abstract methods in base class) /// </summary> TypeAndMethods [] pending_implementations; static bool IsVirtualFilter (MemberInfo m, object filterCriteria) { MethodInfo mi = m as MethodInfo; return (mi == null) ? false : mi.IsVirtual; } /// <summary> /// Inits the virtual_method_filter /// </summary> static PendingImplementation () { virtual_method_filter = new MemberFilter (IsVirtualFilter); } // <remarks> // Returns a list of the abstract methods that are exposed by all of our // bases that we must implement. Notice that this `flattens' the // method search space, and takes into account overrides. // </remarks> static ArrayList GetAbstractMethods (Type t) { ArrayList list = null; bool searching = true; Type current_type = t; do { MemberList mi; mi = TypeContainer.FindMembers ( current_type, MemberTypes.Method, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly, virtual_method_filter, null); if (current_type == TypeManager.object_type) searching = false; else { current_type = current_type.BaseType; if (!current_type.IsAbstract) searching = false; } if (mi.Count == 0) continue; if (mi.Count == 1 && !(mi [0] is MethodBase)) searching = false; else list = TypeManager.CopyNewMethods (list, mi); } while (searching); if (list == null) return null; for (int i = 0; i < list.Count; i++){ while (list.Count > i && !((MethodInfo) list [i]).IsAbstract) list.RemoveAt (i); } if (list.Count == 0) return null; return list; } PendingImplementation (TypeContainer container, MissingInterfacesInfo [] missing_ifaces, ArrayList abstract_methods, int total) { TypeBuilder type_builder = container.TypeBuilder; this.container = container; pending_implementations = new TypeAndMethods [total]; int i = 0; foreach (MissingInterfacesInfo missing in missing_ifaces){ MethodInfo [] mi; Type t = missing.Type; if (!t.IsInterface) continue; if (t is TypeBuilder){ TypeContainer iface; iface = TypeManager.LookupInterface (t); mi = iface.GetMethods (); } else mi = t.GetMethods (); int count = mi.Length; pending_implementations [i].type = t; pending_implementations [i].optional = missing.Optional; pending_implementations [i].methods = mi; pending_implementations [i].args = new Type [count][]; pending_implementations [i].mods = new Parameter.Modifier [count][]; pending_implementations [i].found = new bool [count]; pending_implementations [i].need_proxy = new MethodInfo [count]; string indexer_name = TypeManager.IndexerPropertyName (t); pending_implementations [i].set_indexer_name = "set_" + indexer_name; pending_implementations [i].get_indexer_name = "get_" + indexer_name; int j = 0; foreach (MethodInfo m in mi){ pending_implementations [i].args [j] = TypeManager.NoTypes; pending_implementations [i].mods [j] = null; // If there is a previous error, just ignore if (m == null) continue; pending_implementations [i].args [j] = TypeManager.GetArgumentTypes (m); ParameterData pd = TypeManager.GetParameterData (m); if (pd.Count > 0){ Parameter.Modifier [] pm = new Parameter.Modifier [pd.Count]; for (int k = 0; k < pd.Count; k++) pm [k] = pd.ParameterModifier (k); pending_implementations [i].mods [j] = pm; } j++; } i++; } if (abstract_methods != null){ int count = abstract_methods.Count; pending_implementations [i].methods = new MethodInfo [count]; pending_implementations [i].need_proxy = new MethodInfo [count]; abstract_methods.CopyTo (pending_implementations [i].methods, 0); pending_implementations [i].found = new bool [count]; pending_implementations [i].args = new Type [count][]; pending_implementations [i].mods = new Parameter.Modifier [count][]; pending_implementations [i].type = type_builder; string indexer_name = TypeManager.IndexerPropertyName (type_builder); pending_implementations [i].set_indexer_name = "set_" + indexer_name; pending_implementations [i].get_indexer_name = "get_" + indexer_name; int j = 0; foreach (MemberInfo m in abstract_methods){ MethodInfo mi = (MethodInfo) m; Type [] types = TypeManager.GetArgumentTypes (mi); ParameterData pd = TypeManager.GetParameterData (mi); pending_implementations [i].args [j] = types; pending_implementations [i].mods [j] = null; if (pd.Count > 0){ Parameter.Modifier [] pm = new Parameter.Modifier [pd.Count]; for (int k = 0; k < pd.Count; k++) pm [k] = pd.ParameterModifier (k); pending_implementations [i].mods [j] = pm; } j++; } } } struct MissingInterfacesInfo { public Type Type; public bool Optional; public MissingInterfacesInfo (Type t) { Type = t; Optional = false; } } static MissingInterfacesInfo [] EmptyMissingInterfacesInfo = new MissingInterfacesInfo [0]; static MissingInterfacesInfo [] GetMissingInterfaces (TypeBuilder type_builder) { // // Notice that TypeBuilders will only return the interfaces that the Type // is supposed to implement, not all the interfaces that the type implements. // // Even better -- on MS it returns an empty array, no matter what. // // Completely broken. So we do it ourselves! // Type [] impl = TypeManager.GetExplicitInterfaces (type_builder); if (impl == null || impl.Length == 0) return EmptyMissingInterfacesInfo; MissingInterfacesInfo [] ret = new MissingInterfacesInfo [impl.Length]; for (int i = 0; i < impl.Length; i++) ret [i] = new MissingInterfacesInfo (impl [i]); // we really should not get here because Object doesnt implement any // interfaces. But it could implement something internal, so we have // to handle that case. if (type_builder.BaseType == null) return ret; Type [] base_impls = TypeManager.GetInterfaces (type_builder.BaseType); foreach (Type t in base_impls) { for (int i = 0; i < ret.Length; i ++) { if (t == ret [i].Type) { ret [i].Optional = true; break; } } } return ret; } // // Factory method: if there are pending implementation methods, we return a PendingImplementation // object, otherwise we return null. // // Register method implementations are either abstract methods // flagged as such on the base class or interface methods // static public PendingImplementation GetPendingImplementations (TypeContainer container) { TypeBuilder type_builder = container.TypeBuilder; MissingInterfacesInfo [] missing_interfaces; Type b = type_builder.BaseType; missing_interfaces = GetMissingInterfaces (type_builder); //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -