[Mono-list] [patch] System.Reflection.Assemly.GetModules & GetReferencedAssemblies.

Tomi Pakarinen tomi.pakarinen@welho.com
26 Aug 2002 23:03:57 +0300


--=-faHZLF0Uwysajlz7O+B7
Content-Type: text/plain
Content-Transfer-Encoding: 7bit


 Hi.

I've implemented two methods:
  System.Reflection.Assembly.GetModules()
  System.Reflection.Assembly.GetReferencedAssemblies()

GetModules implementation is not complete, because i couldn't find, how
to make a difference between resource modules and other kind of modules.
System.Reflection.Module.IsResource is unimplemented.

I've attached patches and a simple test program. I'm bit suspicious of
my mono_assemblyname_get_object & mono_module_get_object
implementations. I'm not sure, how those are supposed to be implemented.



 Tomi.









--=-faHZLF0Uwysajlz7O+B7
Content-Disposition: attachment; filename=GetModules-mcs.diff
Content-Transfer-Encoding: quoted-printable
Content-Type: text/x-patch; name=GetModules-mcs.diff; charset=ISO-8859-1

Index: class/corlib/System.Reflection/Assembly.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /mono/mcs/class/corlib/System.Reflection/Assembly.cs,v
retrieving revision 1.29
diff -u -r1.29 Assembly.cs
--- class/corlib/System.Reflection/Assembly.cs	21 Aug 2002 11:04:50 -0000	1=
.29
+++ class/corlib/System.Reflection/Assembly.cs	26 Aug 2002 19:13:42 -0000
@@ -282,9 +282,12 @@
 			throw new NotImplementedException ();
 		}
=20
+		[MethodImplAttribute (MethodImplOptions.InternalCall)]
+		public extern Module[] GetModules (bool include_resources);
+
 		public Module[] GetModules ()
 		{
-			throw new NotImplementedException ();
+			return GetModules (true);
 		}
=20
 		public Module GetModule (String name)
@@ -301,10 +304,8 @@
 		[MethodImplAttribute (MethodImplOptions.InternalCall)]
 		public extern static Assembly GetCallingAssembly ();
=20
-		public AssemblyName[] GetReferencedAssemblies ()
-		{
-			throw new NotImplementedException ();
-		}
+		[MethodImplAttribute (MethodImplOptions.InternalCall)]
+		public extern AssemblyName[] GetReferencedAssemblies ();
=20
 		public virtual ManifestResourceInfo GetManifestResourceInfo (String reso=
urceName)
 		{

--=-faHZLF0Uwysajlz7O+B7
Content-Disposition: attachment; filename=GetModules-mono.diff
Content-Transfer-Encoding: quoted-printable
Content-Type: text/x-patch; name=GetModules-mono.diff; charset=ISO-8859-1

Index: mono/metadata/icall.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /mono/mono/mono/metadata/icall.c,v
retrieving revision 1.198
diff -u -r1.198 icall.c
--- mono/metadata/icall.c	24 Aug 2002 16:48:44 -0000	1.198
+++ mono/metadata/icall.c	25 Aug 2002 20:04:26 -0000
@@ -2032,6 +2032,33 @@
 }
=20
=20
+static MonoArray*
+ves_icall_System_Reflection_Assembly_GetReferencedAssemblies (MonoReflecti=
onAssembly *assembly)
+{
+	MonoDomain *domain =3D mono_domain_get();
+	MonoArray *res;
+	MonoClass *klass;
+	int i, count =3D 0;
+	MonoAssembly **refs =3D assembly->assembly->image->references;
+
+	if (refs)=20
+	{
+		while (refs[count])
+			++count;
+	}
+
+	klass =3D mono_class_from_name ( mono_defaults.corlib, "System.Reflection=
", "AssemblyName");
+
+	res =3D mono_array_new (domain, klass, count);
+
+	for ( i =3D 0; i < count; ++i )
+		mono_array_set (res, gpointer, i, mono_assemblyname_get_object (domain, =
&refs[i]->aname));
+
+	return res;
+}
+
+
+
 static MonoReflectionAssembly*
 ves_icall_System_Reflection_Assembly_GetCallingAssembly (void)
 {
@@ -2043,6 +2070,35 @@
 	return mono_assembly_get_object (mono_domain_get (), dest->klass->image->=
assembly);
 }
=20
+static MonoArray*
+ves_icall_System_Reflection_Assembly_GetModules (MonoReflectionAssembly* a=
ssembly, MonoBoolean include_resource_modules )
+{
+	MonoDomain *domain =3D mono_domain_get();
+	MonoArray *res;
+	MonoClass *klass;
+	int i, count =3D 0;
+	MonoImage **modules =3D assembly->assembly->modules;
+
+	if (modules)=20
+	{
+		while (modules[count])
+			++count;
+	}
+
+	g_assert( assembly->assembly->image !=3D NULL );
+	++count;
+
+	klass =3D mono_class_from_name ( mono_defaults.corlib, "System.Reflection=
", "Module");
+	res =3D mono_array_new (domain, klass, count);
+
+	mono_array_set (res, gpointer, 0, mono_module_get_object (domain, assembl=
y->assembly->image));
+	for ( i =3D 1; i < count; ++i )=20
+		mono_array_set (res, gpointer, i, mono_module_get_object (domain, module=
s[i]));
+
+	return res;
+}
+
+
 static MonoString *
 ves_icall_System_MonoType_getFullName (MonoReflectionType *object)
 {
@@ -2865,8 +2921,10 @@
 	"System.Reflection.Assembly::FillName", ves_icall_System_Reflection_Assem=
bly_FillName,
 	"System.Reflection.Assembly::get_code_base", ves_icall_System_Reflection_=
Assembly_get_code_base,
 	"System.Reflection.Assembly::GetExecutingAssembly", ves_icall_System_Refl=
ection_Assembly_GetExecutingAssembly,
+	"System.Reflection.Assembly::GetReferencedAssemblies", ves_icall_System_R=
eflection_Assembly_GetReferencedAssemblies,
 	"System.Reflection.Assembly::GetEntryAssembly", ves_icall_System_Reflecti=
on_Assembly_GetEntryAssembly,
 	"System.Reflection.Assembly::GetCallingAssembly", ves_icall_System_Reflec=
tion_Assembly_GetCallingAssembly,
+	"System.Reflection.Assembly::GetModules", ves_icall_System_Reflection_Ass=
embly_GetModules,
 	"System.Reflection.Assembly::get_EntryPoint", ves_icall_System_Reflection=
_Assembly_get_EntryPoint,
 	"System.Reflection.Assembly::GetManifestResourceNames", ves_icall_System_=
Reflection_Assembly_GetManifestResourceNames,
 	"System.Reflection.Assembly::GetManifestResourceInternal", ves_icall_Syst=
em_Reflection_Assembly_GetManifestResourceInternal,
Index: mono/metadata/reflection.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /mono/mono/mono/metadata/reflection.c,v
retrieving revision 1.106
diff -u -r1.106 reflection.c
--- mono/metadata/reflection.c	14 Aug 2002 15:40:31 -0000	1.106
+++ mono/metadata/reflection.c	25 Aug 2002 20:04:27 -0000
@@ -2692,6 +2692,62 @@
 	return res;
 }
=20
+
+/*
+ * mono_assemblyname_get_object:
+ * @domain: an app domain
+ * @assembly: an assembly_name
+ *
+ * Return an System.Reflection.AssemblyName object representing the MonoAs=
semblyName @assembly_name.
+ */
+MonoReflectionAssemblyName*=20
+mono_assemblyname_get_object (MonoDomain *domain, MonoAssemblyName *assemb=
ly_name)
+{
+	static MonoClass *System_Reflection_AssemblyName;
+	MonoReflectionAssemblyName *res;
+=09
+	CHECK_OBJECT (MonoReflectionAssemblyName *, assembly_name, NULL);
+	if (!System_Reflection_AssemblyName)
+		System_Reflection_AssemblyName =3D mono_class_from_name (
+			mono_defaults.corlib, "System.Reflection", "AssemblyName");
+	res =3D (MonoReflectionAssemblyName *)mono_object_new (domain, System_Ref=
lection_AssemblyName);
+
+	res->name     =3D mono_string_new (domain, assembly_name->name);
+	res->codebase =3D NULL; /* FIXME */
+
+	res->major =3D assembly_name->major;
+	res->minor =3D assembly_name->minor;
+	res->build =3D assembly_name->build;
+	res->revision =3D assembly_name->revision;
+
+	CACHE_OBJECT (assembly_name, res, NULL);
+	return res;
+}
+
+MonoReflectionModule*  =20
+mono_module_get_object   (MonoDomain *domain, MonoImage *image)
+{
+	static MonoClass *System_Reflection_Module;
+	MonoReflectionModule *res;
+=09
+	CHECK_OBJECT (MonoReflectionModule *, image, NULL);
+	if (!System_Reflection_Module)
+		System_Reflection_Module =3D mono_class_from_name (
+			mono_defaults.corlib, "System.Reflection", "Module");
+	res =3D (MonoReflectionModule *)mono_object_new (domain, System_Reflectio=
n_Module);
+
+	res->image =3D image;
+	res->assembly =3D (MonoReflectionAssembly *) mono_assembly_get_object(dom=
ain, image->assembly);
+
+	res->fqname    =3D mono_string_new (domain, image->name);
+	res->name      =3D mono_string_new (domain, image->name);
+	res->scopename =3D mono_string_new (domain, image->module_name);
+
+	CACHE_OBJECT (image, res, NULL);
+	return res;
+}
+
+
 static gboolean
 mymono_metadata_type_equal (MonoType *t1, MonoType *t2)
 {
Index: mono/metadata/reflection.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /mono/mono/mono/metadata/reflection.h,v
retrieving revision 1.54
diff -u -r1.54 reflection.h
--- mono/metadata/reflection.h	14 Aug 2002 15:40:31 -0000	1.54
+++ mono/metadata/reflection.h	25 Aug 2002 20:04:27 -0000
@@ -325,7 +325,7 @@
 typedef struct {
 	MonoObject	obj;
 	MonoImage  *image;
-	MonoObject *assembly;
+	MonoReflectionAssembly *assembly;
 	MonoString *fqname;
 	MonoString *name;
 	MonoString *scopename;
@@ -367,6 +367,16 @@
 	MonoString *codebase;
 	gint32 major, minor, build, revision;
 	/* FIXME: add missing stuff */
+/*	CultureInfo cultureinfo;
+	AssemblyNameFlags flags;
+	AssemblyHashAlgorithm hashalg;
+	StrongNameKeyPair keypair;
+	AssemblyVersionCompatibility versioncompat;*/
+	MonoObject  *cultureInfo;
+	MonoObject  *flags;
+	MonoObject  *hashalg;
+	MonoObject  *keypair;
+	MonoObject  *versioncompat;
 } MonoReflectionAssemblyName;
=20
 typedef struct {
@@ -419,6 +429,8 @@
 guint32       mono_image_create_token  (MonoDynamicAssembly *assembly, Mon=
oObject *obj);
=20
 MonoReflectionAssembly* mono_assembly_get_object (MonoDomain *domain, Mono=
Assembly *assembly);
+MonoReflectionAssemblyName* mono_assemblyname_get_object (MonoDomain *doma=
in, MonoAssemblyName *assembly_name);
+MonoReflectionModule*   mono_module_get_object   (MonoDomain *domain, Mono=
Image *image);
 MonoReflectionType*     mono_type_get_object     (MonoDomain *domain, Mono=
Type *type);
 MonoReflectionMethod*   mono_method_get_object   (MonoDomain *domain, Mono=
Method *method, MonoClass *refclass);
 MonoReflectionField*    mono_field_get_object    (MonoDomain *domain, Mono=
Class *klass, MonoClassField *field);

--=-faHZLF0Uwysajlz7O+B7
Content-Disposition: attachment; filename=GetModules_test.cs
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; name=GetModules_test.cs; charset=ISO-8859-1

// created on 20.8.2002 at 21:13=0D
using System;=0D
using System.Reflection;=0D
=0D
class TestiMain {=0D
=0D
	static int Main( string [] args )=0D
	{=0D
		System.Reflection.Assembly assembly;=0D
		System.Reflection.AssemblyName[] assemblyNames;=0D
		System.Reflection.Module[] modules;=0D
	=09=0D
		assembly =3D System.Reflection.Assembly.GetEntryAssembly();=0D
		assemblyNames =3D assembly.GetReferencedAssemblies();=0D
	=09=0D
		Console.WriteLine( "\nassemblyNames.Lenght =3D " + assemblyNames.Length )=
;=0D
		Console.WriteLine( "assemblyNames.GetLenght(0) =3D " + assemblyNames.GetL=
ength(0) );=0D
		Console.WriteLine( "assemblyNames.Rank =3D " + assemblyNames.Rank );=0D
	=09=0D
		foreach (System.Reflection.AssemblyName assemblyName in assemblyNames) {=0D
			Console.WriteLine( "GetReferencesAssemblies =3D " +assemblyName.ToString=
() );=0D
		}=0D
=0D
		modules =3D assembly.GetModules();=0D
=0D
		Console.WriteLine( "\nmodules.Lenght =3D " + modules.Length );=0D
		Console.WriteLine( "modules.GetLenght(0) =3D " + modules.GetLength(0) );=0D
		Console.WriteLine( "modules.Rank =3D " + modules.Rank );=0D
=0D
		Console.WriteLine( "GetModules =3D " +modules.ToString() );=0D
		foreach (System.Reflection.Module module in modules) {=0D
			Console.WriteLine( module.ToString() );=0D
		}=0D
=0D
		return 0;=0D
	}=0D
}=0D

--=-faHZLF0Uwysajlz7O+B7--