2011-01-19 6 views
0

기본 마스터 페이지와 콘텐츠 페이지가 있고 콘텐츠 페이지에서 마스터 페이지의 일부 속성이나 메서드를 사용하려는 경우 :CodeContracts : null 참조 'this.Master'에서 메서드 호출 가능

string something = this.Master.MasterPageFile; 

코드 정적 분석과 불평 계약합니다 :

warning : CodeContracts: Possibly calling a method on a null reference 'this.Master' 

어떻게 내가 마스터 페이지에 액세스 할 때마다 코드 계약은 불평을 중지 할 수 있습니까?

R.

[편집]

이 한 번 봐?

public partial class Manage : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     this.Master.ShowFeedback("Test.", true); 
    } 

    [ContractInvariantMethod] 
    private void ObjectInvariants() 
    { 
     Contract.Invariant(this.Master != null); 
    } 
} 
내가) Showfeedback을 (를 호출 할 때마다 다음해야 할 싶지 않아

:

Contract.Assume(this.Master != null) 
this.Master.ShowFeedback("Test.", true); 

합니까 의미가?

+0

당신은 this.Master'가 null되지 않습니다'있도록 수 있을까? –

답변

0

당신은 당신의 클래스에 불변로 추가 할 수 있습니다 :

[ContractInvariantMethod] 
private void Invariants() 
{ 
    Contract.Invariant(Master != null); 
} 
+0

이미 시도했지만 코드 계약은 경고와 함께 돌아 왔습니다. CodeContracts : invariant unproven : this.Master! = null – Richard

+0

끝 부분에서? 생성자? – porges

+0

예제를 보여주기 위해 코드를 편집했습니다. 이상한 일은 때때로 그 전화가 경고를 유발하지 않을 때입니다. 하지만 전화를하기 전에 방법에 액세스하면 경고가 확실히 발동됩니다. 혼란스러워. – Richard