Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null Reference Exception with System.Reflection.Assembly

I have developed a Library for internal email reporting. When I am using that Library from another project (By Adding Reference).

It gives NullReferenceException on the following line.

System.Reflection.Assembly.GetEntryAssembly().GetName().Name

Any idea, why Assembly is null?

like image 374
Scorpion Avatar asked Feb 25 '11 12:02

Scorpion


People also ask

How do I fix null reference exception in unity?

This error is caused when an object is trying to be used by a script but does not refer to an instance of an object. To fix this example we can acquire a reference to an instance of the script using GameObject. Find to find the object it is attached to.

How do you avoid null reference exception?

Use Null Coalescing to Avoid NullReferenceExceptions It works with all nullable data types. The following code throws an exception without the null coalescing. Adding “?? new List<string>()” prevents the “Object reference not set to an instance of an object” exception.

What is a null reference exception?

A NullReferenceException exception is thrown when you try to access a member on a type whose value is null . A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios: You've forgotten to instantiate a reference type.

How do you solve this system NullReferenceException object reference not set to an instance of an object?

The best way to avoid the "NullReferenceException: Object reference not set to an instance of an object” error is to check the values of all variables while coding. You can also use a simple if-else statement to check for null values, such as if (numbers!= null) to avoid this exception.


2 Answers

This is expected especially in the Windows Services where they are loaded by an unmanaged runtime.

Use:

  Process.GetCurrentProcess().MainModule.FileName

To get unmanaged entry point file.


Update

It seems you are looking for this:

  System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
like image 76
Aliostad Avatar answered Oct 13 '22 11:10

Aliostad


problem is solved guys,

I am using

Assembly.GetAssembly(ex.TargetSite.DeclaringType.UnderlyingSystemType).GetName().Name 

to get the EntryAssemblyName.
In this case I already has parameter which is taking Exception 'ex', so I solved it by using that.

Many Thanks Guys, specially @Aliostad

Cheers

like image 35
Scorpion Avatar answered Oct 13 '22 12:10

Scorpion