我有一个嵌入在 HTML 页面(在 IE7 中)中的 .Net 2.0 activex 控件。我使用javascript来修改它的属性和调用方法。在我调用使用 Activator.CreateInstance(type) 实例化类的方法之前,这一切都很好。我收到以下消息:
System.Reflection.TargetInvocationException:调用的目标已引发异常。---> System.Security.SecurityException:请求失败。
..
..
失败的操作是:InheritanceDemand
第一个失败的权限类型是:System.Security.PermissionSet 失败
的程序集区域是:Intranet
我试图实例化的类有一个无参数的公共构造函数,从我读过的内容来看,对公共类型使用反射应该没有问题吗?
我使用 Microsoft .NET Framework 配置实用程序进行了临时修复,将 Intranet 信任修改为完全。见这里。
如何修改方法、类或程序集以避免必须配置框架?
一些额外的点:
- activex 控件是针对 .Net 2 编译的
- 它的组件不是强命名的
- 我不介意授予反射权限。
谢谢
更新
事实证明,导致问题的不是反射,而是对 TypeDescriptor.GetAttributes 的调用引发了 FileIOPermission 安全异常。我已经用以下代码解决了这个问题:
Dim temp As New Security.Permissions.FileIOPermission(Security.Permissions.PermissionState.Unrestricted)
temp.Assert()
// Get attributes
System.Security.CodeAccessPermission.RevertAssert()
现在,如果我设置一个分配给我的程序集强名称的代码组并将权限集设置为FullTrust,一切都很好。
但是,我似乎无法对其进行微调,它要么是FullTrust ,要么是抛出异常(见下文)。甚至Everything权限集也不起作用。
例外:
System.Security.SecurityException: Request failed.
at System.Reflection.CustomAttribute._CreateCaObject(Void* pModule, Void* pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
at System.Reflection.CustomAttribute.CreateCaObject(Module module, RuntimeMethodHandle ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
at System.Reflection.CustomAttribute.GetCustomAttributes(Module decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit)
at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectGetAttributes(Type type)
at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectedTypeData.GetAttributes()
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetAttributes()
at System.ComponentModel.TypeDescriptor.GetAttributes(Object component, Boolean noCustomTypeDesc)
at System.ComponentModel.TypeDescriptor.GetAttributes(Object component)
... GetAttributes
...
The action that failed was: InheritanceDemand
The type of the first permission that failed was: System.Security.PermissionSet
The Zone of the assembly that failed was: Intranet