Reflection on my own assmbly

  • Thread starter Thread starter ALI-R
  • Start date Start date
A

ALI-R

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:

Object reference not set to an instance of an object.



Why if I put for instance ,"Syste.Reflection.Assembly" instead of
"MyAssemblyName.ParameterTypeEnum",it works but it dosent work for mine.the
"ParameterTypeEnum"

is accessible in my code but when I try to extract its type,it shows that
error.



Thanks in advance
 
ALI-R said:
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:

Object reference not set to an instance of an object.

Why if I put for instance ,"Syste.Reflection.Assembly" instead of
"MyAssemblyName.ParameterTypeEnum",it works but it dosent work for mine.the
"ParameterTypeEnum"

is accessible in my code but when I try to extract its type,it shows that
error.

You shouldn't be specifying the assembly name - just the fully
qualified (with namespace) type name. Assemblies and namespaces are
different concepts.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Sorry ,by assembly name I meant just the fully qualified (with namespace)
type name.that type is in my Business logic layer (which is an
assebmly,ofcource) that's why I said Assembly.

Type theType =
Type.GetType("MyCompanyName.MyAppName.BLL.ParameterTypeEnum");

bool test= theType.IsEnum; <----------------I get the error here,seems that
the type hasn't been created at all.

thanks for your help
 
Bingo,,I think I have solved the problem:
I must have used Type theType = Type.GetType("ParameterTypeEnum") in the
context of my BLL ,because it is exactly what reflection is for,,I did
reflection on a type outside of BLL and in PL ,,and ofcourse it is not
possible .what I did was to create a property which returns
Type.GetType("ParameterTypeEnum") to my PL and it is working fine now.

Thanks for your help
ALi
 
ALI-R said:
Bingo,,I think I have solved the problem:
I must have used Type theType = Type.GetType("ParameterTypeEnum") in the
context of my BLL ,because it is exactly what reflection is for,,I did
reflection on a type outside of BLL and in PL ,,and ofcourse it is not
possible .what I did was to create a property which returns
Type.GetType("ParameterTypeEnum") to my PL and it is working fine now.

I'm glad you've sorted the problem, but for future reference you can
access a type in a different assembly - you either need to get the
reference to the assembly itself (loading it if necessary) and use
Assembly.GetType, or use Type.GetType but include the assembly name in
there as well.
 
I tried to load the assembly too,using
Assembly a=Assembly.Load("MyAssembly")
Type theType=a.GetType("ParameterTypeEnum");
but I still get that error.

Thanks for your help.
ALI
 
ALI-R said:
I tried to load the assembly too,using
Assembly a=Assembly.Load("MyAssembly")
Type theType=a.GetType("ParameterTypeEnum");
but I still get that error.

Is ParameterTypeEnum the fully qualified name (with namespace) of the
type though? It should be fine - at least if it's a public type.
 
In my app when I type ParameterTypeEnum ,it intellisense it and when I point
to it ,tooltip shows the complete path of it including the name space ,I
qualifiy it then ,but still nothing .here is my situation:

MarkT.ReportImager.BLL is the name of my business layer and this type is
defined in a class called ReportingSerivces in that layer ,so when I want to
qualify it ,I use "MarkT.ReportImager.BLL.ParameterTypeEnum",since it is a
public type in that layer,right???

Thanks for your continiously monitoring of this thread.

ALI
 
ALI-R said:
In my app when I type ParameterTypeEnum ,it intellisense it and when I point
to it ,tooltip shows the complete path of it including the name space ,I
qualifiy it then ,but still nothing .here is my situation:

MarkT.ReportImager.BLL is the name of my business layer and this type is
defined in a class called ReportingSerivces in that layer ,so when I want to
qualify it ,I use "MarkT.ReportImager.BLL.ParameterTypeEnum",since it is a
public type in that layer,right???

When you say that's the name of your business layer - is it the
namespace, or the assembly name? They're (pootentially) different
things.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
yes,that layer is completely in differnet namespace(different project) but
same solution .
I've referenced to that layer from my PL.
I think that's why I can't get its Type.
 
ALI-R said:
I can't do that because there are two different projects in one solution.

You can still post a short but complete program, even if it consists of
two classes - one in one assembly, one in the other.
 
ALI-R said:
yes,that layer is completely in differnet namespace(different project)

You still need to distinguish between namespaces and assemblies.
but same solution .
I've referenced to that layer from my PL.
I think that's why I can't get its Type.

You should be able to get the type from the other assembly though. Once
you've got a reference to the assembly (either using Assembly.Load or
finding the assembly of some other type you know the name of at compile
time, using typeof and then the Assembly property) and then
Assembly.GetType. You say this doesn't work - that's what I'd like to
see a sample of.
 
First of all I'd like to say that you have been so helpful,thank you very
much indeed,,here is what I have :

**** Description of the project******
I have a solution called Vision which consists of two projects:
1) Vision.ReportImager ((Windows Form Application(UI))
2)Vision.ReportImager.BLL((ClassLibrary(BLL)).In this project there is a
class called ReportingService.cs which containt that Enumuration,I am trying
to
get its type.
***** End of Description*********

=============start of Vision.ReportImager
====================================
using Vision.ReportImager.BLL;
namespace Vision.ReportImager
{
.....
private void ConfigureParameterPanel()
{
Type theType = Type.GetType("Vision.ReportImager.BLL.ParameterTypeEnum");
<---the Type is null after this line is executed
short
paramType=(short)Convert.ToInt16(Enum.Parse(theType,lstParameters.SelectedIt
ems[0].SubItems[1].Text));
}
.....
}end of Vision.ReportImager
=============End of of Vision.ReportImager
==================================
=============start of class ReportingService.cs in
Vision.ReportImager.BLL=============
using System;
namespace Vision.ReportImager.BLL
{
public class ReportingService :
System.Web.Services.Protocols.SoapHttpClientProtocol {.......... }
public class ReportParameter
{
public string Name;
public ParameterTypeEnum Type;
..........
}
public enum ParameterTypeEnum
{
Boolean,
DateTime,
Integer,
Float,
String,
}
} //End of Namespace Vision.ReportImager.BLL

=============End of class ReportingService.cs in
Vision.ReportImager.BLL=============
 
ALI-R said:
First of all I'd like to say that you have been so helpful,thank you very
much indeed,,here is what I have :

**** Description of the project******
I have a solution called Vision which consists of two projects:
1) Vision.ReportImager ((Windows Form Application(UI))
2)Vision.ReportImager.BLL((ClassLibrary(BLL)).In this project there is a
class called ReportingService.cs which containt that Enumuration,I am trying
to
get its type.
***** End of Description*********

You're back to the problem at the start of the thread - you're giving
the type name without the assembly name. If you find the assembly
first, and use Assembly.Gettype, it should be fine.

Alternatively, do you definitely not know the type ahead of time? If
you do, just use typeof instead.
 
Back
Top