protected static bool IsEqualOrSubclass(Mono.Linker.AssemblyResolver resolver, MC.TypeReference inspect, MC.TypeReference baseClass) { MC.TypeDefinition currentType = resolver.Resolve(inspect); while (true) { if (baseClass.FullName == currentType.FullName) { return(true); } if (currentType.BaseType.FullName == MC.Constants.Object) { return(false); } currentType = resolver.Resolve(currentType.BaseType); } }
protected static IEnumerable <MC.CustomAttribute> AllCustomAttributes(Mono.Linker.AssemblyResolver resolver, MC.TypeDefinition typedef) { MC.TypeDefinition currentType = typedef; while (true) { foreach (MC.CustomAttribute attr in currentType.CustomAttributes) { yield return(attr); } if (currentType.BaseType.FullName == MC.Constants.Object) { yield break; } currentType = resolver.Resolve(currentType.BaseType); } }
protected static bool ImplementsInterface(Mono.Linker.AssemblyResolver resolver, MC.TypeReference inspect, MC.TypeReference interf) { MC.TypeDefinition currentType = resolver.Resolve(inspect); foreach (MC.TypeReference child in currentType.Interfaces) { if (child.FullName == interf.FullName) { return(true); } if (ImplementsInterface(resolver, child, interf)) { return(true); } } if (ImplementsInterface(resolver, currentType.BaseType, interf)) { return(true); } return(false); }
public IList <ItemToolboxNode> Load(LoaderContext ctx, string filename) { MC.AssemblyDefinition assem = MC.AssemblyFactory.GetAssembly(filename); Mono.Linker.AssemblyResolver resolver = new Mono.Linker.AssemblyResolver(); assem.Resolver = resolver; //Grab types that should be in System.dll Version runtimeVersion; switch (assem.Runtime) { case MC.TargetRuntime.NET_1_0: case MC.TargetRuntime.NET_1_1: runtimeVersion = new Version(1, 0, 5000, 0); break; case MC.TargetRuntime.NET_2_0: runtimeVersion = new Version(2, 0, 0, 0); break; default: throw new NotSupportedException("Runtime '" + assem.Runtime + "' is not supported."); } MC.AssemblyNameReference sysAssem = new MC.AssemblyNameReference("System", string.Empty, runtimeVersion); MC.TypeReference toolboxItemType = new MC.TypeReference("ToolboxItemAttribute", "System.ComponentModel", sysAssem, false); MC.TypeReference categoryType = new MC.TypeReference("CategoryAttribute", "System.ComponentModel", sysAssem, false); if (resolver.Resolve(toolboxItemType) == null) { return(null); } List <ItemToolboxNode> list = new List <ItemToolboxNode> (); foreach (MC.ModuleDefinition module in assem.Modules) { foreach (MC.TypeDefinition typedef in module.Types) { //only show visible concrete classes if (typedef.IsAbstract || !typedef.IsClass || !typedef.IsPublic) { continue; } MC.TypeDefinition toolboxItem = null; string category = null; //find the ToolboxItem and Category attributes foreach (MC.CustomAttribute att in AllCustomAttributes(resolver, typedef)) { //find the value of the toolbox attribute if (toolboxItem == null && IsEqualOrSubclass(resolver, att.Constructor.DeclaringType, toolboxItemType)) { //check if it's ToolboxItemAttribute(false) (or null) att.Resolve(); IList args = GetArgumentsToBaseConstructor(att, toolboxItemType); if (args != null) { if (args.Count != 1) { throw new InvalidOperationException("Malformed toolboxitem attribute"); } object o = args[0]; if (o == null || (o is bool && ((bool)o) == false)) { break; } else { //w have a type name for the toolbox item. try to resolve it string typeName = (string)o; toolboxItem = null; try { resolver.Resolve(TypeReferenceFromString(typeName)); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } if (toolboxItem == null) { System.Diagnostics.Debug.WriteLine( "CecilToolboxItemScanner: Error resolving type " + typeName); break; } } } } //find the value of the category attribute if (category == null && IsEqualOrSubclass(resolver, att.Constructor.DeclaringType, categoryType)) { att.Resolve(); IList args = GetArgumentsToBaseConstructor(att, categoryType); if (args != null && args.Count == 1) { category = (string)args[0]; } else { throw new InvalidOperationException("Malformed category attribute"); } } if (toolboxItem != null && category != null) { break; } } if (toolboxItem == null) { continue; } Load(assem, typedef, toolboxItem, category); } } return(list); }
public IList<ItemToolboxNode> Load (LoaderContext ctx, string filename) { Mono.Linker.AssemblyResolver resolver = new Mono.Linker.AssemblyResolver (); MC.AssemblyDefinition assem = MC.AssemblyDefinition.ReadAssembly (filename, new MC.ReaderParameters { AssemblyResolver = resolver }); //Grab types that should be in System.dll Version runtimeVersion; switch (assem.MainModule.Runtime) { case MC.TargetRuntime.Net_1_0: case MC.TargetRuntime.Net_1_1: runtimeVersion = new Version (1, 0, 5000, 0); break; case MC.TargetRuntime.Net_2_0: runtimeVersion = new Version (2, 0, 0, 0); break; default: throw new NotSupportedException ("Runtime '" + assem.MainModule.Runtime + "' is not supported."); } MC.AssemblyNameReference sysAssem = new MC.AssemblyNameReference ("System", runtimeVersion); MC.TypeReference toolboxItemType = new MC.TypeReference ("System.ComponentModel", "ToolboxItemAttribute", assem.MainModule, sysAssem, false); MC.TypeReference categoryType = new MC.TypeReference ("ToolboxItemAttribute", "CategoryAttribute", assem.MainModule, sysAssem, false); if (resolver.Resolve (toolboxItemType) == null) return null; List<ItemToolboxNode> list = new List<ItemToolboxNode> (); foreach (MC.ModuleDefinition module in assem.Modules) { foreach (MC.TypeDefinition typedef in module.Types) { //only show visible concrete classes if (typedef.IsAbstract || !typedef.IsClass ||!typedef.IsPublic) continue; MC.TypeDefinition toolboxItem = null; string category = null; //find the ToolboxItem and Category attributes foreach (MC.CustomAttribute att in AllCustomAttributes (resolver, typedef)) { //find the value of the toolbox attribute if (toolboxItem == null && IsEqualOrSubclass (resolver, att.Constructor.DeclaringType, toolboxItemType)) { //check if it's ToolboxItemAttribute(false) (or null) IList args = GetArgumentsToBaseConstructor (att, toolboxItemType); if (args != null) { if (args.Count != 1) throw new InvalidOperationException ("Malformed toolboxitem attribute"); object o = args[0]; if (o == null || (o is bool && ((bool)o) == false)) { break; } else { //w have a type name for the toolbox item. try to resolve it string typeName = (string) o; toolboxItem = null; try { resolver.Resolve (TypeReferenceFromString (module, typeName)); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine (ex); } if (toolboxItem == null) { System.Diagnostics.Debug.WriteLine ( "CecilToolboxItemScanner: Error resolving type " + typeName); break; } } } } //find the value of the category attribute if (category == null && IsEqualOrSubclass (resolver, att.Constructor.DeclaringType, categoryType)) { IList args = GetArgumentsToBaseConstructor (att, categoryType); if (args != null && args.Count == 1) category = (string) args[0]; else throw new InvalidOperationException ("Malformed category attribute"); } if (toolboxItem != null && category != null) break; } if (toolboxItem == null) continue; Load (assem, typedef, toolboxItem, category); } } return list; }