¿Por qué Assembly.GetType () refleja el ensamblaje Reflections cuando se carga un ensamblaje diferente?

I am new to using Reflection but trying to understand the behavior of GetType()

Tengo lo siguiente:

assembly = Assembly.LoadFile(@"D:\ObjectRelations.dll");             
Type type = assembly.GetType("ObjectRelations.JSHierarchyBuilder");               
MethodInfo[] methods = type.GetMethods();

 foreach (MethodInfo method in methods)
     .... List Some info, etc.
catch ...

While this yields the expected output for what's contained in my Assembly, using an alternative of

     Type type = assembly.GetType();

Gives me the methods associated with the Reflection Assembly and not the Assembly that is designated in the LoadFile method which isn't really what I'd expected or would have thought would be a practical behavior; else why would I need to specify any file to load at all if the return Type would be that of the Reflection Assembly. Kinda wierd to me.

Anyway, if this is correct; and seeing that my 1st example worked as intended. Then how would one acquire any information for example "The Methods" of an Assembly without precisely knowing the Namespace/and or Class Name of the Assembly which could be different than the name of the Assembly?

I am presuming that Reflection should allow you to examine this type of information even if you have no foreknowledge of what's under the hood of an assembly and that the Dynamic aspect of instantiating objects at runtime isn't the "only" benefit.

Could someone plz expound? Or am I going about this all wrong.

preguntado el 16 de mayo de 11 a las 16:05

What type do you expect it to return? -

As assembly isn't a class and doesn't have any methods. -

2 Respuestas

Cuando usted llama:

 Type type = assembly.GetType();

You're actually calling the Object.GetType() method on Assembly, which returns typeof(Assembly).

Sospecho que querías:

 Type[] types = assembly.GetTypes();

This will return the collection of all types defined within the assembly.

The difference here is that, in the first case you're asking the variable (assembly) for it's type, which is Assembly. In the second, you're asking the instance for the list of types that are defined within the assembly itself.

contestado el 16 de mayo de 11 a las 20:05

You're calling the standard Object.GetType() method, which gives you the runtime type of the instance it's called on.

Podrías estar buscando Assembly.GetTypes() (plural)

contestado el 16 de mayo de 11 a las 20:05

Thanks Guys, it actually makes sense for Object.GetType() but I didn't consider that because of the fact that assembly = Assembly.LoadFile(@"D:\ObjectRelations.dll"); So I expected "assembly" to be a reference to the loaded assembly and not a self reference of base Object type. Hidden in plain site so to say. ;-) I just tried using the array of GetTypes() and see that there is still some parsing that needs to take place to get the actual Assembly Name that I want from the list but it's definitely a start. Thanks! - Mark

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.