{"id":309212,"date":"2020-08-30T21:00:39","date_gmt":"2020-08-30T21:00:39","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=309212"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=309212","title":{"rendered":"\u041a\u0430\u043a \u00ab\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c\u00bb nullable reference types \u0441 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c appsettings.json"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/517102\/\">\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0441\u0432\u043e\u0438\u043c\u0438 \u0440\u0430\u0437\u043c\u044b\u0448\u043b\u0435\u043d\u0438\u044f\u043c\u0438 \u043e \u0442\u043e\u043c, \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u043d\u0430 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c C# \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434, \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u043e\u0442 NullReferenceException. \u042d\u0442\u043e\u0442 \u0437\u043b\u043e\u0432\u0440\u0435\u0434\u043d\u044b\u0439 \u0442\u0438\u043f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043d\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443, \u0432 \u043a\u0430\u043a\u043e\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u043c\u0435\u0441\u0442\u0435 \u0443 \u043d\u0435\u0433\u043e null. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043e\u0442 \u043e\u0442\u0447\u0430\u044f\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e?.\u043d\u0430\u0447\u0430\u0442\u044c?.\u043f\u0438\u0441\u0430\u0442\u044c?.\u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435?.\u043a\u043e?.\u0432\u0441\u0435\u043c?.\u043f\u043e\u043b\u044f\u043c?.\u0432\u043e\u0442?.\u0442\u0430\u043a?.\u0432\u043e\u0442, \u043d\u043e \u0435\u0441\u0442\u044c \u0430\u0434\u0435\u043a\u0432\u0430\u0442\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u0442\u0438\u043f\u043e\u0432 \u043e\u0442 <a href=\"https:\/\/www.jetbrains.com\/help\/resharper\/Reference__Code_Annotation_Attributes.html\" rel=\"nofollow\">JetBrains<\/a> \u0438\u043b\u0438 <a href=\"https:\/\/docs.microsoft.com\/ru-ru\/dotnet\/csharp\/tutorials\/nullable-reference-types\" rel=\"nofollow\">Microsoft<\/a>. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0430\u0447\u043d\u0435\u0442 \u043d\u0430\u043c \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c (\u0438 \u00ab\u043f\u043e\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u00bb \u043e\u0447\u0435\u043d\u044c \u043d\u0430\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e, \u0435\u0441\u043b\u0438 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043e\u043f\u0446\u0438\u044e WarningsAsError), \u0432 \u043a\u0430\u043a\u043e\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u043c\u0435\u0441\u0442\u0435 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443.<\/p>\n<p>  \u041d\u043e \u0432\u0441\u0435 \u043b\u0438 \u0442\u0430\u043a \u0433\u043b\u0430\u0434\u043a\u043e? \u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c \u0438 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/bc\/s_\/g3\/bcs_g3ye82g3rgvgzhllfkkdz3e.png\"><br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h2>\u041f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438<\/h2>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: <i>\u041f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432\u0435\u0441\u044c \u043a\u043e\u0434 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430: <\/i><\/p>\n<pre><code class=\"xml\">&lt;TreatWarningsAsErrors&gt;true&lt;\/TreatWarningsAsErrors&gt; &lt;Nullable&gt;enable&lt;\/Nullable&gt; <\/code><\/pre>\n<p>  \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u0435\u043c\u0443 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b:<\/p>\n<pre><code class=\"cs\">    public sealed class SomeClient     {         private readonly SomeClientOptions options;          public SomeClient(SomeClientOptions options)         {             this.options = options;         }          public void SendSomeRequest()         {             Console.WriteLine($&quot;Do work with { this.options.Login.ToLower() }&quot; +                 $&quot; and { this.options.CertificatePath.ToLower() }&quot;);         }     } <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u0431\u044b \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u043d\u0435\u043a\u0438\u0439 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u0438 \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u043c\u0443 \u043a\u043e\u0434\u0443, \u0447\u0442\u043e \u043e\u043d \u043d\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c Login \u0438 CertificatePath \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 null. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441 SomeClientOptions \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0430\u043a-\u0442\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">    public sealed class SomeClientOptions     {         public string Login { get; set; }          public string CertificatePath { get; set; }          public SomeClientOptions(string login, string certificatePath)         {             Login = login;             CertificatePath = certificatePath;         }     } <\/code><\/pre>\n<p>  \u0412\u0442\u043e\u0440\u043e\u0435 \u0432\u043f\u043e\u043b\u043d\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 \u043a \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e \u0432 \u0446\u0435\u043b\u043e\u043c (\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u044f asp.net core): \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043d\u0430\u0448 SomeClientOptions \u0438\u0437 \u043a\u0430\u043a\u043e\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c json \u0444\u0430\u0439\u043b\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0434\u0435\u043f\u043b\u043e\u044f. <\/p>\n<p>  \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043e\u0434\u043d\u043e\u0438\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u0435\u043a\u0446\u0438\u044e \u0432 appsettings.json:<\/p>\n<pre><code class=\"json\">{   &quot;SomeClientOptions&quot;: {     &quot;Login&quot;: &quot;ferzisdis&quot;,     &quot;CertificatePath&quot;:  &quot;.\\full_access.pfx&quot;   } } <\/code><\/pre>\n<p>  \u041d\u0443 \u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u043e\u043f\u0440\u043e\u0441: \u043a\u0430\u043a \u043d\u0430\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 SomeClientOptions \u0438 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u0441\u0435 NotNull \u043f\u043e\u043b\u044f \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c null \u043d\u0435 \u043f\u0440\u0438 \u043a\u0430\u043a\u0438\u0445 \u043e\u0431\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430\u0445?<\/p>\n<h2>\u041d\u0430\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u043f\u044b\u0442\u043a\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b<\/h2>\n<p>  \u041c\u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0431\u043b\u043e\u043a \u043a\u043e\u0434\u0430, \u0430 \u043d\u0435 \u0441\u0442\u0440\u043e\u0447\u0438\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u044e \u043d\u0430 \u0425\u0430\u0431\u0440:<\/p>\n<pre><code class=\"cs\">    public class Startup     {         public Startup(IConfiguration configuration)         {             Configuration = configuration;         }          public IConfiguration Configuration { get; }          public void ConfigureServices(IServiceCollection services)         {             var options = Configuration.GetSection(nameof(SomeClientOptions)).Get&lt;SomeClientOptions&gt;();             services.AddSingleton(options);         }     } <\/code><\/pre>\n<p>  \u041d\u043e \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0435\u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u0435\u043d, \u0442.\u043a. \u043c\u0435\u0442\u043e\u0434 Get() \u043d\u0430\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442 \u0440\u044f\u0434 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043d\u0430 \u0442\u0438\u043f, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442: <\/p>\n<ul>\n<li>\u0422\u0438\u043f T \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043d\u0435\u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u043c \u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0431\u0435\u0437 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432<\/li>\n<li>\u0413\u0435\u0442\u0435\u0440\u044b \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439<\/li>\n<\/ul>\n<p>  \u0423\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f, \u043c\u044b \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u044b \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 SomeClientOptions \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"cs\">public sealed class SomeClientOptions     {         private string login = null!;         private string certificatePath = null!;          public string Login         {             get             {                 return login;             }             set             {                 login = !string.IsNullOrEmpty(value) ? value : throw new InvalidOperationException($&quot;{nameof(Login)} cannot be null!&quot;);             }         }          public string CertificatePath         {             get             {                 return certificatePath;             }             set             {                 certificatePath = !string.IsNullOrEmpty(value) ? value : throw new InvalidOperationException($&quot;{nameof(CertificatePath)} cannot be null!&quot;);             }         }     } <\/code><\/pre>\n<p>  \u0414\u0443\u043c\u0430\u044e, \u0432\u044b \u0441\u043e \u043c\u043d\u043e\u0439 \u0441\u043e\u0433\u043b\u0430\u0441\u0438\u0442\u0435\u0441\u044c, \u0442\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0438 \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u043c, \u043d\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c. \u041a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0442\u0438\u043f \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0443 SomeClient \u2014 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0434\u0430\u043d\u043e \u043d\u0438 \u0435\u0434\u0438\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u044f, \u0430 \u0432 \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0437\u0430\u0432\u0435\u0442\u043d\u044b\u0439 NRE.<\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: <i>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 null \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c string.IsNullOrEmpty(), \u0442.\u043a. \u0432 \u0431\u043e\u043b\u044c\u0448\u0435\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043f\u0443\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043d\u0435\u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/i><\/p>\n<h2>\u0410\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0448\u0435<\/h2>\n<p>  \u041f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0440\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u043c\u0435\u044e\u0442 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0435 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438.<\/p>\n<p>  \u041c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c SomeClientOptions \u043d\u0430 \u0434\u0432\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0433\u0434\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0430 \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e:<\/p>\n<pre><code class=\"cs\">    public sealed class SomeClientOptionsRaw     {         public string? Login { get; set; }          public string? CertificatePath { get; set; }     }      public sealed class SomeClientOptions : ISomeClientOptions     {         private readonly SomeClientOptionsRaw raw;          public SomeClientOptions(SomeClientOptionsRaw raw)         {             this.raw = raw;         }          public string Login             =&gt; !string.IsNullOrEmpty(this.raw.Login) ? this.raw.Login : throw new InvalidOperationException($&quot;{nameof(Login)} cannot be null!&quot;);          public string CertificatePath             =&gt; !string.IsNullOrEmpty(this.raw.CertificatePath) ? this.raw.CertificatePath : throw new InvalidOperationException($&quot;{nameof(CertificatePath)} cannot be null!&quot;);     }      public interface ISomeClientOptions     {         public string Login { get; }          public string CertificatePath { get; }     } <\/code><\/pre>\n<p>  \u0421\u0447\u0438\u0442\u0430\u044e \u044d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0438 \u0438\u0437\u044f\u0449\u043d\u044b\u043c, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0443 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0430 \u043e\u0434\u0438\u043d \u043a\u043b\u0430\u0441\u0441 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0431\u043e\u0440 \u0441\u0432\u043e\u0439\u0441\u0442\u0432. <\/p>\n<p>  \u0413\u043e\u0440\u0430\u0437\u0434\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0435\u0435 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 SomeClient \u0432\u043c\u0435\u0441\u0442\u043e SomeClientOptions \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 ISomeClientOptions (\u043a\u0430\u043a \u043c\u044b \u0443\u0431\u0435\u0434\u0438\u043b\u0438\u0441\u044c, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u0441\u0438\u043b\u044c\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0435\u0442\u044c \u043e\u0442 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f). <\/p>\n<p>  \u0412\u0442\u043e\u0440\u043e\u0439 (\u043c\u0435\u043d\u0435\u0435 \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u044b\u0439) \u0441\u043f\u043e\u0441\u043e\u0431 \u2014 \u0432\u044b\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0442\u044c \u00ab\u0432\u0440\u0443\u0447\u043d\u0443\u044e\u00bb \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u0437 IConfiguration: <\/p>\n<pre><code class=\"cs\">    public sealed class SomeClientOptions : ISomeClientOptions     {         private readonly IConfiguration configuration;          public SomeClientOptions(IConfiguration configuration)         {             this.configuration = configuration;         }          public string Login =&gt; GetNotNullValue(nameof(Login));          public string CertificatePath =&gt; GetNotNullValue(nameof(CertificatePath));          private string GetNotNullValue(string propertyName)         {             var value = configuration[$&quot;{nameof(SomeClientOptions)}:{propertyName}&quot;];             return !string.IsNullOrEmpty(value) ? value : throw new InvalidOperationException($&quot;{propertyName} cannot be null!&quot;);         }     } <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043c\u043d\u0435 \u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u0438\u0437-\u0437\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u043f\u0430\u0440\u0441\u0438\u043d\u0433\u0430 \u0438 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438 \u0442\u0438\u043f\u043e\u0432.<\/p>\n<p>  \u041a \u0442\u043e\u043c\u0443 \u0436\u0435, \u0432\u044b \u043d\u0435 \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0443\u0436 \u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u0434\u043b\u044f \u0442\u0430\u043a\u043e\u0439 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438?<\/p>\n<h2>\u041a\u0430\u043a \u043d\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0443\u043a\u0430\u043c\u0438 \u043b\u0438\u0448\u043d\u0438\u0439 \u043a\u043e\u0434?<\/h2>\n<p>  \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0434\u043b\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 ISomeClientOptions \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0432 runtime, \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0443\u044e \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0447\u0443 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u043b\u0438\u0448\u044c \u043a\u043e\u043d\u0446\u0435\u043f\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u0442\u0435\u043c\u0430 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u043e, \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u044e nuget-\u043f\u0430\u043a\u0435\u0442 \u0434\u043b\u044f \u0431\u043e\u0435\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f (\u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0430\u043c\u0438 \u043d\u0430 \u0433\u0438\u0442\u0445\u0430\u0431\u0435).<\/p>\n<p>  \u0414\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u044f \u0440\u0430\u0437\u0431\u0438\u043b \u0432\u0441\u044e \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0443 \u043d\u0430 3 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0447\u0430\u0441\u0442\u0438:<\/p>\n<ol>\n<li>\u0421\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f runtime \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430<\/li>\n<li>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u043c\u0438 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438<\/li>\n<li>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u043d\u0430 null (\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u044b \u043a\u0430\u043a NotNull)<\/li>\n<\/ol>\n<p>  <\/p>\n<pre><code class=\"cs\">    public static class ConfigurationExtensions     {         private static readonly InterfaceImplementationBuilder InterfaceImplementationBuilder = new InterfaceImplementationBuilder();         private static readonly NullReferenceValidator NullReferenceValidator = new NullReferenceValidator();          public static T GetOptions&lt;T&gt;(this IConfiguration configuration, string sectionName)         {             var implementationOfInterface = InterfaceImplementationBuilder.BuildClass&lt;T&gt;();             var options = configuration.GetSection(sectionName).Get(implementationOfInterface);             NullReferenceValidator.CheckNotNullProperties&lt;T&gt;(options);              return (T) options;         }     } <\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">InterfaceImplementationBuilder<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">    public sealed class InterfaceImplementationBuilder     {         private readonly Lazy&lt;ModuleBuilder&gt; _module;          public InterfaceImplementationBuilder()         {             _module = new Lazy&lt;ModuleBuilder&gt;(() =&gt; AssemblyBuilder                 .DefineDynamicAssembly(new AssemblyName(Guid.NewGuid().ToString()), AssemblyBuilderAccess.Run)                 .DefineDynamicModule(&quot;MainModule&quot;));         }          public Type BuildClass&lt;TInterface&gt;()         {             return BuildClass(typeof(TInterface));         }          public Type BuildClass(Type implementingInterface)         {             if (!implementingInterface.IsInterface)             {                 throw new InvalidOperationException(&quot;Only interface is supported&quot;);             }              var typeBuilder = DefineNewType(implementingInterface.Name);              ImplementInterface(typeBuilder, implementingInterface);              return typeBuilder.CreateType() ?? throw new InvalidOperationException(&quot;Cannot build type!&quot;);         }          private void ImplementInterface(TypeBuilder typeBuilder, Type implementingInterface)         {             foreach (var propertyInfo in implementingInterface.GetProperties())             {                 DefineNewProperty(typeBuilder, propertyInfo.Name, propertyInfo.PropertyType);             }                          typeBuilder.AddInterfaceImplementation(implementingInterface);         }             private TypeBuilder DefineNewType(string baseName)         {             return _module.Value.DefineType($&quot;{baseName}_{Guid.NewGuid():N}&quot;);         }          private static void DefineNewProperty(TypeBuilder typeBuilder, string propertyName, Type propertyType)         {             FieldBuilder fieldBuilder = typeBuilder.DefineField(&quot;_&quot; + propertyName, propertyType, FieldAttributes.Private);              PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null);             MethodBuilder getPropMthdBldr = typeBuilder.DefineMethod(&quot;get_&quot; + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual, propertyType, Type.EmptyTypes);             ILGenerator getIl = getPropMthdBldr.GetILGenerator();              getIl.Emit(OpCodes.Ldarg_0);             getIl.Emit(OpCodes.Ldfld, fieldBuilder);             getIl.Emit(OpCodes.Ret);              MethodBuilder setPropMthdBldr =                 typeBuilder.DefineMethod(&quot;set_&quot; + propertyName,                     MethodAttributes.Public                     | MethodAttributes.SpecialName                     | MethodAttributes.HideBySig                     | MethodAttributes.Virtual,                     null, new[] { propertyType });              ILGenerator setIl = setPropMthdBldr.GetILGenerator();             Label modifyProperty = setIl.DefineLabel();             Label exitSet = setIl.DefineLabel();              setIl.MarkLabel(modifyProperty);             setIl.Emit(OpCodes.Ldarg_0);             setIl.Emit(OpCodes.Ldarg_1);             setIl.Emit(OpCodes.Stfld, fieldBuilder);              setIl.Emit(OpCodes.Nop);             setIl.MarkLabel(exitSet);             setIl.Emit(OpCodes.Ret);              propertyBuilder.SetGetMethod(getPropMthdBldr);             propertyBuilder.SetSetMethod(setPropMthdBldr);         }     } <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">NullReferenceValidator<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">    public sealed class NullReferenceValidator     {         public void CheckNotNullProperties&lt;TInterface&gt;(object options)         {             var propertyInfos = typeof(TInterface).GetProperties();             foreach (var propertyInfo in propertyInfos)             {                 if (propertyInfo.PropertyType.IsValueType)                 {                     continue;                 }                  if (!IsNullable(propertyInfo) &amp;&amp; IsNull(propertyInfo, options))                 {                     throw new InvalidOperationException($&quot;Property {propertyInfo.Name} cannot be null!&quot;);                 }             }         }          private bool IsNull(PropertyInfo propertyInfo, object obj)         {             var value = propertyInfo.GetValue(obj);              switch (value)             {                 case string s: return string.IsNullOrEmpty(s);                 default: return value == null;             }         }          \/\/ https:\/\/stackoverflow.com\/questions\/58453972\/how-to-use-net-reflection-to-check-for-nullable-reference-type         private bool IsNullable(PropertyInfo property)         {             if (property.PropertyType.IsValueType)             {                 throw new ArgumentException(&quot;Property must be a reference type&quot;, nameof(property));             }              var nullable = property.CustomAttributes                 .FirstOrDefault(x =&gt; x.AttributeType.FullName == &quot;System.Runtime.CompilerServices.NullableAttribute&quot;);             if (nullable != null &amp;&amp; nullable.ConstructorArguments.Count == 1)             {                 var attributeArgument = nullable.ConstructorArguments[0];                 if (attributeArgument.ArgumentType == typeof(byte[]) &amp;&amp; attributeArgument.Value != null)                 {                     var args = (ReadOnlyCollection&lt;CustomAttributeTypedArgument&gt;)attributeArgument.Value;                     if (args.Count &gt; 0 &amp;&amp; args[0].ArgumentType == typeof(byte))                     {                         return (byte)args[0].Value == 2;                     }                 }                 else if (attributeArgument.ArgumentType == typeof(byte))                 {                     return (byte)attributeArgument.Value == 2;                 }             }              var context = property.DeclaringType.CustomAttributes                 .FirstOrDefault(x =&gt; x.AttributeType.FullName == &quot;System.Runtime.CompilerServices.NullableContextAttribute&quot;);             if (context != null &amp;&amp;                 context.ConstructorArguments.Count == 1 &amp;&amp;                 context.ConstructorArguments[0].ArgumentType == typeof(byte) &amp;&amp;                 context.ConstructorArguments[0].Value != null)             {                 return (byte)context.ConstructorArguments[0].Value == 2;             }              \/\/ Couldn't find a suitable attribute             return false;         }     } <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:  <\/p>\n<pre><code class=\"cs\">    public class Startup     {         public Startup(IConfiguration configuration)         {             Configuration = configuration;         }          public IConfiguration Configuration { get; }          public void ConfigureServices(IServiceCollection services)         {             var options = Configuration.GetOptions&lt;ISomeClientOptions&gt;(&quot;SomeClientOptions&quot;);             services.AddSingleton(options);         }     } <\/code><\/pre>\n<p>  <\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 nullabe reference types \u043d\u0435 \u0442\u0430\u043a \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u043e, \u043a\u0430\u043a \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434. \u042d\u0442\u043e\u0442 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043b\u0438\u0448\u044c \u0441\u043d\u0438\u0437\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e NRE, \u0430 \u043d\u0435 \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043d\u0438\u0445 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e. \u0414\u0430 \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0435\u0449\u0435 \u043d\u0435 \u0430\u043d\u043d\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0434\u043e\u043b\u0436\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c.<\/p>\n<p>  \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0443\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0432\u0430\u043c \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u043b\u0430\u0441\u044c \u0441\u0442\u0430\u0442\u044c\u044f.<\/p>\n<p>  \u0420\u0430\u0441\u0441\u043a\u0430\u0436\u0438\u0442\u0435, \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u043b\u0438 \u0432\u044b \u0441 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 \u0438 \u043a\u0430\u043a \u043e\u0431\u0445\u043e\u0434\u0438\u043b\u0438 \u0435\u0435. \u0411\u0443\u0434\u0443 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u0435\u043d \u0437\u0430 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043a \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044e.<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/517102\/\"> https:\/\/habr.com\/ru\/post\/517102\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/517102\/\">\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0441\u0432\u043e\u0438\u043c\u0438 \u0440\u0430\u0437\u043c\u044b\u0448\u043b\u0435\u043d\u0438\u044f\u043c\u0438 \u043e \u0442\u043e\u043c, \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u043d\u0430 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c C# \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434, \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u043e\u0442 NullReferenceException. \u042d\u0442\u043e\u0442 \u0437\u043b\u043e\u0432\u0440\u0435\u0434\u043d\u044b\u0439 \u0442\u0438\u043f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043d\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443, \u0432 \u043a\u0430\u043a\u043e\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u043c\u0435\u0441\u0442\u0435 \u0443 \u043d\u0435\u0433\u043e null. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043e\u0442 \u043e\u0442\u0447\u0430\u044f\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e?.\u043d\u0430\u0447\u0430\u0442\u044c?.\u043f\u0438\u0441\u0430\u0442\u044c?.\u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435?.\u043a\u043e?.\u0432\u0441\u0435\u043c?.\u043f\u043e\u043b\u044f\u043c?.\u0432\u043e\u0442?.\u0442\u0430\u043a?.\u0432\u043e\u0442, \u043d\u043e \u0435\u0441\u0442\u044c \u0430\u0434\u0435\u043a\u0432\u0430\u0442\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u0442\u0438\u043f\u043e\u0432 \u043e\u0442 <a href=\"https:\/\/www.jetbrains.com\/help\/resharper\/Reference__Code_Annotation_Attributes.html\" rel=\"nofollow\">JetBrains<\/a> \u0438\u043b\u0438 <a href=\"https:\/\/docs.microsoft.com\/ru-ru\/dotnet\/csharp\/tutorials\/nullable-reference-types\" rel=\"nofollow\">Microsoft<\/a>. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043d\u0430\u0447\u043d\u0435\u0442 \u043d\u0430\u043c \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c (\u0438 \u00ab\u043f\u043e\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u00bb \u043e\u0447\u0435\u043d\u044c \u043d\u0430\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e, \u0435\u0441\u043b\u0438 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043e\u043f\u0446\u0438\u044e WarningsAsError), \u0432 \u043a\u0430\u043a\u043e\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u043c\u0435\u0441\u0442\u0435 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443.<\/p>\n<p>  \u041d\u043e \u0432\u0441\u0435 \u043b\u0438 \u0442\u0430\u043a \u0433\u043b\u0430\u0434\u043a\u043e? \u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c \u0438 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/bc\/s_\/g3\/bcs_g3ye82g3rgvgzhllfkkdz3e.png\">  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-309212","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/309212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=309212"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/309212\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=309212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=309212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=309212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}