2015-09-19 65 views
1
中的類類型屬性合同

將參數化對象分配給類類型屬性合同時出現錯誤。以下是代碼。如何將參數化構造函數對象分配給接口

internal interface IDisplayContents : IHttpHandler 
{ 
    MenuRoute propMenuRoute { get; set; } 
} 


public class MenuRoute 
{ 

    private string _OriginalRoute = string.Empty; 
    private string[] _RouteNodes = null; 
    private bool _RouteFound; 

    public string OriginalRoute 
    { 
     get { return _OriginalRoute; } 
    } 
    public string[] RouteNodes 
    { 
     get { return _RouteNodes; } 
    } 
    public bool RouteFound 
    { 
     get { return _RouteFound; } 
    } 

    public MenuRoute(string route, bool routeFound) 
    { 
     _RouteFound = routeFound; 
     _RouteNodes = GlobalObjects.SplitString(route,"/"); 
     if (_RouteNodes.Length .Equals(0)) 
      _RouteNodes = new string[] { "/" }; 


     for (int nodeIndex = 0; nodeIndex <= _RouteNodes.Length - 1; nodeIndex++) 
     { 

      _RouteNodes[nodeIndex] = GlobalObjects.SplitString(_RouteNodes[nodeIndex].ToString(), ".aspx")[0]; 

     } 
     _OriginalRoute = route; 

    } 

public class UrlRouteHandler : IRouteHandler 
{ 


    private string _virtualPath; 
    public UrlRouteHandler(string virtualPath) 
    { 
     _virtualPath = virtualPath; 
    } 

    public IHttpHandler GetHttpHandler(RequestContext requestContext) 
    { 



     IDisplayContents displayContent = BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(Page)) as IDisplayContents; 

     if (requestContext.RouteData.Values["notImplementedUrl"] != null) 
     { 
      displayContent.propMenuRoute = new MenuRoute("", false); 

     } 
     else 
     { 
      RouteValueDictionary routes = requestContext.RouteData.Values; 
      int intLevels = routes.Count; 
      string strRoute = string.Empty; 


      for (int intIterate = 1; intIterate <= intLevels; intIterate++) 
      { 
       strRoute += routes["level" + intIterate.ToString()] as string + "/"; 

      } 

      displayContent.propMenuRoute = new MenuRoute(strRoute, true); 
There comes the error 

System.NullReferenceException類型的例外,」發生但在用戶代碼沒有處理

其他信息:對象沒有設置爲一個對象的一個​​實例。 displayContent.MenuRoute =新MenuRoute(strRoute,真)

 } 

     return displayContent; 

    } 

} 

請幫

回答

0

我假設存在的錯誤信息和附加信息的錯字居然是:

附加信息:未將對象引用設置爲對象的實例。 displayContent.propMenuRoute =新MenuRoute(strRoute,真)

否則您的代碼將無法編譯,因爲IDisplayContents不具有財產MenuRoute


其中displayContent被設定唯一的地方是在

IDisplayContents displayContent = BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(Page)) as IDisplayContents; 

這意味着如果displayContentnull,那麼無論是CreateInstanceFromVirtualPath方法返回null或它返回不實現類型的對象接口爲IDisplayContents

雖然我不能確定不知道您的方法的實施,但似乎CreateInstanceFromVirtualPath返回Page對象和Page不實現IDisplayContents

相關問題