/// <summary>Creates and returns a permission that is the intersection of the current permission and the specified permission.</summary> /// <returns>A new permission object that represents the intersection of the current permission and the specified permission. This new permission is null if the intersection is empty.</returns> /// <param name="target">A permission to intersect with the current permission. It must be of the same type as the current permission. </param> /// <exception cref="T:System.ArgumentException">The <paramref name="target" /> parameter is not null and is not of the same type as the current permission. </exception> public override IPermission Intersect(IPermission target) { SecurityPermission securityPermission = this.Cast(target); if (securityPermission == null) { return(null); } if (this.IsEmpty() || securityPermission.IsEmpty()) { return(null); } if (this.IsUnrestricted() && securityPermission.IsUnrestricted()) { return(new SecurityPermission(PermissionState.Unrestricted)); } if (this.IsUnrestricted()) { return(securityPermission.Copy()); } if (securityPermission.IsUnrestricted()) { return(this.Copy()); } SecurityPermissionFlag securityPermissionFlag = this.flags & securityPermission.flags; if (securityPermissionFlag == SecurityPermissionFlag.NoFlags) { return(null); } return(new SecurityPermission(securityPermissionFlag)); }
// // CodeAccessPermission methods // /* * IPermission interface implementation */ /// <include file='doc\SecurityPermission.uex' path='docs/doc[@for="SecurityPermission.IsSubsetOf"]/*' /> public override bool IsSubsetOf(IPermission target) { if (target == null) { return(m_flags == 0); } try { SecurityPermission operand = (SecurityPermission)target; if (operand.IsUnrestricted()) { return(true); } else if (this.IsUnrestricted()) { return(false); } else { return((((int)this.m_flags) & ~((int)operand.m_flags)) == 0); } } catch (InvalidCastException) { throw new ArgumentException( String.Format(Environment.GetResourceString("Argument_WrongType"), this.GetType().FullName) ); } }
public override IPermission Intersect(IPermission target) { if (target == null) { return(null); } if (!base.VerifyType(target)) { throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", new object[] { base.GetType().FullName })); } SecurityPermission permission = (SecurityPermission)target; SecurityPermissionFlag noFlags = SecurityPermissionFlag.NoFlags; if (permission.IsUnrestricted()) { if (this.IsUnrestricted()) { return(new SecurityPermission(PermissionState.Unrestricted)); } noFlags = this.m_flags; } else if (this.IsUnrestricted()) { noFlags = permission.m_flags; } else { noFlags = this.m_flags & permission.m_flags; } if (noFlags == SecurityPermissionFlag.NoFlags) { return(null); } return(new SecurityPermission(noFlags)); }
/// <summary>创建并返回一个权限,该权限是当前权限和指定权限的交集。</summary> /// <returns>一个新权限对象,它表示当前权限与指定权限的交集。如果交集为空,则此新权限为 null。</returns> /// <param name="target">要与当前权限相交的权限。它必须与当前权限属于同一类型。</param> /// <exception cref="T:System.ArgumentException"> /// <paramref name="target" /> 参数不是 null,而且与当前权限不是同一类型。</exception> public override IPermission Intersect(IPermission target) { if (target == null) { return((IPermission)null); } if (!this.VerifyType(target)) { throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", (object)this.GetType().FullName)); } SecurityPermission securityPermission = (SecurityPermission)target; SecurityPermissionFlag flag; if (securityPermission.IsUnrestricted()) { if (this.IsUnrestricted()) { return((IPermission) new SecurityPermission(PermissionState.Unrestricted)); } flag = this.m_flags; } else { flag = !this.IsUnrestricted() ? this.m_flags & securityPermission.m_flags : securityPermission.m_flags; } if (flag == SecurityPermissionFlag.NoFlags) { return((IPermission)null); } return((IPermission) new SecurityPermission(flag)); }
/// <summary>Determines whether the current permission is a subset of the specified permission.</summary> /// <returns>true if the current permission is a subset of the specified permission; otherwise, false.</returns> /// <param name="target">A permission that is to be tested for the subset relationship. This permission must be of the same type as the current permission. </param> /// <exception cref="T:System.ArgumentException">The <paramref name="target" /> parameter is not null and is not of the same type as the current permission. </exception> public override bool IsSubsetOf(IPermission target) { SecurityPermission securityPermission = this.Cast(target); if (securityPermission == null) { return(this.IsEmpty()); } return(securityPermission.IsUnrestricted() || (!this.IsUnrestricted() && (this.flags & ~securityPermission.flags) == SecurityPermissionFlag.NoFlags)); }
public override IPermission Intersect(IPermission target) { if (target == null) { return(null); } else if (!VerifyType(target)) { throw new ArgumentException( Environment.GetResourceString("Argument_WrongType", this.GetType().FullName) ); } SecurityPermission operand = (SecurityPermission)target; #pragma warning disable 618 SecurityPermissionFlag isectFlags = SecurityPermissionFlag.NoFlags; #pragma warning restore 618 if (operand.IsUnrestricted()) { if (this.IsUnrestricted()) { return(new SecurityPermission(PermissionState.Unrestricted)); } else #pragma warning disable 618 { isectFlags = (SecurityPermissionFlag)this.m_flags; } #pragma warning restore 618 } else if (this.IsUnrestricted()) { #pragma warning disable 618 isectFlags = (SecurityPermissionFlag)operand.m_flags; #pragma warning restore 618 } else { #pragma warning disable 618 isectFlags = (SecurityPermissionFlag)m_flags & (SecurityPermissionFlag)operand.m_flags; #pragma warning restore 618 } if (isectFlags == 0) { return(null); } else { return(new SecurityPermission(isectFlags)); } }
/// <summary> /// 创建并返回一个权限,该权限是当前权限和指定权限的交集。(替代 CodeAccessPermission.Intersect(IPermission)。) /// </summary> /// <param name="target">目标对象</param> /// <returns></returns> public override IPermission Intersect(IPermission target) { if (target == null)//如果目标对象为空,则返回为空 { return(null); } else if (!VerifyType(target))//如果格式不正确,则抛出Argument_WrongType的参数异常 { throw new ArgumentException( Environment.GetResourceString("Argument_WrongType", this.GetType().FullName) ); } SecurityPermission operand = (SecurityPermission)target;//将目标接口转化为SecurityPermission操作对象 #pragma warning disable 618 SecurityPermissionFlag isectFlags = SecurityPermissionFlag.NoFlags;//声明一个嵌入无安全性访问标志 #pragma warning restore 618 if (operand.IsUnrestricted()) //如果操作对象权限是无权限限制等级 { if (this.IsUnrestricted()) //如果本对象也为无权限限制等级,则返回一个无权限限制SecurityPermission对象 { return(new SecurityPermission(PermissionState.Unrestricted)); } else #pragma warning disable 618 { isectFlags = (SecurityPermissionFlag)this.m_flags;//将本对象标志赋值为嵌入标志 } #pragma warning restore 618 } else if (this.IsUnrestricted())//如果本对象是无权限限制等级,则将操作对象标志赋值给嵌入标志 { #pragma warning disable 618 isectFlags = (SecurityPermissionFlag)operand.m_flags; #pragma warning restore 618 } else { #pragma warning disable 618 isectFlags = (SecurityPermissionFlag)m_flags & (SecurityPermissionFlag)operand.m_flags;//返回本对象与目标对象的交集 #pragma warning restore 618 } if (isectFlags == 0) { return(null); } else { return(new SecurityPermission(isectFlags)); } }
/// <summary>Creates a permission that is the union of the current permission and the specified permission.</summary> /// <returns>A new permission that represents the union of the current permission and the specified permission.</returns> /// <param name="target">A permission to combine with the current permission. It must be of the same type as the current permission. </param> /// <exception cref="T:System.ArgumentException">The <paramref name="target" /> parameter is not null and is not of the same type as the current permission. </exception> public override IPermission Union(IPermission target) { SecurityPermission securityPermission = this.Cast(target); if (securityPermission == null) { return(this.Copy()); } if (this.IsUnrestricted() || securityPermission.IsUnrestricted()) { return(new SecurityPermission(PermissionState.Unrestricted)); } return(new SecurityPermission(this.flags | securityPermission.flags)); }
public override IPermission Intersect(IPermission target) { SecurityPermission sp = Cast(target); if (sp == null) { return(null); } if (IsEmpty() || sp.IsEmpty()) { return(null); } if (this.IsUnrestricted() && sp.IsUnrestricted()) { return(new SecurityPermission(PermissionState.Unrestricted)); } if (this.IsUnrestricted()) { return(sp.Copy()); } if (sp.IsUnrestricted()) { return(this.Copy()); } SecurityPermissionFlag f = flags & sp.flags; if (f == SecurityPermissionFlag.NoFlags) { return(null); } else { return(new SecurityPermission(f)); } }
public override IPermission Intersect(IPermission target) { if (target == null) { return(null); } else if (!VerifyType(target)) { throw new ArgumentException( String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), this.GetType().FullName) ); } SecurityPermission operand = (SecurityPermission)target; SecurityPermissionFlag isectFlags = SecurityPermissionFlag.NoFlags; if (operand.IsUnrestricted()) { if (this.IsUnrestricted()) { return(new SecurityPermission(PermissionState.Unrestricted)); } else { isectFlags = (SecurityPermissionFlag)this.m_flags; } } else if (this.IsUnrestricted()) { isectFlags = (SecurityPermissionFlag)operand.m_flags; } else { isectFlags = (SecurityPermissionFlag)m_flags & (SecurityPermissionFlag)operand.m_flags; } if (isectFlags == 0) { return(null); } else { return(new SecurityPermission(isectFlags)); } }
/// <summary>创建一个权限,该权限是当前权限与指定权限的并集。</summary> /// <returns>一个新权限,它表示当前权限与指定权限的并集。</returns> /// <param name="target">将与当前权限合并的权限。它必须与当前权限属于同一类型。</param> /// <exception cref="T:System.ArgumentException"> /// <paramref name="target" /> 参数不是 null,而且与当前权限不是同一类型。</exception> public override IPermission Union(IPermission target) { if (target == null) { return(this.Copy()); } if (!this.VerifyType(target)) { throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", (object)this.GetType().FullName)); } SecurityPermission securityPermission = (SecurityPermission)target; if (securityPermission.IsUnrestricted() || this.IsUnrestricted()) { return((IPermission) new SecurityPermission(PermissionState.Unrestricted)); } return((IPermission) new SecurityPermission(this.m_flags | securityPermission.m_flags)); }
public override bool IsSubsetOf(IPermission target) { SecurityPermission sp = Cast(target); if (sp == null) { return(IsEmpty()); } if (sp.IsUnrestricted()) { return(true); } if (this.IsUnrestricted()) { return(false); } return((flags & ~sp.flags) == 0); }
public override IPermission Union(IPermission target) { if (target == null) { return(this.Copy()); } if (!VerifyType(target)) { throw new ArgumentException( String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), this.GetType().FullName) ); } SecurityPermission sp_target = (SecurityPermission)target; if (sp_target.IsUnrestricted() || IsUnrestricted()) { return(new SecurityPermission(PermissionState.Unrestricted)); } SecurityPermissionFlag flag_union = (SecurityPermissionFlag)(m_flags | sp_target.m_flags); return(new SecurityPermission(flag_union)); }
public override IPermission Union(IPermission target) { if (target == null) { return(this.Copy()); } if (!VerifyType(target)) { throw new ArgumentException( Environment.GetResourceString("Argument_WrongType", this.GetType().FullName) ); } SecurityPermission sp_target = (SecurityPermission)target; if (sp_target.IsUnrestricted() || IsUnrestricted()) { return(new SecurityPermission(PermissionState.Unrestricted)); } #pragma warning disable 618 SecurityPermissionFlag flag_union = (SecurityPermissionFlag)(m_flags | sp_target.m_flags); #pragma warning restore 618 return(new SecurityPermission(flag_union)); }
/// <summary> /// 创建一个权限,该权限是当前权限与指定权限的并集。 (重写 CodeAccessPermission.Union(IPermission)。) /// </summary> /// <param name="target">目标对象</param> /// <returns></returns> public override IPermission Union(IPermission target) { if (target == null) { return(this.Copy()); } if (!VerifyType(target)) { throw new ArgumentException( Environment.GetResourceString("Argument_WrongType", this.GetType().FullName) ); } SecurityPermission sp_target = (SecurityPermission)target; //将目标转化为SecurityPermission格式 if (sp_target.IsUnrestricted() || IsUnrestricted()) //如果本对象或者目标对象有一个为无权限限制,则返回为无限制权限的SecurityPermission对象 { return(new SecurityPermission(PermissionState.Unrestricted)); } #pragma warning disable 618 SecurityPermissionFlag flag_union = (SecurityPermissionFlag)(m_flags | sp_target.m_flags);//按位运算,求出本对象与目标对象的交集 #pragma warning restore 618 return(new SecurityPermission(flag_union)); }
private void rtfcontrol_Load(object sender, System.EventArgs e) { //new FileIOPermission(PermissionState.Unrestricted).Assert(); PrincipalPermission P = new PrincipalPermission(PermissionState.Unrestricted); if(!P.IsUnrestricted()) Msg("Assigning Principal failed!"); else Msg("Assigning Principal passed!!"); SecurityPermission SP = new SecurityPermission(PermissionState.Unrestricted); if(!SP.IsUnrestricted()) Msg("Assigning SecurityPermission failed!"); else Msg("Assigning SecurityPermission passed!!"); try { SP.Assert(); } catch(Exception Err) { throw new Exception(Err.Message,Err.InnerException); } ListViewItem LVI = listView1.Items.Add(System.DateTime.Now.ToShortTimeString()); LVI.SubItems.Add("Contacting Server please wait..."); CodeAccessPermission.RevertAssert(); }
/// <summary> /// Workitem 7889: handle ERROR_LOCK_VIOLATION during read /// </summary> /// <remarks> /// This could be gracefully handled with an extension attribute, but /// This assembly is built for .NET 2.0, so I cannot use them. /// </remarks> internal static int ReadWithRetry(System.IO.Stream s, byte[] buffer, int offset, int count, string FileName) { int n = 0; bool done = false; #if !NETCF && !SILVERLIGHT int retries = 0; #endif do { try { n = s.Read(buffer, offset, count); done = true; } #if NETCF || SILVERLIGHT catch (System.IO.IOException) { throw; } #else catch (System.IO.IOException ioexc1) { // Check if we can call GetHRForException, // which makes unmanaged code calls. var p = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode); if (p.IsUnrestricted()) { uint hresult = _HRForException(ioexc1); if (hresult != 0x80070021) // ERROR_LOCK_VIOLATION throw new System.IO.IOException(String.Format("Cannot read file {0}", FileName), ioexc1); retries++; if (retries > 10) throw new System.IO.IOException(String.Format("Cannot read file {0}, at offset 0x{1:X8} after 10 retries", FileName, offset), ioexc1); // max time waited on last retry = 250 + 10*550 = 5.75s // aggregate time waited after 10 retries: 250 + 55*550 = 30.5s System.Threading.Thread.Sleep(250 + retries * 550); } else { // The permission.Demand() failed. Therefore, we cannot call // GetHRForException, and cannot do the subtle handling of // ERROR_LOCK_VIOLATION. Just bail. throw; } } #endif } while (!done); return n; }
public void PermissionState_Unrestricted () { PermissionState ps = PermissionState.Unrestricted; SecurityPermission sp = new SecurityPermission (ps); Assert.AreEqual (SecurityPermissionFlag.AllFlags, sp.Flags, "Flags"); Assert.IsTrue (sp.IsUnrestricted (), "IsUnrestricted"); SecurityElement se = sp.ToXml (); // only class and version are present Assert.AreEqual ("true", se.Attribute ("Unrestricted"), "Xml-Unrestricted"); Assert.IsNull (se.Children, "Xml-Children"); SecurityPermission copy = (SecurityPermission)sp.Copy (); Assert.IsFalse (Object.ReferenceEquals (sp, copy), "ReferenceEquals"); Assert.AreEqual (sp.Flags, copy.Flags, "Flags"); Assert.AreEqual (sp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()"); }