obtain ref to assembly from created class instance

  • Thread starter Thread starter John Grandy
  • Start date Start date
J

John Grandy

It does not seem possible to compare instances of assemblies from which
instances of controls/classes are created.

In other words, if I have

Assembly myAssembly Assembly.LoadFrom(path);
object myClass = myAssembly.CreateInstance("<ClassName>");

It is not possible to obtain a reference to myAssembly from myClass ...
 
It does not seem possible to compare instances of assemblies from which
instances of controls/classes are created.

In other words, if I have

Assembly myAssembly Assembly.LoadFrom(path);
object myClass = myAssembly.CreateInstance("<ClassName>");

It is not possible to obtain a reference to myAssembly from myClass ...

How about

Assembly a = myClass.GetType().Assembly;

?
 
I wonder if myClass.GetType().Assembly returns the assembly instance from
which the object was created ( via Assembly.CreateInstance() ), or if it
returns a new instance of the assembly associated with the object's type.

In other words, if two class instances were created respectively from two
instances of the same assembly, then does

myClass1.GetType().Assembly == myClass2.GetType().Assembly

return true or false ?
 
I wonder if myClass.GetType().Assembly returns the assembly instance from
which the object was created ( via Assembly.CreateInstance() ), or if it
returns a new instance of the assembly associated with the object's type.

In other words, if two class instances were created respectively from two
instances of the same assembly, then does

myClass1.GetType().Assembly == myClass2.GetType().Assembly

return true or false ?

True, always - and GetType() will always return the same Type reference
too.
 
Is there any other way to test whether two instances of a class were created
from different instances of the same assembly type ? Or do class instances
not have "memory" of how they were created ?
 
Is there any other way to test whether two instances of a class were created
from different instances of the same assembly type ? Or do class instances
not have "memory" of how they were created ?

No, they do:

if (a.GetType() != b.GetType())

will compare the types including which assemblies they were loaded
from.
 
Here is what I'm finding in practice :
Assembly assembly1 = Assembly.LoadFrom( assemblyPath );

Assembly assembly2 = Assembly.LoadFrom( assemblyPath );

object instance1 = assembly1.CreateInstance( className );

object instance2 = assembly2.CreateInstance( className );

Assert.IsTrue( instance1.GetType() == instance2.GetType(), "Assert.IsTrue:
instance1.GetType() == instance2.GetType()" );

The assert does not throw an exception.
 
Here is what I'm finding in practice :
Assembly assembly1 = Assembly.LoadFrom( assemblyPath );

Assembly assembly2 = Assembly.LoadFrom( assemblyPath );

object instance1 = assembly1.CreateInstance( className );

object instance2 = assembly2.CreateInstance( className );

Assert.IsTrue( instance1.GetType() == instance2.GetType(), "Assert.IsTrue:
instance1.GetType() == instance2.GetType()" );

The assert does not throw an exception.

I suspect you'll find that assembly1==assembly2 then as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top