Expression Body VS Block Body
Asked Answered
A

0

11

While coding, a discrepancy surfaced. Normally while coding simple methods or constructors I often utilize the expression body technique. However, when I produce the following:

public class Sample : ISample
{
     private readonly IConfigurationRoot configuration;

     public Sample(IConfigurationRoot configuration) => this.configuration = configuration;
}

The code appears to be valid, Visual Studio and compile both work. The issue though comes if inside that same class, I go to use the configuration variable. It produces "A field initializer cannot reference a non static field initializer."

That syntax usage that produced:

var example = configuration.GetSection("Settings:Key").Value;

However, if I leave the snippet above this and modify to a block body. Visual Studio no longer freaks out, why would an expression body cause such a peculiar error? While the block body works correctly with snippet above?

public class Sample : ISample
{
     private readonly IConfigurationRoot configuration;

     public Sample(IConfigurationRoot configuration)
     {
           this.configuration = configuration;
     }
}

public class ApplicationProvider
{
     public IConfigurationRoot Configuration { get; } = CreateConfiguration();

     public IServiceProvider BuildProvider()
     {
         var services = new ServiceCollection();
         DependencyRegistration(services);

         return services.AddLogging().BuildServiceProvider();
     }

     private IConfigurationRoot CreateConfiguration() => new ConfigurationBuilder()
     .SetBasePath(AppContext.BaseDirectory)
     .AddJsonFile("appsettings.json")
     .Build();

     private void DependencyRegistration(this IServiceCollection services)
     {
          services.AddSingleton(_ => Configuration);
          // All other dependency registration would also go here.
     }
}

public static IServiceProvider ServiceProvider { get; } = new ApplicationProvider().BuildProvider();

I would have an interface for class, then instantiate by pulling from the provider.

ISample sample = ServiceProvider.GetServices<ISample>();
Aquino answered 29/11, 2017 at 23:19 Comment(17)
@CalC Weird right? I'm trying to dig into the Microsoft Dependency Injection Framework for Core, thinking it might be there or in another location. But no matter what I do, that error appears inside this core 2.0 application.Aquino
Can you add more information showing how you create an instance of the Sample class?Fluidextract
Please show a minimal reproducible example. The code you've shown should be fine in a method. If you're trying to declare a field, you can't declare it with var anyway.Sized
@JonSkeet I'm confused by the var comment, the example I have with a var is trying to read the field to retrieve the value. Not per se use. Could you clarify please?Aquino
It's trying to declare a variable called example, which you can't do using var if you're trying to make that variable a field. Basically, we're guessing what your code really looks like because you haven't provided us with a minimal reproducible example. It's easy to stop the speculation: provide a complete example.Sized
@Aquino For example, I tried to replicate the issue last night. chat.stackoverflow.com/transcript/message/40245623#40245623 Using DotNetFiddle and the Roslyn compiler, I was able to get similar code to run without issue. However, I completely assumed the var example line was a member function or something similar. Also, if you are using a framework to perform the dependency injection for you -- as suggested by your comments -- that is important to know.Fluidextract
I've updated, it is Asp.Net Core 2.0 utilizing all of the new Core Microsoft.Extensions.Aquino
@Aquino Does Sample implement ISample? Assuming it does. Is that class definition located in a class? Otherwise, public static IServiceProvider ServiceProvider { get; } = new ApplicationProvider().BuildProvider(); doesn't work.Fluidextract
@TylerStahlhuth Yes, I updated question.Aquino
@Aquino I see a lot of problems with the code you provided, but am still futzing with trying to get AspNetCore to install so I am unable to test it. Are you sure the code provided compiles?Fluidextract
I ask because you have a non-static extension method in a non-static class.Fluidextract
@TylerStahlhuth I may have made typos, I did it really quick and a system went down, so I've been distracted. I'll fix in a bit. The static is in a whole different class.Aquino
@Aquino This Gist works just fine when I created a Asp project and used the classes without anything else going on. gist.github.com/anonymous/a9f7a9a85399a6f6e323896e737608b5Fluidextract
@TylerStahlhuth Hm, weird. I wonder why Visual Studio flags.Aquino
@Aquino Wait, it flags it? Does it actually build? Are you sure that error message isn't A field initializer cannot reference the non-static field, method, or property If so, it's this line that is the problem: public IConfigurationRoot Configuration { get; } = CreateConfiguration();Fluidextract
@TylerStahlhuth That line has been popping out at me. When I have a chance I'll let you know.Aquino
@Aquino The reason my version works is because I made the CreateConfiguration method static.Fluidextract

© 2022 - 2024 — McMap. All rights reserved.