2011-12-09 32 views
0

我已綁定到對象的列表的簡單轉發器,包含一系列字符串用於渲染的對象。TargetInvocationException上中繼器

的對象是:

class BestPractice 
    { 
     public string Title { get; set; } 
     public string Author { get; set; } 
     public string Body { get; set; } 
    } 

使用下面的代碼,以確保循環並沒有造成問題呈現:

List<BestPractice> BestPractices = new List<BestPractice>(); 
foreach (SPListItem item in items) 
       { 
        BestPractice bp = new BestPractice(); 
        bp.Author = "test";//(string)item["Author"]; 
        bp.Body = "test";// (string)item["Body"]; 
        bp.Title = "test";// (string)item["Title"]; 
        BestPractices.Add(bp); 
       } 

       BPRepeater.DataSource = BestPractices; 
       BPRepeater.DataBind(); 

我已經通過代碼加強,以確保列表包含一個項目,並且該項目在數據綁定之前填充了字符串。唯一的例外發生在這裏的.g.cs文件:

[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)] 
     public void @__DataBind__control4(object sender, System.EventArgs e) { 
      System.Web.UI.WebControls.RepeaterItem Container; 
      System.Web.UI.DataBoundLiteralControl target; 
      target = ((System.Web.UI.DataBoundLiteralControl)(sender)); 
      Container = ((System.Web.UI.WebControls.RepeaterItem)(target.BindingContainer)); 
      target.SetDataBoundString(0, System.Convert.ToString(DataBinder.Eval(Container.DataItem, "Author"), System.Globalization.CultureInfo.CurrentCulture)); 
     } 

以下是例外本身:

System.Reflection.TargetInvocationException was unhandled by user code 
    Message=Property accessor 'Author' on object 'Carpool_Webparts.Offer_Details.BestPractice' threw the following exception:'Carpool_Webparts.Offer_Details.BestPractice.get_Author()' 
    Source=System 
    StackTrace: 
     at System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) 
     at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) 
     at System.Web.UI.DataBinder.Eval(Object container, String[] expressionParts) 
     at Carpool_Webparts.Offer_Details.Offer_Details.__DataBind__control4(Object sender, EventArgs e) 
     at System.Web.UI.Control.OnDataBinding(EventArgs e) 
     at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) 
     at System.Web.UI.Control.DataBindChildren() 
    InnerException: System.MethodAccessException 
     Message=Carpool_Webparts.Offer_Details.BestPractice.get_Author() 
     Source=mscorlib 
     StackTrace: 
      at System.Reflection.MethodBase.PerformSecurityCheck(Object obj, RuntimeMethodHandle method, IntPtr parent, UInt32 invocationFlags) 
      at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) 
      at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
      at System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) 
     InnerException: System.Security.SecurityException 
      Message=Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. 
      Source=mscorlib 
      StackTrace: 
       at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Assembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed) 
       at System.Security.CodeAccessSecurityEngine.CheckSetHelper(PermissionSet grants, PermissionSet refused, PermissionSet demands, RuntimeMethodHandle rmh, Object assemblyOrString, SecurityAction action, Boolean throwException) 
       at System.Security.PermissionSetTriple.CheckSetDemand(PermissionSet demandSet, PermissionSet& alteredDemandset, RuntimeMethodHandle rmh) 
       at System.Security.PermissionListSet.CheckSetDemand(PermissionSet pset, RuntimeMethodHandle rmh) 
       at System.Security.PermissionListSet.DemandFlagsOrGrantSet(Int32 flags, PermissionSet grantSet) 
       at System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper(Int32 permission, PermissionSet targetGrant, CompressedStack securityContext) 
       at System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper(Int32 permission, PermissionSet targetGrant) 
      InnerException: 

我完全失去了:(

回答

1

我不是100%肯定什麼具體的問題,但是基於錯誤信息,我建議兩種可能的變化:

1)驗證BestPractice類是公共的(你可以衝高曝光量回落,一旦你驗證它是否工作)。

2)更改自動實現的屬性到構件支持的特性。內部異常表明get_Author訪問器存在安全異常,這使我相信自動實現可能與它有關。

+0

類是不公開的,但在相同的命名空間環路,所以循環運行,沒有問題。看起來好像databind()代碼雖然在正確的命名空間中被觸發,但實際上在另一箇中執行,除非該類是公共的,否則databind()不能訪問它。這似乎是在編譯時很好的東西,但我明白爲什麼它不會。 – Wesley

1

確保裝配的最新鮮的版本是可見到SharePoint。最可靠的方法來實現這一目標是確保DLL沒有在Web應用程序的bin文件夾(從那裏刪除它,如果它是存在的),並內置最新版本複製到GAC。如果錯誤消失 - 請分別調整您的部署過程。

相關問題