473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Reflection across assemblies not working

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
Nov 16 '05 #1
5 1448
Aashish wrote:
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
Nov 16 '05 #2
-----Original Message-----
Aashish wrote:
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
.


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.

}
}

}


Nov 16 '05 #3
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
-----Original Message-----
Aashish wrote:
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
.

Nov 16 '05 #4
Aashish <aa**********@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
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
-----Original Message-----
Aashish <aa**********@hotmail.com> wrote:
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 callAssembly.GetType.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jozsef Bekes | last post by:
Hi All, I am trying to use reflection for getting all the classes from the dlls of a software written by my company. I am using this line: Assembly.LoadFile(s).GetTypes() Gettypes fails, I...
16
by: ALI-R | last post by:
I have defined a public enum in my assembly and I am trying to have access to it through reflection: Type theType = Type.GetType("MyAssemblyName.ParameterTypeEnum"); but I get this error: ...
12
by: Antony | last post by:
Hello - I am wanting to print out a "Yes" next to classes that implement "Interface01", otherwise a "No". Here is my code. It crashes with a null reference exception and I am not sure why. Ideas?...
2
by: Jeff | last post by:
I am trying to dynamically load an assembly via reflection and then invoke a method of that assembly that will populate a custom type collection passed into the method byref. I am able to...
9
by: TS | last post by:
i have code that creates assemblies and classes from the assemlby and methods from the classes to set properties of dynamically created controls. How i should go about validating the assemblies,...
7
by: Allan Ebdrup | last post by:
How do I get hold of the type when I have a string that represents the type. For example I have the string "OFiR.Recruitment.Department" And I want to get the type called...
13
by: jerryau | last post by:
Hi, I am trying to dynamically create object instances based on a string class name, and then I need to dynamically set values to these objects, but I have no idea how to do this in C#. Here is...
6
by: Cralis | last post by:
Hi guys, Someone once said, 'You can do that with reflection'. I can't recall what it was I was trying to do at the time, but then he said, 'Any developer knows what reflection is...'. I kept...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.