Reflection across assemblies not working

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
 
R

Robert Jordan

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
 
G

Guest

-----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.

}
}

}
 
A

Aashish Patil

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
 
J

Jon Skeet [C# MVP]

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.
 
G

Guest

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
 

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

Top