code within libraries vs code within same project

G

Guest

What are some possible reasons for this:

I call a static method from a class within my assembly...works fine.
I call the same static method after it has been compiled into a different
assembly and referenced to my project....does't work. Give's me a "Object
not set to an instance...".

What are some possible reasons for this? I'm just giving the assembly a
strong name, compiling it into a dll and adding it as a reference into my
project. Why would this work any differently?
 
J

Jon Skeet [C# MVP]

melinda said:
What are some possible reasons for this:

I call a static method from a class within my assembly...works fine.
I call the same static method after it has been compiled into a different
assembly and referenced to my project....does't work. Give's me a "Object
not set to an instance...".

What are some possible reasons for this? I'm just giving the assembly a
strong name, compiling it into a dll and adding it as a reference into my
project. Why would this work any differently?

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

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I bet your problem has nothing to do with the fact the type is in a dll,
most probably it's cause you are using some resource particular to the .exe
that is not accesible to the dll.
Are you using any Environment, AppSetting or something like that?

Take a look at the null object , in the Exception.StackTrace you will get
the line number of the error.

if this do not help just post the code

cheers,
 
G

Guest

Well, there was too much code to add but it is dying in this static method
that is located in the dll... I believe it dies when it calls
sf.GetFileName()....but not sure.

private static string Header()
{
// Create a StackTrace that captures
// filename, line number and column
// information, for the current thread.
// 2 stack frames up because we were called by the Log method,
which was
// called by the method we want info about.
System.Diagnostics.StackTrace st = new
System.Diagnostics.StackTrace(2, true);

// High up the call stack, there is only one stack frame
System.Diagnostics.StackFrame sf = st.GetFrame(0);

// remove all characters leading up to the last directory path
separator
string shortFileName = sf.GetFileName().Remove(
0, // Starting character
sf.GetFileName().LastIndexOfAny( new
char[]{System.IO.Path.DirectorySeparatorChar} ) + 1 // For this many
);

return string.Format(
"{0:T}, {1} {2}, {3}: ",
System.DateTime.Now,
shortFileName,
sf.GetFileLineNumber(),
sf.GetMethod());
}
 
G

Guest

apperantly the file names don't exist in the frame properties (in the
StackTrace Object) that belong to the dll. Kind of what you were saying
Ignacio. I'm not sure why though.

melinda said:
Well, there was too much code to add but it is dying in this static method
that is located in the dll... I believe it dies when it calls
sf.GetFileName()....but not sure.

private static string Header()
{
// Create a StackTrace that captures
// filename, line number and column
// information, for the current thread.
// 2 stack frames up because we were called by the Log method,
which was
// called by the method we want info about.
System.Diagnostics.StackTrace st = new
System.Diagnostics.StackTrace(2, true);

// High up the call stack, there is only one stack frame
System.Diagnostics.StackFrame sf = st.GetFrame(0);

// remove all characters leading up to the last directory path
separator
string shortFileName = sf.GetFileName().Remove(
0, // Starting character
sf.GetFileName().LastIndexOfAny( new
char[]{System.IO.Path.DirectorySeparatorChar} ) + 1 // For this many
);

return string.Format(
"{0:T}, {1} {2}, {3}: ",
System.DateTime.Now,
shortFileName,
sf.GetFileLineNumber(),
sf.GetMethod());
}



Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

I bet your problem has nothing to do with the fact the type is in a dll,
most probably it's cause you are using some resource particular to the .exe
that is not accesible to the dll.
Are you using any Environment, AppSetting or something like that?

Take a look at the null object , in the Exception.StackTrace you will get
the line number of the error.

if this do not help just post the code

cheers,
 
G

Guest

sorry for so many posts, but I still have one question....
Is this a bug in System.Diagnostics.StackTrace?
I call
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(2, true);
//at this point st.FrameCount is 4, although only 3 have filenames (All
frames that
//appear that are part of the .exe display properly, but the one's that are
part of
//the .dll do not appear to have all the info, particularly filename.
System.Diagnostics.StackFrame sf = st.GetFrame(0);
//now sf is the one without a filename, it's null, it happens to be the
first frame that is part of the dll.
sf.GetFileName(); // this throws an exception, becasue filename is null.

First off, why is filename not there? I'm building with debug on and not
optimized.
Second off, why is filename null? This seems like a bug, but I'm not an
expert on what's considered a bug.
 

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