+ static Type HasExtensionAttribute (IList<CustomAttributeData> attributes)
+ {
+ foreach (var attr in attributes) {
+ var dt = attr.Constructor.DeclaringType;
+ if (dt.Name == "ExtensionAttribute" && dt.Namespace == "System.Runtime.CompilerServices") {
+ return dt;
+ }
+ }
+
+ return null;
+ }
+
+ public void ImportAssembly (Assembly assembly, Namespace targetNamespace)
+ {
+ Type extension_type = HasExtensionAttribute (CustomAttributeData.GetCustomAttributes (assembly));
+
+ //
+ // This part tries to simulate loading of top-level
+ // types only, any missing dependencies are ignores here.
+ // Full error report is reported later when the type is
+ // actually used
+ //
+ Type[] all_types;
+ try {
+ all_types = assembly.GetTypes ();
+ } catch (ReflectionTypeLoadException e) {
+ all_types = e.Types;
+ }
+
+ ImportTypes (all_types, targetNamespace, extension_type);
+ }
+
+ public void ImportModule (Module module, Namespace targetNamespace)
+ {
+ Type extension_type = HasExtensionAttribute (CustomAttributeData.GetCustomAttributes (module));
+
+ Type[] all_types;
+ try {
+ all_types = module.GetTypes ();
+ } catch (ReflectionTypeLoadException e) {
+ all_types = e.Types;
+ throw;
+ }
+
+ ImportTypes (all_types, targetNamespace, extension_type);
+ }
+
+ void ImportTypes (Type[] types, Namespace targetNamespace, Type extension_type)
+ {
+ Namespace ns = targetNamespace;
+ string prev_namespace = null;
+ foreach (var t in types) {
+ if (t == null)
+ continue;
+
+ // Be careful not to trigger full parent type loading
+ if (t.MemberType == MemberTypes.NestedType)
+ continue;
+
+ if (t.Name[0] == '<')
+ continue;
+
+ var it = CreateType (t, null, t, 0, true);
+ if (it == null)
+ continue;
+
+ if (prev_namespace != t.Namespace) {
+ ns = t.Namespace == null ? targetNamespace : targetNamespace.GetNamespace (t.Namespace, true);
+ prev_namespace = t.Namespace;
+ }
+
+ ns.AddType (it);
+
+ if (it.IsStatic && extension_type != null && t.IsDefined (extension_type, false)) {
+ it.SetExtensionMethodContainer ();
+ }
+ }
+ }
+
+ public TypeSpec ImportType (Type type)
+ {
+ return ImportType (type, null, 0);
+ }
+
+ public TypeSpec ImportType (Type type, ICustomAttributeProvider ca, int dynamicCursor)