C# 中继器上的目标异常
我有一个简单的转发器绑定到一个对象列表,该对象包含一系列用于渲染的字符串 对象如下所示:C# 中继器上的目标异常,c#,sharepoint,C#,Sharepoint,我有一个简单的转发器绑定到一个对象列表,该对象包含一系列用于渲染的字符串 对象如下所示: class BestPractice { public string Title { get; set; } public string Author { get; set; } public string Body { get; set; } } 使用以下代码进行渲染,以确保循环不会导致问题: List<BestPractice>
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();
以下是例外情况:
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 app bin文件夹中(如果存在,请将其从该文件夹中删除),并将最新的构建版本复制到GAC。如果错误消失,请分别调整部署过程。该类不是公共类,但与循环位于同一命名空间中,因此循环运行时不会出现问题。似乎databind()代码虽然在正确的命名空间中激发,但实际上在另一个命名空间中执行,除非该类是公共的,否则databind()无法访问它。这看起来像是在编译时捕捉到的好东西,但我理解为什么不会。
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: