Inheritance: CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission
Example #1
0
        /// <summary>Determines whether the current permission is a subset of the specified permission.</summary>
        /// <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>
        /// <returns>
        ///     <see langword="true" /> if the current permission is a subset of the specified permission; otherwise, <see langword="false" />.</returns>
        /// <exception cref="T:System.ArgumentException">The <paramref name="target" /> parameter is not <see langword="null" /> and is not of the same type as the current permission. </exception>
        // Token: 0x060025F3 RID: 9715 RVA: 0x00088858 File Offset: 0x00086A58
        public override bool IsSubsetOf(IPermission target)
        {
            if (target == null)
            {
                return(this.m_flags == ReflectionPermissionFlag.NoFlags);
            }
            bool result;

            try
            {
                ReflectionPermission reflectionPermission = (ReflectionPermission)target;
                if (reflectionPermission.IsUnrestricted())
                {
                    result = true;
                }
                else if (this.IsUnrestricted())
                {
                    result = false;
                }
                else
                {
                    result = ((this.m_flags & ~reflectionPermission.m_flags) == ReflectionPermissionFlag.NoFlags);
                }
            }
            catch (InvalidCastException)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", new object[]
                {
                    base.GetType().FullName
                }));
            }
            return(result);
        }
		public void ReflectionEmit () 
		{
			ReflectionPermission p = new ReflectionPermission (ReflectionPermissionFlag.ReflectionEmit);
			Assert ("IsUnrestricted", !p.IsUnrestricted ());
			SecurityElement se = p.ToXml ();
			AssertEquals ("ToXml-Flags=ReflectionEmit", "ReflectionEmit", (se.Attributes ["Flags"] as string));
		}
		public void Derestricted () 
		{
			ReflectionPermission p = new ReflectionPermission (PermissionState.Unrestricted);
			Assert.IsTrue (p.IsUnrestricted (), "IsUnrestricted");
			p.Flags = ReflectionPermissionFlag.NoFlags;
			Assert.IsTrue (!p.IsUnrestricted (), "IsUnrestricted");
		}
        public bool makeTheRequestQueryStringDataEditable()
        {
            try
            {
                object objectToReflect = HttpContext.Current.Request.QueryString;
                string stringObjectType = "MakeReadWrite";

                // Assert the retrival of information from a Private Method
                ReflectionPermission reflectionPerm = new ReflectionPermission(ReflectionPermissionFlag.TypeInformation);
                reflectionPerm.Assert();

                MethodInfo objTempMethodType = objectToReflect.GetType().GetMethod(
                    stringObjectType,BindingFlags.Public | BindingFlags.NonPublic |
                    BindingFlags.Instance, null,CallingConventions.Any,new Type[0] {},null);

                // Revert the previous assert since it is only possible to have one active Assert
                ReflectionPermission.RevertAssert();
                // Assert the execution of a Private Method
                reflectionPerm = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
                reflectionPerm.Assert();

                object invokeResult =
                    objTempMethodType.Invoke(
                    objectToReflect,BindingFlags.Public | BindingFlags.NonPublic |
                    BindingFlags.Instance | BindingFlags.InvokeMethod,null,new object[0] {} ,null);
                return true;
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
                return false;
            }
        }
		public void MemberAccess () 
		{
			ReflectionPermission p = new ReflectionPermission (ReflectionPermissionFlag.MemberAccess);
			Assert.IsTrue (!p.IsUnrestricted (), "IsUnrestricted");
			SecurityElement se = p.ToXml ();
			Assert.AreEqual ("MemberAccess", (se.Attributes ["Flags"] as string), "ToXml-Flags=MemberAccess");
		}
		public void TypeInformation () 
		{
			ReflectionPermission p = new ReflectionPermission (ReflectionPermissionFlag.TypeInformation);
			Assert.IsTrue (!p.IsUnrestricted (), "IsUnrestricted");
			SecurityElement se = p.ToXml ();
			Assert.AreEqual ("TypeInformation", (se.Attributes ["Flags"] as string), "ToXml-Flags=TypeInformation");
		}
Example #7
0
        //
        // IPermission implementation
        //

        public override IPermission Union(IPermission other)
        {
            if (other == null)
            {
                return(this.Copy());
            }
            else if (!VerifyType(other))
            {
                throw new
                      ArgumentException(
                          Environment.GetResourceString("Argument_WrongType", this.GetType().FullName)
                          );
            }

            ReflectionPermission operand = (ReflectionPermission)other;

            if (this.IsUnrestricted() || operand.IsUnrestricted())
            {
                return(new ReflectionPermission(PermissionState.Unrestricted));
            }
            else
            {
                ReflectionPermissionFlag flag_union = (ReflectionPermissionFlag)(m_flags | operand.m_flags);
                return(new ReflectionPermission(flag_union));
            }
        }
		public void Derestricted () 
		{
			ReflectionPermission p = new ReflectionPermission (PermissionState.Unrestricted);
			Assert ("IsUnrestricted", p.IsUnrestricted ());
			p.Flags = ReflectionPermissionFlag.NoFlags;
			Assert ("IsUnrestricted", !p.IsUnrestricted ());
		}
Example #9
0
        public override bool IsSubsetOf(IPermission target)
        {
            if (target == null)
            {
                return(m_flags == ReflectionPermissionFlag.NoFlags);
            }

            try
            {
                ReflectionPermission operand = (ReflectionPermission)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(
                          Environment.GetResourceString("Argument_WrongType", this.GetType().FullName)
                          );
            }
        }
        /// <summary>Creates and returns a permission that is the intersection of the current permission and the specified permission.</summary>
        /// <returns>A new permission 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)
        {
            ReflectionPermission reflectionPermission = this.Cast(target);

            if (reflectionPermission == null)
            {
                return(null);
            }
            if (this.IsUnrestricted())
            {
                if (reflectionPermission.Flags == ReflectionPermissionFlag.NoFlags)
                {
                    return(null);
                }
                return(reflectionPermission.Copy());
            }
            else
            {
                if (!reflectionPermission.IsUnrestricted())
                {
                    ReflectionPermission reflectionPermission2 = (ReflectionPermission)reflectionPermission.Copy();
                    reflectionPermission2.Flags &= this.flags;
                    return((reflectionPermission2.Flags != ReflectionPermissionFlag.NoFlags) ? reflectionPermission2 : null);
                }
                if (this.flags == ReflectionPermissionFlag.NoFlags)
                {
                    return(null);
                }
                return(this.Copy());
            }
        }
Example #11
0
        public override bool IsSubsetOf(IPermission target)
        {
            bool flag;

            if (target == null)
            {
                return(this.m_flags == ReflectionPermissionFlag.NoFlags);
            }
            try
            {
                ReflectionPermission permission = (ReflectionPermission)target;
                if (permission.IsUnrestricted())
                {
                    return(true);
                }
                if (this.IsUnrestricted())
                {
                    return(false);
                }
                flag = (this.m_flags & ~permission.m_flags) == ReflectionPermissionFlag.NoFlags;
            }
            catch (InvalidCastException)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", new object[] { base.GetType().FullName }));
            }
            return(flag);
        }
Example #12
0
        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)
                          );
            }

            ReflectionPermission operand = (ReflectionPermission)target;

            ReflectionPermissionFlag newFlags = operand.m_flags & this.m_flags;

            if (newFlags == ReflectionPermissionFlag.NoFlags)
            {
                return(null);
            }
            else
            {
                return(new ReflectionPermission(newFlags));
            }
        }
		public void MemberAccess () 
		{
			ReflectionPermission p = new ReflectionPermission (ReflectionPermissionFlag.MemberAccess);
			Assert ("IsUnrestricted", !p.IsUnrestricted ());
			SecurityElement se = p.ToXml ();
			AssertEquals ("ToXml-Flags=MemberAccess", "MemberAccess", (se.Attributes ["Flags"] as string));
		}
Example #14
0
		public void ReflectionEmit () 
		{
			ReflectionPermission p = new ReflectionPermission (ReflectionPermissionFlag.ReflectionEmit);
			Assert.IsTrue (!p.IsUnrestricted (), "IsUnrestricted");
			SecurityElement se = p.ToXml ();
			Assert.AreEqual ("ReflectionEmit", (se.Attributes ["Flags"] as string), "ToXml-Flags=ReflectionEmit");
		}
Example #15
0
        private static void GetPermissions()
        {
            if (!m_Initialized)
            {
                //test RelectionPermission
                CodeAccessPermission securityTest;
                try
                {
                    securityTest = new ReflectionPermission(PermissionState.Unrestricted);
                    securityTest.Demand();
                    m_ReflectionPermission = true;
                }
                catch
                {
                    //code access security error
                    m_ReflectionPermission = false;
                }
				
                //test WebPermission
                try
                {
                    securityTest = new WebPermission(PermissionState.Unrestricted);
                    securityTest.Demand();
                    m_WebPermission = true;
                }
                catch
                {
                    //code access security error
                    m_WebPermission = false;
                }
				
                //test WebHosting Permission (Full Trust)
                try
                {
                    securityTest = new AspNetHostingPermission(AspNetHostingPermissionLevel.Unrestricted);
                    securityTest.Demand();
                    m_AspNetHostingPermission = true;
                }
                catch
                {
                    //code access security error
                    m_AspNetHostingPermission = false;
                }
                m_Initialized = true;

                //Test for Unmanaged Code permission
                try
                {
                    securityTest = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
                    securityTest.Demand();
                    m_UnManagedCodePermission = true;
                }
                catch (Exception e)
                {
                    m_UnManagedCodePermission = false;
                }
            }
        }
        public PrivateObjectTester (object instance)
        {
            perm = new ReflectionPermission(PermissionState.Unrestricted);
            perm.Demand();

            type = Type.GetTypeFromHandle(Type.GetTypeHandle(instance));

            this.instance = instance;
        } 
		public void PermissionStateUnrestricted () 
		{
			ReflectionPermission p = new ReflectionPermission (PermissionState.Unrestricted);
			AssertNotNull ("ReflectionPermission(PermissionState.Unrestricted)", p);
			Assert ("IsUnrestricted", p.IsUnrestricted ());
			ReflectionPermission copy = (ReflectionPermission) p.Copy ();
			AssertEquals ("Copy.IsUnrestricted", p.IsUnrestricted (), copy.IsUnrestricted ());
			SecurityElement se = p.ToXml ();
			AssertEquals ("ToXml-Unrestricted", "true", (se.Attributes ["Unrestricted"] as string));
		}
Example #18
0
		public void PermissionStateUnrestricted () 
		{
			ReflectionPermission p = new ReflectionPermission (PermissionState.Unrestricted);
			Assert.IsNotNull (p, "ReflectionPermission(PermissionState.Unrestricted)");
			Assert.IsTrue (p.IsUnrestricted (), "IsUnrestricted");
			ReflectionPermission copy = (ReflectionPermission) p.Copy ();
			Assert.AreEqual (p.IsUnrestricted (), copy.IsUnrestricted (), "Copy.IsUnrestricted");
			SecurityElement se = p.ToXml ();
			Assert.AreEqual ("true", (se.Attributes ["Unrestricted"] as string), "ToXml-Unrestricted");
		}
Example #19
0
		public void PermissionStateNone ()
		{
			ReflectionPermission p = new ReflectionPermission (PermissionState.None);
			Assert.IsNotNull (p, "ReflectionPermission(PermissionState.None)");
			Assert.IsTrue (!p.IsUnrestricted (), "IsUnrestricted");
			ReflectionPermission copy = (ReflectionPermission) p.Copy ();
			Assert.AreEqual (p.IsUnrestricted (), copy.IsUnrestricted (), "Copy.IsUnrestricted");
			SecurityElement se = p.ToXml ();
			Assert.IsTrue ((se.Attributes ["class"] as string).StartsWith (className), "ToXml-class");
			Assert.AreEqual ("1", (se.Attributes ["version"] as string), "ToXml-version");
		}
		public void PermissionStateNone () 
		{
			ReflectionPermission p = new ReflectionPermission (PermissionState.None);
			AssertNotNull ("ReflectionPermission(PermissionState.None)", p);
			Assert ("IsUnrestricted", !p.IsUnrestricted ());
			ReflectionPermission copy = (ReflectionPermission) p.Copy ();
			AssertEquals ("Copy.IsUnrestricted", p.IsUnrestricted (), copy.IsUnrestricted ());
			SecurityElement se = p.ToXml ();
			Assert ("ToXml-class", (se.Attributes ["class"] as string).StartsWith (className));
			AssertEquals ("ToXml-version", "1", (se.Attributes ["version"] as string));
		}
        private Type type; // type of class to manage

        #endregion Fields

        #region Constructors

        public PrivateObjectReflector(string qualifiedTypeName, params object[] args)
        {
            perm = new ReflectionPermission(PermissionState.Unrestricted);
            perm.Demand();
            type = Type.GetType(qualifiedTypeName);
            Type[] types = new Type[args.Length];
            for (int i = 0; i < args.Length; i++)
            {
                types[i] = args[i].GetType();
            }
            instance = type.GetConstructor(bindingFlags, null, types, null).Invoke(args);
        }
        public void CannotInvokeNonPublicMethodWhenCallsMadeDirectly()
        {
            ReflectionPermission permission = new ReflectionPermission(ReflectionPermissionFlag.TypeInformation);
            permission.Deny();

            XmlIncludeTypeDataCollection xmlIncludeTypes = new XmlIncludeTypeDataCollection();
            xmlIncludeTypes.Add(new XmlIncludeTypeData("shouldNotBeAccessible", typeof(TestingInternal.ShouldNotBeAccessible).AssemblyQualifiedName));

            RuntimeConfigurationView view = new RuntimeConfigurationView(new ConfigurationContext(new ConfigurationDictionary()));
            Type [] collectedTypes = view.CollectTypes(xmlIncludeTypes);

            Assert.AreEqual(1, collectedTypes.Length);
        }
        /// <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)
        {
            ReflectionPermission reflectionPermission = this.Cast(target);

            if (reflectionPermission == null)
            {
                return(this.flags == ReflectionPermissionFlag.NoFlags);
            }
            if (this.IsUnrestricted())
            {
                return(reflectionPermission.IsUnrestricted());
            }
            return(reflectionPermission.IsUnrestricted() || (this.flags & reflectionPermission.Flags) == this.flags);
        }
        private ReflectionPermission Cast(IPermission target)
        {
            if (target == null)
            {
                return(null);
            }
            ReflectionPermission reflectionPermission = target as ReflectionPermission;

            if (reflectionPermission == null)
            {
                CodeAccessPermission.ThrowInvalidPermission(target, typeof(ReflectionPermission));
            }
            return(reflectionPermission);
        }
Example #25
0
        // Methods
        public override IPermission CreatePermission()
        {
            ReflectionPermission perm = null;

            if (this.Unrestricted)
            {
                perm = new ReflectionPermission(PermissionState.Unrestricted);
            }
            else
            {
                perm = new ReflectionPermission(flags);
            }
            return(perm);
        }
        public void CannotInvokeNonPublicMethodWhenCallsMadeThroughReflection()
        {
            ReflectionPermission permission = new ReflectionPermission(ReflectionPermissionFlag.TypeInformation);
            permission.Deny();

            XmlIncludeTypeDataCollection xmlIncludeTypes = new XmlIncludeTypeDataCollection();
            xmlIncludeTypes.Add(new XmlIncludeTypeData("shouldNotBeAccessible", typeof(TestingInternal.ShouldNotBeAccessible).AssemblyQualifiedName));

            RuntimeConfigurationView view = new RuntimeConfigurationView(new ConfigurationContext(new ConfigurationDictionary()));
            MethodInfo collectTypes = view.GetType().GetMethod("CollectTypes", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { typeof(XmlIncludeTypeDataCollection)}, null);
            object returnedTypes = collectTypes.Invoke(view, new object[] { xmlIncludeTypes });
            Type [] collectedTypes = returnedTypes as Type[];

            Assert.AreEqual(1, collectedTypes.Length);
        }
Example #27
0
        // helpers

        private ReflectionPermission Cast(IPermission target)
        {
            if (target == null)
            {
                return(null);
            }

            ReflectionPermission rp = (target as ReflectionPermission);

            if (rp == null)
            {
                ThrowInvalidPermission(target, typeof(ReflectionPermission));
            }

            return(rp);
        }
        public IPermission Parse(JToken token)
        {
            var items = token as JArray;

            if (items == null)
            {
                return null;
            }

            var result = new ReflectionPermission(PermissionState.None);
            var flags = items.ToObject<string[]>();

            if (flags.Contains("memberAccess")) result.Flags |= ReflectionPermissionFlag.MemberAccess;
            if (flags.Contains("restrictedMemberAccess")) result.Flags |= ReflectionPermissionFlag.RestrictedMemberAccess;

            return result;
        }
        /// <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="other">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="other" /> parameter is not null and is not of the same type as the current permission. </exception>
        public override IPermission Union(IPermission other)
        {
            ReflectionPermission reflectionPermission = this.Cast(other);

            if (other == null)
            {
                return(this.Copy());
            }
            if (this.IsUnrestricted() || reflectionPermission.IsUnrestricted())
            {
                return(new ReflectionPermission(PermissionState.Unrestricted));
            }
            ReflectionPermission reflectionPermission2 = (ReflectionPermission)reflectionPermission.Copy();

            reflectionPermission2.Flags |= this.flags;
            return(reflectionPermission2);
        }
        internal static Expression InjectReflectionPermissionIfNecessary(MethodInfo method, Expression expression)
        {
            if (method == null)
            {
                return expression;
            }

            if (method.IsPublic)
            {
                return expression;
            }
            else
            {
                ReflectionPermission reflectionMemberAccessPermission = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
                Expression demandExpression = Expression.Call(Expression.Constant(reflectionMemberAccessPermission), "Demand", null, null);
                return Expression.Block(expression.Type, demandExpression, expression);
            }
        }
Example #31
0
        public override IPermission Union(IPermission other)
        {
            if (other == null)
            {
                return(this.Copy());
            }
            if (!base.VerifyType(other))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", new object[] { base.GetType().FullName }));
            }
            ReflectionPermission permission = (ReflectionPermission)other;

            if (this.IsUnrestricted() || permission.IsUnrestricted())
            {
                return(new ReflectionPermission(PermissionState.Unrestricted));
            }
            return(new ReflectionPermission(this.m_flags | permission.m_flags));
        }
Example #32
0
 public static PermissionToken FindToken( Type cls )
 {
     if (cls == null)
         return null;
      
     if (cls.GetInterface( "System.Security.Permissions.IBuiltInPermission" ) != null)
     {
                     if (s_reflectPerm == null)
                         s_reflectPerm = new ReflectionPermission(PermissionState.Unrestricted);
                     s_reflectPerm.Assert();
         MethodInfo method = cls.GetMethod( "GetTokenIndex", BindingFlags.Static | BindingFlags.NonPublic );
         BCLDebug.Assert( method != null, "IBuiltInPermission types should have a static method called 'GetTokenIndex'" );
         return s_theTokenFactory.BuiltInGetToken( (int)method.Invoke( null, null ), null, cls );
     }             
     else
     {
         return s_theTokenFactory.FindToken( cls );
     }
 }
Example #33
0
        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 }));
            }
            ReflectionPermission     permission = (ReflectionPermission)target;
            ReflectionPermissionFlag flag       = permission.m_flags & this.m_flags;

            if (flag == ReflectionPermissionFlag.NoFlags)
            {
                return(null);
            }
            return(new ReflectionPermission(flag));
        }
 public static PermissionToken FindToken(Type cls)
 {
     if (cls == null)
     {
         return null;
     }
     if (cls.GetInterface("System.Security.Permissions.IBuiltInPermission") == null)
     {
         return s_theTokenFactory.FindToken(cls);
     }
     if (s_reflectPerm == null)
     {
         s_reflectPerm = new ReflectionPermission(PermissionState.Unrestricted);
     }
     s_reflectPerm.Assert();
     RuntimeMethodInfo method = cls.GetMethod("GetTokenIndex", BindingFlags.NonPublic | BindingFlags.Static) as RuntimeMethodInfo;
     int index = (int) method.Invoke(null, BindingFlags.Default, null, null, null, true);
     return s_theTokenFactory.BuiltInGetToken(index, null, cls);
 }
Example #35
0
        public override IPermission Union(IPermission other)
        {
            ReflectionPermission rp = Cast(other);

            if (other == null)
            {
                return(Copy());
            }

            if (IsUnrestricted() || rp.IsUnrestricted())
            {
                return(new ReflectionPermission(PermissionState.Unrestricted));
            }

            ReflectionPermission p = (ReflectionPermission)rp.Copy();

            p.Flags |= flags;
            return(p);
        }
Example #36
0
        public override bool IsSubsetOf(IPermission target)
        {
            ReflectionPermission rp = Cast(target);

            if (rp == null)
            {
                return(flags == ReflectionPermissionFlag.NoFlags);
            }

            if (IsUnrestricted())
            {
                return(rp.IsUnrestricted());
            }
            else if (rp.IsUnrestricted())
            {
                return(true);
            }

            return((flags & rp.Flags) == flags);
        }
Example #37
0
 private static void GetPermissions()
 {
     if (!m_Initialized)
     {
         //  test RelectionPermission
         System.Security.CodeAccessPermission securityTest;
         try
         {
             securityTest = new ReflectionPermission(PermissionState.Unrestricted);
             securityTest.Demand();
             m_ReflectionPermission = true;
         }
         catch
         {
             m_ReflectionPermission = false;
         }
         //  test WebPermission
         try
         {
             securityTest = new System.Net.WebPermission(PermissionState.Unrestricted);
             securityTest.Demand();
             m_WebPermission = true;
         }
         catch
         {
            m_WebPermission = false;
         }
         //  test WebHosting Permission (Full Trust)
         try
         {
             securityTest = new AspNetHostingPermission(AspNetHostingPermissionLevel.Unrestricted);
             securityTest.Demand();
             m_AspNetHostingPermission = true;
         }
         catch
         {
             m_AspNetHostingPermission = false;
         }
         m_Initialized = true;
     }
 }
Example #38
0
        public override IPermission Intersect(IPermission target)
        {
            ReflectionPermission rp = Cast(target);

            if (rp == null)
            {
                return(null);
            }

            if (IsUnrestricted())
            {
                if (rp.Flags == ReflectionPermissionFlag.NoFlags)
                {
                    return(null);
                }
                else
                {
                    return(rp.Copy());
                }
            }
            if (rp.IsUnrestricted())
            {
                if (flags == ReflectionPermissionFlag.NoFlags)
                {
                    return(null);
                }
                else
                {
                    return(Copy());
                }
            }

            ReflectionPermission p = (ReflectionPermission)rp.Copy();

            p.Flags &= flags;
            return((p.Flags == ReflectionPermissionFlag.NoFlags) ? null : p);
        }
 /// <summary>确定当前权限是否为指定权限的子集。</summary>
 /// <returns>如果当前权限是指定权限的子集,则为 true;否则为 false。</returns>
 /// <param name="target">将要测试子集关系的权限。此权限必须与当前权限属于同一类型。</param>
 /// <exception cref="T:System.ArgumentException">
 /// <paramref name="target" /> 参数不是 null,而且与当前权限不是同一类型。</exception>
 public override bool IsSubsetOf(IPermission target)
 {
     if (target == null)
     {
         return(this.m_flags == ReflectionPermissionFlag.NoFlags);
     }
     try
     {
         ReflectionPermission reflectionPermission = (ReflectionPermission)target;
         if (reflectionPermission.IsUnrestricted())
         {
             return(true);
         }
         if (this.IsUnrestricted())
         {
             return(false);
         }
         return((this.m_flags & ~reflectionPermission.m_flags) == ReflectionPermissionFlag.NoFlags);
     }
     catch (InvalidCastException ex)
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", (object)this.GetType().FullName));
     }
 }
        static XPathDocumentWriter()
        {
            ReflectionPermission perm = new ReflectionPermission(PermissionState.Unrestricted);
            perm.Flags = ReflectionPermissionFlag.MemberAccess;

            try
            {
                perm.Assert();

                Type t = typeof(XPathDocument);
                defaultConstructor = t.GetConstructor(
                    BindingFlags.NonPublic | BindingFlags.Instance, null,
                    Type.EmptyTypes,
                    new ParameterModifier[0]);
                Debug.Assert(defaultConstructor != null, ".NET Framework implementation changed");

                loadWriterMethod = t.GetMethod("LoadFromWriter", BindingFlags.Instance | BindingFlags.NonPublic);
                Debug.Assert(loadWriterMethod != null, ".NET Framework implementation changed");
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
        }
Example #41
0
 /// <internalonly/>
 int IBuiltInPermission.GetTokenIndex()
 {
     return(ReflectionPermission.GetTokenIndex());
 }
		// Methods
		public override IPermission CreatePermission ()
		{
#if NET_2_1
			return null;
#else
			ReflectionPermission perm = null;
			if (this.Unrestricted)
				perm = new ReflectionPermission (PermissionState.Unrestricted);
			else
				perm = new ReflectionPermission (flags);
			return perm;
#endif
		}
Example #43
0
        public static PermissionToken FindToken( Type cls )
        {
            if (cls == null)
                return null;
             
#if FEATURE_CAS_POLICY
            if (cls.GetInterface( "System.Security.Permissions.IBuiltInPermission" ) != null)
            {
                if (s_reflectPerm == null)
                    s_reflectPerm = new ReflectionPermission(PermissionState.Unrestricted);
                s_reflectPerm.Assert();
                MethodInfo method = cls.GetMethod( "GetTokenIndex", BindingFlags.Static | BindingFlags.NonPublic );
                Contract.Assert( method != null, "IBuiltInPermission types should have a static method called 'GetTokenIndex'" );

                // GetTokenIndex needs to be invoked without any security checks, since doing a security check
                // will involve a ReflectionTargetDemand which creates a CompressedStack and attempts to get the
                // token.
                RuntimeMethodInfo getTokenIndex = method as RuntimeMethodInfo;
                Contract.Assert(getTokenIndex != null, "method is not a RuntimeMethodInfo");
                int token = (int)getTokenIndex.UnsafeInvoke(null, BindingFlags.Default, null, null, null);
                return s_theTokenFactory.BuiltInGetToken(token, null, cls);
            }
            else
#endif // FEATURE_CAS_POLICY
            {
                return s_theTokenFactory.FindToken( cls );
            }
        }
Example #44
0
        public static object InvokePropertyOrMethod(string className,
                                                    string methodName, object[] parameters, InvokeType invokeType)
        {
            System.Security.Permissions.ReflectionPermission perm =
                new System.Security.Permissions.ReflectionPermission(
                    System.Security.Permissions.PermissionState.Unrestricted);
            perm.Assert();

            Type type;
            bool isGetProperty;

            object[]     newargs;
            object       instance;
            BindingFlags defaultFlags;
            object       resultObject;

            //if (ActionManager.IsInvokeTypeStatic(invokeType))
            //{
            //    if (String.IsNullOrEmpty(className))
            //    {
            //        throw new ArgumentException(
            //            "className cannot be null or empty for static invocations",
            //            "className");
            //    }
            //    type = ReflectionUtils.FindType(className);
            //    if (type == null)
            //    {
            //       throw new InvalidOperationException("FindType fails");
            //    }
            //}
            //else
            //{
            if (parameters == null || parameters.Length == 0)
            {
                throw new ArgumentException(
                          "parameters cannot be null or empty for instance invocations",
                          "parameters");
            }
            if (parameters[0] == null)
            {
                throw new ArgumentException(
                          "parameters cannot have a null first element for instance invocations",
                          "parameters");
            }
            type = parameters[0].GetType();
            //}
            System.Diagnostics.Debug.Assert(type != null);

            defaultFlags = BindingFlags.ExactBinding | BindingFlags.Public;
            newargs      = null;
            instance     = null;

            switch (invokeType)
            {
            case InvokeType.StaticMethod:
                defaultFlags |= BindingFlags.InvokeMethod;
                defaultFlags |= BindingFlags.Static;
                break;

            case InvokeType.InstanceMethod:
                defaultFlags |= BindingFlags.InvokeMethod;
                defaultFlags |= BindingFlags.Instance;
                instance      = ExtractFirstParameterAsInstance(parameters, ref newargs);
                parameters    = newargs;
                break;

            case InvokeType.GetStaticProperty:
                defaultFlags |= BindingFlags.GetProperty;
                defaultFlags |= BindingFlags.Static;
                break;

            case InvokeType.SetStaticProperty:
                defaultFlags |= BindingFlags.SetProperty;
                defaultFlags |= BindingFlags.Static;
                break;

            case InvokeType.GetInstanceProperty:
                defaultFlags |= BindingFlags.GetProperty;
                defaultFlags |= BindingFlags.Instance;
                instance      = ExtractFirstParameterAsInstance(parameters, ref newargs);
                parameters    = newargs;
                break;

            case InvokeType.SetInstanceProperty:
                defaultFlags |= BindingFlags.SetProperty;
                defaultFlags |= BindingFlags.Instance;
                instance      = ExtractFirstParameterAsInstance(parameters, ref newargs);
                parameters    = newargs;
                break;

            case InvokeType.GetStaticField:
                defaultFlags |= BindingFlags.GetField;
                defaultFlags |= BindingFlags.Static;
                break;

            case InvokeType.SetStaticField:
                defaultFlags |= BindingFlags.SetField;
                defaultFlags |= BindingFlags.Static;
                break;

            case InvokeType.GetInstanceField:
                defaultFlags |= BindingFlags.GetField;
                defaultFlags |= BindingFlags.Instance;
                instance      = ExtractFirstParameterAsInstance(parameters, ref newargs);
                parameters    = newargs;
                break;

            case InvokeType.SetInstanceField:
                defaultFlags |= BindingFlags.SetField;
                defaultFlags |= BindingFlags.Instance;
                instance      = ExtractFirstParameterAsInstance(parameters, ref newargs);
                parameters    = newargs;
                break;
            }

            try
            {
                resultObject = type.InvokeMember(
                    methodName, defaultFlags, null, instance, parameters);
            }
            catch (MissingMethodException e)
            {
                isGetProperty = invokeType == InvokeType.GetInstanceProperty ||
                                invokeType == InvokeType.GetStaticProperty;
                if (parameters.Length > 0 && isGetProperty)
                {
                }
                throw;
            }
            catch (AmbiguousMatchException e)
            {
                throw;
            }
            return(resultObject);
        }
Example #45
0
		public void AllFlags () 
		{
			ReflectionPermission p = new ReflectionPermission (ReflectionPermissionFlag.AllFlags);
			Assert.IsTrue (p.IsUnrestricted (), "IsUnrestricted");
			SecurityElement se = p.ToXml ();
			Assert.AreEqual ("true", (se.Attributes ["Unrestricted"] as string), "ToXml-Unrestricted");
		}
		private static string GetReflectionPermission (ReflectionPermission rp)
		{
			return String.Format ("  ReflectionPermission\\l    {0}\\l",
				(rp as ReflectionPermission).Flags);
		}
Example #47
0
        internal void SetAttributes(IDataReader reader)
        {
            // allow access to private fields
            ReflectionPermission perm = new ReflectionPermission(ReflectionPermissionFlag.NoFlags);
            perm.Demand();
            perm.Assert();

            try
            {
                IDictionary TimeSheetFieldMappings = GetDataLink().FieldMappings;

                _log.Debug("SetAttributes: starting for loop (" + this.GetType().FullName + ")");
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    try
                    {
                        // Get the field mapping entry corresponding to the database column name
                        TimeSheetFieldMap TimeSheetFieldMap = null;
                        foreach (TimeSheetFieldMap TimeSheetFieldMap2 in TimeSheetFieldMappings.Values)
                        {
                            if (TimeSheetFieldMap2.FieldName == reader.GetName(i))
                            {
                                TimeSheetFieldMap = TimeSheetFieldMap2;
                                break;
                            }
                        }

                        if (TimeSheetFieldMap == null)
                        {
                            //throw new ApplicationException(string.Format("Could not locate {0} in field mappings.", reader.GetName(i)));
                        }

                        // Read the object from the database.  This may be a plain value
                        // Enum stored as an integer in the database
                        object value;
                        if (!reader.IsDBNull(i))
                        {
                            if (TimeSheetFieldMap.IsEnum)
                            {
                                // the database field holds the int value for an enum
                                _log.Debug("SetAttributes: Setting enum value for " + TimeSheetFieldMap.AttributeName);
                                object enumValue = GetObjectFromDatabase(reader, i);
                                value = Enum.ToObject(TimeSheetFieldMap.EnumType, enumValue);
                            }
                            else
                            {
                                // the database field holds the attribute value
                                _log.Debug("SetAttributes: Setting direct value for " + TimeSheetFieldMap.AttributeName);
                                value = GetObjectFromDatabase(reader, i);
                            }
                        }
                        else
                        {
                            value = null;
                        }

                        _log.Debug("SetAttributes: now setting field " + TimeSheetFieldMap.FieldInfo + " to " + value);
                        // set the instance's field to the value
                        try
                        {
                            TimeSheetFieldMap.FieldInfo.SetValue(this, value);
                        }
                        catch
                        {
                            _log.Debug("Exception caught in FieldInfo.SetValue setting field {0} to {1}.", TimeSheetFieldMap.FieldInfo, value);
                            //throw;
                        }
                        _log.Debug("SetAttributes: Setting " + TimeSheetFieldMap.FieldName + " to " + value);
                    }
                    catch (Exception e)
                    {
                        _log.Debug("Exception caught reading {0}, column {1}, column name '{2}'.", e, this.GetType().Name, i, reader.GetName(i));
                        //throw;
                    }
                }
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            _log.Debug("SetAttributes: done (" + this.GetType().FullName + ")");
        }
Example #48
0
        public ArrayList GetIntfObjects(Type intfType)
        {
            ArrayList aRet = new ArrayList();

            ReflectionPermission reflectionPerm1 = new ReflectionPermission(PermissionState.None);
            reflectionPerm1.Flags = ReflectionPermissionFlag.AllFlags;

            if (intfType.IsInterface)
            {
                Type type = this.GetType();
                FieldInfo[] myFields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
                for (int i = 0; i < myFields.Length; i++)
                {
                    object newobj = myFields[i].GetValue(this);
                    if ((null != newobj) && (null != newobj.GetType().GetInterface(intfType.Name)))
                    {
                        aRet.Add(newobj);
                    }
                }
            }
            return aRet;
        }
Example #49
0
		public void IsSubsetOfBadPermission () 
		{
			ReflectionPermission p1 = new ReflectionPermission (ReflectionPermissionFlag.NoFlags);
			EnvironmentPermission ep2 = new EnvironmentPermission (PermissionState.Unrestricted);
			Assert.IsTrue (p1.IsSubsetOf (ep2), "IsSubsetOf(EnvironmentPermission)");
		}