重复器上的TargetInvocationException
本文关键字:TargetInvocationException | 更新日期: 2023-09-27 18:17:23
我有一个简单的Repeater绑定到一个对象列表,该对象包含一系列用于呈现的字符串。
对象如下:
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();
我已经遍历了代码以确保List包含一个项目,并且该项目在绑定之前已经填充了字符串。异常发生在.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:
我不是100%确定具体问题是什么,但根据错误消息,我建议两个可能的更改:
1)验证最佳实践类是公开的(一旦您验证它是否有效,您可以将暴露水平降低)。
2)将自动实现的属性更改为成员支持属性。内部异常表明get_Author访问器上存在安全异常,这使我相信自动实现可能与此有关。
确保SharePoint可以看到最新版本的程序集。实现这一目标的最可靠的方法是确保dll不在web应用程序bin文件夹中(如果存在,请将其从那里删除),并将最新构建的版本复制到GAC。如果错误消失-分别调整您的部署过程