Reflection across assemblies not working

  • Thread starter Thread starter Aashish
  • Start date Start date
A

Aashish

Hi,

A piece of my code is trying to instantiate classes that
can exist in different assemblies. However, its only able
to instantiate classes in the same assembly.

Example,

Assembly A - Class x,y
Assembly B - Class z, z1

Class x calls Class z that tries to instantiate Class y.
However, it fails at the first step of Type.GetType().

Any ideas why its failing?

Thanks
Aashish
 
Aashish said:
Hi,

A piece of my code is trying to instantiate classes that
can exist in different assemblies. However, its only able
to instantiate classes in the same assembly.

Example,

Assembly A - Class x,y
Assembly B - Class z, z1

Class x calls Class z that tries to instantiate Class y.
However, it fails at the first step of Type.GetType().

Any ideas why its failing?

Please post some code.

bye
Rob
 
-----Original Message-----


Please post some code.

bye
Rob
.

Hi Rob,

Here is the skeleton code.

Note A.X is class that B.Z is trying to instantiate. Also,
A.X is a subtype of B.Z

//Assembly A
using Z;

namespace A
{
public class X : Z
{
....

}

public class Y
{
public static void Main(string[] args)
{
//the following line works
Type xType = Type.GetType("A.X");

//Z is in diff assembly
X x = (X) Z.newInstance("A.X");

}

}
}


//assembly B
namespace B
{
public class Z
{

public static Z newInstance(string className)
{
Type clType = Type.GetType(className);
if(clType == null)
throw new ArgumentException("...");
//fails at above statement.

}
}

}
 
Hi Rob,

The anonymous posting was mine. Apologise for that. Here
is the code again.

//Z.cs
//csc /target:library /out:B.dll Z.cs

using System;

namespace B
{

public class Z
{

public static Z newInstance(string
className)
{

Type zType = Type.GetType(className);
if(zType == null)
{
throw new ArgumentException
("type is null");
}
return new Z();
}

}
}

---------------------

//X.cs
//csc /r:B.dll X.cs
//Then run the X.exe generated

using B;
using System;

namespace A
{
public class X : Z
{


}

public class Y
{

public static void Main(string[] args)
{
try
{
string clName = "A.X";
Z z = Z.newInstance
(clName);
}
catch(Exception ex)
{
Console.WriteLine
(ex.Message);
Console.WriteLine
(ex.StackTrace);
}
}
}

}

Thanks
Regards,
Aashish
 
Aashish said:
A piece of my code is trying to instantiate classes that
can exist in different assemblies. However, its only able
to instantiate classes in the same assembly.

Example,

Assembly A - Class x,y
Assembly B - Class z, z1

Class x calls Class z that tries to instantiate Class y.
However, it fails at the first step of Type.GetType().

Any ideas why its failing?

As documented, unless you specify which assembly you're interested in,
Type.GetType looks in mscorelib and the currently executing assembly.
Either specify the assembly in the call to Type.GetType, or call
Assembly.GetType.
 
Thanks Jon!

That works great. Am still migrating from Java and I
expected it to work like in Java where its possible to
load classes from any of the jars in the classpath.

Regards,
Aashish
 
Back
Top