NickStrupat / NameOf

Provides strongly typed access to a compile-time string representing the name of a variable, field, property, method, event, enum, or type.
MIT License
20 stars 7 forks source link

Exception on static properties #4

Open MovGP0 opened 9 years ago

MovGP0 commented 9 years ago

I have the following code:

using System;

namespace NameOfTest
{
    public static class Foo
    {
        public static string Property { get; set; }
    }

    class Program
    {
        static void Main()
        {
            Console.WriteLine(Name.Of(Foo.Property));
        }
    }
}

I get the following build Error:

1>    Fody (version 1.27.1.0) Executing
1>MSBUILD : error : Fody: An unhandled exception occurred:
1>MSBUILD : error : Exception:
1>MSBUILD : error : Object reference not set to an instance of an object.
1>MSBUILD : error : StackTrace:
1>MSBUILD : error :    at NameOf.Fody.ModuleWeaver.Execute() in c:\Users\Nick\Documents\Visual Studio 2013\Projects\NameOf\NameOf.Fody\ModuleWeaver.cs:line 34
1>MSBUILD : error :    at lambda_method(Closure , Object )
1>MSBUILD : error :    at InnerWeaver.ExecuteWeavers(List`1 weaverInstances) in c:\ConsoleBuildAgent\work\ed448661dbb30d2e\FodyIsolated\InnerWeaver.cs:line 111
1>MSBUILD : error :    at InnerWeaver.Execute() in c:\ConsoleBuildAgent\work\ed448661dbb30d2e\FodyIsolated\InnerWeaver.cs:line 51
1>MSBUILD : error : Source:
1>MSBUILD : error : NameOf.Fody
1>MSBUILD : error : TargetSite:
1>MSBUILD : error : Void Execute()
1>MSBUILD : error : 
1>      Finished Fody 11ms.
NickStrupat commented 9 years ago

Are you using NameOf.Fody 0.8? I just tested this and it compiles and writes "Property" to the console at run-time.

MovGP0 commented 9 years ago

yes. I was using the version 0.8. strangely, I was able to compile one project, while a colleague of mine got the error. so I made a test project and got the same error.

also, non-static classes/properties seem to work. the issue is with static classes only.

NickStrupat commented 9 years ago

What version of VS are you using and what version of .NET are you targeting? If you can link me to your solution which is giving the error, I can check it out.

MovGP0 commented 9 years ago

I was able to replicate the error on VS 2012/.NET 4.0 and 2013/.NET 4.5; haven't tried 2015/.NET4.6 yet.

Might provide an example solution later today.

MovGP0 commented 9 years ago

The interesting thing is, that when I have a successful build and then change the code like the one above, it works. After cleaning or rebuilding it causes the above error.

The solution is here https://github.com/MovGP0/NameOfTest

NickStrupat commented 9 years ago

Confirmed the failure of your solution, modified to the code above, on my machine. I suspect a race condition. I'll check tonight. Thanks again for your help and patience!

NickStrupat commented 9 years ago

I reworked a few things and published a 0.8.2 NuGet package. I refactored the opcode groupings to prevent potential static initialization order bugs (I don't think this was happening, but it is safer now anyway), and I refactored a few things in the weaving code to avoid throwing NullReferenceException.

NickStrupat commented 9 years ago

Is your issue resolved? Can I close this?

MovGP0 commented 9 years ago

Unfortunately, this problem is still occuring on a different place now:

Error   757 Fody: An unhandled exception occurred:
Exception:
Object reference not set to an instance of an object.
StackTrace:
   at NameOf.Fody.ModuleWeaver.<IsNotUsed>b__7(MethodDefinition x) in c:\Users\nick.strupat\Documents\Visual Studio 2013\Projects\NameOf\NameOf.Fody\ModuleWeaver.cs:line 47
   at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at NameOf.Fody.ModuleWeaver.IsNotUsed(IMetadataTokenProvider methodDefinition) in c:\Users\nick.strupat\Documents\Visual Studio 2013\Projects\NameOf\NameOf.Fody\ModuleWeaver.cs:line 45
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at NameOf.Fody.ModuleWeaver.Execute() in c:\Users\nick.strupat\Documents\Visual Studio 2013\Projects\NameOf\NameOf.Fody\ModuleWeaver.cs:line 33
   at lambda_method(Closure , Object )
   at InnerWeaver.ExecuteWeavers() in c:\ConsoleBuildAgent\work\ed448661dbb30d2e\FodyIsolated\InnerWeaver.cs:line 144
   at InnerWeaver.Execute() in c:\ConsoleBuildAgent\work\ed448661dbb30d2e\FodyIsolated\InnerWeaver.cs:line 62
Source:
NameOf.Fody
TargetSite:
System.Collections.Generic.IEnumerable`1[Mono.Cecil.Cil.Instruction] <IsNotUsed>b__7(Mono.Cecil.MethodDefinition)