2017-12-06 3 views
1

에서 역 참조됩니다 지금은 방법을 아래에있는 Null 참조 '#as (테스트, 0)'라인 I 클래스 이하가 28

public class Test1 
{ 
    public Test2 Test2 { get; set; } 
} 

public class Test2 { } 

,

private void Test() 
    { 
     var test = ConfigurationManager.GetSection("Test"); 

     if (test != null) 
     { 
      var a= (test as Test1).Test2; 
     } 
    } 

지금은 Klockwork 오류 말을 얻고,

Null reference '#as(test, 0)' that comes from line 28 will be dereferenced at line 28

이 오류는 무엇을 의미하고 난을 해결하는 방법 티?

참고 이것은 Klockwork 오류이지만 C# 컴파일은 오류가 발생하지 않습니다. 코드의 라인 아래에서

오류,

var a= (test as Test1).Test2;

답변

2
그것은 컴파일

하지만 프로그래머는 결과 값이 null이 될 수 있다고 as있는 기회. 이렇게하면 던지지 않도록 할 수 있습니다.

var test = ConfigurationManager.GetSection("Test") as Test1; 

    if (test != null) 
    { 
     var a = test.Test2; 
    }