Exemplo n.º 1
0
        [System.Security.SecurityCritical]  // auto-generated
        public static PermissionToken GetToken(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.GetToken(cls, null));
            }
        }
        public bool makeTheRequestFormDataEditable()
        {
            try
            {
                object objectToReflect  = HttpContext.Current.Request.Form;
                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);
            }
        }
Exemplo n.º 3
0
        static XamlSchemaContext GetXamlSchemaContext(Assembly assembly, string helperClassName)
        {
            XamlSchemaContext typeSchemaContext = null;
            ReflectionPermission reflectionPerm = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
            reflectionPerm.Assert();
            try
            {
                Type schemaContextType = assembly.GetType(helperClassName);
                if (schemaContextType == null)
                {
                    throw FxTrace.Exception.AsError(new InvalidOperationException(SR.SchemaContextFromBeforeInitializeComponentXBTExtensionNotFound(helperClassName)));
                }

                // The "official" BeforeInitializeComponent XBT Extension will not create a generic type for this helper class.
                // This check is here so that the assembly manifest can't lure us into creating a type with a generic argument from a different assembly.
                if (schemaContextType.IsGenericType || schemaContextType.IsGenericTypeDefinition)
                {
                    throw FxTrace.Exception.AsError(new InvalidOperationException(SR.SchemaContextFromBeforeInitializeComponentXBTExtensionCannotBeGeneric(helperClassName)));
                }

                PropertyInfo schemaContextPropertyInfo = schemaContextType.GetProperty("SchemaContext",
                    BindingFlags.NonPublic | BindingFlags.Static);
                typeSchemaContext = (XamlSchemaContext)schemaContextPropertyInfo.GetValue(null,
                    BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetProperty, null, null, null);
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            return typeSchemaContext;
        }
        static XPathDocumentWriter()
        {
            var perm = new ReflectionPermission(PermissionState.Unrestricted)
            {
                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
            {
                System.Security.CodeAccessPermission.RevertAssert();
            }
        }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
        public ObjectTextDumper Dump(
            object value,
            Type dumpMetadata           = null,
            DumpAttribute dumpAttribute = null)
        {
            var originalIndentLevel  = _indentLevel;
            var reflectionPermission = new ReflectionPermission(PermissionState.Unrestricted);
            var revertPermission     = false;

            try
            {
                // assert the permission and dump
                reflectionPermission.Demand();
                revertPermission = true;

                // assert the permission and dump
                reflectionPermission.Assert();
                _maxDepth = int.MinValue;

                DumpObject(value, dumpMetadata, dumpAttribute);
            }
            catch (SecurityException)
            {
                Writer.WriteLine();
                Writer.Write(Resources.CallerDoesNotHavePermissionFormat, value?.ToString() ?? DumpUtilities.Null);
            }
            catch (Exception x)
            {
                var message = $"\n\nATTENTION:\nThe TextDumper threw an exception:\n{x.ToString()}";

                Writer.WriteLine(message);
                Debug.WriteLine(message);
                _indentLevel = originalIndentLevel;
            }
            finally
            {
                // revert the permission assert
                if (revertPermission)
                {
                    CodeAccessPermission.RevertAssert();
                }

                // clear the dumped objects register
                DumpedObjects.Clear();
                // prepare our writer for a new dump
                if (_isDumpWriter)
                {
                    ((DumpTextWriter)Writer).Reset();
                }
            }

            return(this);
        }
Exemplo n.º 7
0
        public static PermissionToken GetToken(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.GetToken(cls, null));
            }
        }
Exemplo n.º 8
0
        public void RandomAccess()
        {
            try
            {
                ReflectionPermission LReflectionPermission = new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess | ReflectionPermissionFlag.AllFlags);
                LReflectionPermission.Assert();

                new RandomFixedSizeCacheAccess(new FixedSizeCache <string, string>(50), 50, 100000, 100).Run();
                Assert.True(ValidateList());
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
        }
        static XPathDocumentWriter()
        {
            var perm = new ReflectionPermission(PermissionState.Unrestricted)
            {
                Flags = ReflectionPermissionFlag.MemberAccess
            };

            try
            {
                perm.Assert();
                var t = typeof(XPathDocument);
                DefaultConstructor = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, new ParameterModifier[0]);
                LoadWriterMethod   = t.GetMethod("LoadFromWriter", BindingFlags.NonPublic | BindingFlags.Instance);
                CodeAccessPermission.RevertAssert();
            }
            catch
            {
                CodeAccessPermission.RevertAssert();
                throw;
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// Copy all of the internal fields.
 /// </summary>
 public void CopyFields(ref object Instance, ref Array Fields, ref object Source)
 {
     try
     {
         //** When used with .Net Remoting hosted by IIS, even with Full trust,
         //** f.SetValue fails with a permission exception.
         //** This asset grants the required permissions so that the f.SetValue works.
         ReflectionPermission rp = new ReflectionPermission(PermissionState.Unrestricted);
         rp.Assert();
         foreach (PropertyInfo f in Fields)
         {
             if (f.CanWrite)
             {
                 f.SetValue(Instance, f.GetValue(Source));
             }
         }
     }
     finally
     {
         ReflectionPermission.RevertAssert();
     }
 }
Exemplo n.º 11
0
        public void InternalRemove()
        {
            try
            {
                //add four remove middle
                //add four remove tail

                ReflectionPermission LReflectionPermission = new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess | ReflectionPermissionFlag.AllFlags);
                LReflectionPermission.Assert();

                FixedSizeCache <string, string> .Entry LHead;
                FixedSizeCache <string, string> .Entry LCutoff;
                FixedSizeCache <string, string> .Entry LTail;

                //add one remove one
                FTestCache = new FixedSizeCache <string, string>(2);
                IncrementTestEntry();

                FTestCache.Remove(GetKey(1));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(0, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.IsNull(LHead);
                Assert.IsNull(LCutoff);
                Assert.IsNull(LTail);

                //add two remove head
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(2);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                string LKey = GetKey(2);

                FTestCache.Remove(LKey);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(1));
                Assert.AreEqual(LCutoff.Key, GetKey(1));
                Assert.AreEqual(LTail.Key, GetKey(1));

                Assert.IsNull(GetNext(LHead));
                Assert.IsNull(GetPrior(LHead));
                Assert.False(GetPreCutoff(LHead));

                //add two remove tail
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(2);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                LKey = GetKey(1);

                FTestCache.Remove(LKey);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(2));
                Assert.AreEqual(LCutoff.Key, GetKey(2));
                Assert.AreEqual(LTail.Key, GetKey(2));

                //add three remove head, head
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(3);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                LKey = GetKey(3);

                FTestCache.Remove(LKey);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(2, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(2));
                Assert.AreEqual(LCutoff.Key, GetKey(2));
                Assert.IsNull(GetNext(LHead));
                Assert.AreEqual(LTail, GetPrior(LHead));
                Assert.False(GetPreCutoff(LHead));

                Assert.AreEqual(LTail.Key, GetKey(1));
                Assert.AreEqual(LHead, GetNext(LTail));
                Assert.IsNull(GetPrior(LTail));
                Assert.False(GetPreCutoff(LTail));

                FTestCache.Remove(GetKey(2));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(1));
                Assert.AreEqual(LCutoff.Key, GetKey(1));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //add three remove head, tail
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(3);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                LKey = GetKey(3);

                FTestCache.Remove(LKey);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(2, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(2));
                Assert.AreEqual(LCutoff.Key, GetKey(2));
                Assert.AreEqual(LTail.Key, GetKey(1));

                FTestCache.Remove(GetKey(1));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(2));
                Assert.AreEqual(LCutoff.Key, GetKey(2));
                Assert.AreEqual(LTail.Key, GetKey(2));

                //add three remove tail, middle
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(3);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                LKey = GetKey(1);

                FTestCache.Remove(LKey);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(2, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(3));
                Assert.AreEqual(LCutoff.Key, GetKey(3));
                Assert.IsNull(GetNext(LHead));
                Assert.AreEqual(LTail, GetPrior(LHead));
                Assert.False(GetPreCutoff(LHead));

                Assert.AreEqual(LTail.Key, GetKey(2));
                Assert.AreEqual(LHead, GetNext(LTail));
                Assert.IsNull(GetPrior(LTail));
                Assert.False(GetPreCutoff(LTail));

                FTestCache.Remove(GetKey(2));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(3));
                Assert.AreEqual(LCutoff.Key, GetKey(3));
                Assert.AreEqual(LTail.Key, GetKey(3));

                //add three remove tail, head
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(3);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                LKey = GetKey(1);

                FTestCache.Remove(LKey);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(2, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(3));
                Assert.AreEqual(LCutoff.Key, GetKey(3));
                Assert.AreEqual(LTail.Key, GetKey(2));

                FTestCache.Remove(GetKey(3));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(2));
                Assert.AreEqual(LCutoff.Key, GetKey(2));
                Assert.AreEqual(LTail.Key, GetKey(2));

                //add four remove head
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(4);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetHead().Key);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(3, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());


                Assert.AreEqual(LHead.Key, GetKey(3));
                Assert.AreEqual(LCutoff.Key, GetKey(3));
                Assert.IsNull(GetNext(LHead));
                //Assert.AreEqual(LTail, GetPrior(LHead));
                //Assert.False(GetPreCutoff(LHead));

                //Assert.AreEqual(LTail.Key, GetKey(2));
                //Assert.AreEqual(LHead, GetNext(LTail));
                //Assert.IsNull(GetPrior(LTail));
                //Assert.False(GetPreCutoff(LTail));


                //add four remove tail
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(4);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetTail().Key);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(3, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(4));
                Assert.AreEqual(LTail.Key, GetKey(2));

                //add four remove cutoff
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(4);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetCutoff().Key);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(3, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(4));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //add four remove middle
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(4);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetKey(2));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(3, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(4));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //add five remove head
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(5);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetHead().Key);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(4, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(5));
                Assert.AreEqual(LCutoff.Key, GetKey(3));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //add five remove tail
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(5);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetTail().Key);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(4, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(5));
                Assert.AreEqual(LTail.Key, GetKey(2));

                //add five remove cutoff
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(5);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetCutoff().Key);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(4, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(3));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //add five remove middle, next to cutoff
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(5);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetKey(3));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(4, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(5));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //add five remove middle, next to tail
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(5);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }

                FTestCache.Remove(GetKey(3));

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();

                Assert.AreEqual(4, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead.Key, GetKey(4));
                Assert.AreEqual(LCutoff.Key, GetKey(5));
                Assert.AreEqual(LTail.Key, GetKey(1));

                //FTestCache = new FixedSizeCache<string, string>(2);
                //while (TestEntry < FTestCache.Size)
                //    IncrementTestEntry();


                //FTestCache = new FixedSizeCache<string, string>(25);
                //while (TestEntry < FTestCache.Size)
                //    IncrementTestEntry();

                //FTestCache.Remove(GetHead().Key);
                //Assert.True(ValidateList());

                //FTestCache.Remove(GetCutoff().Key);
                //Assert.True(ValidateList());

                //FTestCache.Remove(GetTail().Key);
                //Assert.True(ValidateList());

                //while (FTestCache.Count > 0)
                //{
                //    FTestCache.Remove(GetCutoff().Key);
                //    Assert.True(ValidateList());
                //}
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
        }
Exemplo n.º 12
0
        public void InternalReference()
        {
            try
            {
                ReflectionPermission LReflectionPermission = new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess | ReflectionPermissionFlag.AllFlags);
                LReflectionPermission.Assert();

                // Entries don't get promoted until Correlated Period is exceeded and when they do they are promoted to Head
                FTestCache = new FixedSizeCache <string, string>(25);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                }
                FixedSizeCache <string, string> .Entry LEntry = GetPrior(GetHead());
                Assert.AreNotEqual(GetHead(), LEntry);
                int LLocation = GetLocation(LEntry);
                for (int i = (FTestCache.Size - GetLastAccess(LEntry)); i < FixedSizeCache <string, string> .DefaultCorrelatedReferencePeriod; i++)
                {
                    FTestCache.Reference(LEntry.Key, LEntry.Value);
                    Assert.AreEqual(LLocation, GetLocation(LEntry));
                }

                FTestCache.Reference(LEntry.Key, LEntry.Value);
                Assert.AreNotEqual(LLocation, GetLocation(LEntry));
                Assert.AreEqual(GetHead(), LEntry);
                Assert.True(ValidateList());

                // Entries PreCutoff get demoted to not PreCutoff
                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(25);
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                    Assert.True(ValidateCutoff());
                }

                LEntry = GetHead();
                FixedSizeCache <string, string> .Entry LReferenced;
                FixedSizeCache <string, string> .Entry LTail;

                Assert.True(GetPreCutoff(LEntry));
                int LPreCutoffCount = PreCutoffCount();
                int LMoveCount      = 0;
                while (LMoveCount < LPreCutoffCount)
                {
                    LTail = GetTail();
                    if (LEntry == LTail)
                    {
                        LReferenced = LTail.GetType().GetField(CNextName, CFieldFlags).GetValue(LTail) as FixedSizeCache <string, string> .Entry;
                    }
                    else
                    {
                        LReferenced = LTail;
                    }
                    Assert.True(ValidateCutoff());
                    Assert.False(GetPreCutoff(LReferenced));
                    FTestCache.Reference(LReferenced.Key, LReferenced.Value);
                    if (GetPreCutoff(LReferenced))
                    {
                        LMoveCount++;
                    }
                }
                Assert.False(GetPreCutoff(LEntry));
                Assert.True(ValidateList());
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
        }
Exemplo n.º 13
0
        public void InternalAdd()
        {
            try
            {
                ReflectionPermission LReflectionPermission = new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess | ReflectionPermissionFlag.AllFlags);
                LReflectionPermission.Assert();

                int LPreCutoffCount;
                FixedSizeCache <string, string> .Entry LHead;
                FixedSizeCache <string, string> .Entry LCutoff;
                FixedSizeCache <string, string> .Entry LTail;

                FTestCache = new FixedSizeCache <string, string>(7);

                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();
                Assert.AreEqual(0, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());
                Assert.IsNull(LHead);
                Assert.IsNull(LCutoff);
                Assert.IsNull(LTail);

                IncrementTestEntry();

                string LKey = GetKey(1);
                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();
                FixedSizeCache <string, string> .Entry LEntry1 = LHead;

                Assert.AreEqual(1, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LKey, LHead.Key);
                Assert.AreEqual(LKey, LCutoff.Key);
                Assert.AreEqual(LKey, LTail.Key);

                Assert.IsNull(GetPrior(LHead));
                Assert.IsNull(GetNext(LHead));

                Assert.False(GetPreCutoff(LHead));

                IncrementTestEntry();

                LKey    = GetKey(2);
                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();
                FixedSizeCache <string, string> .Entry LEntry2 = GetEntry(2);

                Assert.AreEqual(2, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead, LEntry2);
                Assert.AreEqual(LCutoff, LEntry2);
                Assert.IsNull(GetNext(LEntry2));
                Assert.AreEqual(LEntry1, GetPrior(LEntry2));
                Assert.False(GetPreCutoff(LEntry2));

                Assert.AreEqual(LTail, LEntry1);
                Assert.AreEqual(LEntry2, GetNext(LEntry1));
                Assert.IsNull(GetPrior(LEntry1));
                Assert.False(GetPreCutoff(LEntry1));

                IncrementTestEntry();

                LKey    = GetKey(3);
                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();
                FixedSizeCache <string, string> .Entry LEntry3 = GetEntry(3);

                Assert.AreEqual(3, FTestCache.Count);
                Assert.AreEqual(0, PreCutoffCount());

                Assert.AreEqual(LHead, LEntry3);
                Assert.AreEqual(LCutoff, LEntry3);
                Assert.IsNull(GetNext(LEntry3));
                Assert.AreEqual(LEntry2, GetPrior(LEntry3));
                Assert.False(GetPreCutoff(LEntry3));

                Assert.AreEqual(LEntry3, GetNext(LEntry2));
                Assert.AreEqual(LEntry1, GetPrior(LEntry2));
                Assert.False(GetPreCutoff(LEntry2));

                Assert.AreEqual(LTail, LEntry1);
                Assert.AreEqual(LEntry2, GetNext(LEntry1));
                Assert.IsNull(GetPrior(LEntry1));
                Assert.False(GetPreCutoff(LEntry1));

                IncrementTestEntry();

                LKey    = GetKey(4);
                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();
                FixedSizeCache <string, string> .Entry LEntry4 = GetEntry(4);

                Assert.AreEqual(4, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead, LEntry4);
                Assert.IsNull(GetNext(LEntry4));
                Assert.AreEqual(LEntry3, GetPrior(LEntry4));
                Assert.True(GetPreCutoff(LEntry4));

                Assert.AreEqual(LCutoff, LEntry3);
                Assert.AreEqual(LEntry4, GetNext(LEntry3));
                Assert.AreEqual(LEntry2, GetPrior(LEntry3));
                Assert.False(GetPreCutoff(LEntry3));

                Assert.AreEqual(LEntry3, GetNext(LEntry2));
                Assert.AreEqual(LEntry1, GetPrior(LEntry2));
                Assert.False(GetPreCutoff(LEntry2));

                Assert.AreEqual(LTail, LEntry1);
                Assert.AreEqual(LEntry2, GetNext(LEntry1));
                Assert.IsNull(GetPrior(LEntry1));
                Assert.False(GetPreCutoff(LEntry1));

                IncrementTestEntry();

                LKey    = GetKey(5);
                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();
                FixedSizeCache <string, string> .Entry LEntry5 = GetEntry(5);

                Assert.AreEqual(5, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead, LEntry4);
                Assert.IsNull(GetNext(LEntry4));
                Assert.AreEqual(LEntry5, GetPrior(LEntry4));
                Assert.True(GetPreCutoff(LEntry4));

                Assert.AreEqual(LCutoff, LEntry5);
                Assert.AreEqual(LEntry4, GetNext(LEntry5));
                Assert.AreEqual(LEntry3, GetPrior(LEntry5));
                Assert.False(GetPreCutoff(LEntry5));

                Assert.AreEqual(LEntry5, GetNext(LEntry3));
                Assert.AreEqual(LEntry2, GetPrior(LEntry3));
                Assert.False(GetPreCutoff(LEntry3));

                Assert.AreEqual(LEntry3, GetNext(LEntry2));
                Assert.AreEqual(LEntry1, GetPrior(LEntry2));
                Assert.False(GetPreCutoff(LEntry2));

                Assert.AreEqual(LTail, LEntry1);
                Assert.AreEqual(LEntry2, GetNext(LEntry1));
                Assert.IsNull(GetPrior(LEntry1));
                Assert.False(GetPreCutoff(LEntry1));

                IncrementTestEntry();

                LKey    = GetKey(6);
                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();
                FixedSizeCache <string, string> .Entry LEntry6 = GetEntry(6);

                Assert.AreEqual(6, FTestCache.Count);
                Assert.AreEqual(1, PreCutoffCount());

                Assert.AreEqual(LHead, LEntry4);
                Assert.IsNull(GetNext(LEntry4));
                Assert.AreEqual(LEntry6, GetPrior(LEntry4));
                Assert.True(GetPreCutoff(LEntry4));

                Assert.AreEqual(LCutoff, LEntry6);
                Assert.AreEqual(LEntry4, GetNext(LEntry6));
                Assert.AreEqual(LEntry5, GetPrior(LEntry6));
                Assert.False(GetPreCutoff(LEntry6));

                Assert.AreEqual(LEntry6, GetNext(LEntry5));
                Assert.AreEqual(LEntry3, GetPrior(LEntry5));
                Assert.False(GetPreCutoff(LEntry5));

                Assert.AreEqual(LEntry5, GetNext(LEntry3));
                Assert.AreEqual(LEntry2, GetPrior(LEntry3));
                Assert.False(GetPreCutoff(LEntry3));

                Assert.AreEqual(LEntry3, GetNext(LEntry2));
                Assert.AreEqual(LEntry1, GetPrior(LEntry2));
                Assert.False(GetPreCutoff(LEntry2));

                IncrementTestEntry();

                LKey    = GetKey(7);
                LHead   = GetHead();
                LCutoff = GetCutoff();
                LTail   = GetTail();
                FixedSizeCache <string, string> .Entry LEntry7 = GetEntry(7);

                Assert.AreEqual(7, FTestCache.Count);
                Assert.AreEqual(2, PreCutoffCount());

                Assert.AreEqual(LHead, LEntry4);
                Assert.IsNull(GetNext(LEntry4));
                Assert.AreEqual(LEntry7, GetPrior(LEntry4));
                Assert.True(GetPreCutoff(LEntry4));

                Assert.AreEqual(LEntry4, GetNext(LEntry7));
                Assert.AreEqual(LEntry6, GetPrior(LEntry7));
                Assert.True(GetPreCutoff(LEntry7));

                Assert.AreEqual(LCutoff, LEntry6);
                Assert.AreEqual(LEntry7, GetNext(LEntry6));
                Assert.AreEqual(LEntry5, GetPrior(LEntry6));
                Assert.False(GetPreCutoff(LEntry6));

                Assert.AreEqual(LEntry6, GetNext(LEntry5));
                Assert.AreEqual(LEntry3, GetPrior(LEntry5));
                Assert.False(GetPreCutoff(LEntry5));

                Assert.AreEqual(LEntry5, GetNext(LEntry3));
                Assert.AreEqual(LEntry2, GetPrior(LEntry3));
                Assert.False(GetPreCutoff(LEntry3));

                Assert.AreEqual(LEntry3, GetNext(LEntry2));
                Assert.AreEqual(LEntry1, GetPrior(LEntry2));
                Assert.False(GetPreCutoff(LEntry2));
                Assert.AreEqual(LTail, LEntry1);
                Assert.AreEqual(LEntry2, GetNext(LEntry1));
                Assert.IsNull(GetPrior(LEntry1));
                Assert.False(GetPreCutoff(LEntry1));

                TestSetUp();
                FTestCache = new FixedSizeCache <string, string>(25);

                //disregarding CorrelatedReferencePeriod
                while (TestEntry < FTestCache.Size)
                {
                    IncrementTestEntry();
                    LPreCutoffCount = (int)(TestEntry * FixedSizeCache <string, string> .DefaultCutoff);
                    Assert.AreEqual(LPreCutoffCount, PreCutoffCount());

                    LHead   = GetHead();
                    LCutoff = GetCutoff();
                    LTail   = GetTail();

                    if (LPreCutoffCount == 0)
                    {
                        if (TestEntry == 1)
                        {
                            Assert.True(CompareEntry(TestEntry, LHead));
                            Assert.True(CompareEntry(TestEntry, LCutoff));
                            Assert.True(CompareEntry(TestEntry, LTail));
                            Assert.False(GetPreCutoff(TestEntry));
                        }
                        else
                        {
                            Assert.True(CompareEntry(TestEntry, LHead));
                            Assert.True(CompareEntry(TestEntry, LCutoff));
                            Assert.False(CompareEntry(TestEntry, LTail));
                            Assert.False(GetPreCutoff(TestEntry));
                        }
                    }
                    else if (((TestEntry - 1) % 3) == 0)
                    {
                        if (LPreCutoffCount == 1)
                        {
                            Assert.True(CompareEntry(TestEntry, LHead));
                        }
                        else
                        {
                            Assert.False(CompareEntry(TestEntry, LHead));
                        }
                        Assert.False(CompareEntry(TestEntry, LCutoff));
                        Assert.False(CompareEntry(TestEntry, LTail));
                        Assert.True(GetPreCutoff(TestEntry));
                    }
                    else
                    {
                        Assert.False(CompareEntry(TestEntry, LHead));
                        Assert.True(CompareEntry(TestEntry, LCutoff));
                        Assert.False(CompareEntry(TestEntry, LTail));
                        Assert.False(GetPreCutoff(TestEntry));
                    }
                }

                Assert.True(ValidateList());
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
        }
Exemplo n.º 14
0
        public static object InvokePropertyOrMethod(string className,
                                                    string methodName, object[] parameters, InvokeType invokeType)
        {
            var perm =
                new ReflectionPermission(
                    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",
                          nameof(parameters));
            }
            if (parameters[0] == null)
            {
                throw new ArgumentException(
                          "parameters cannot have a null first element for instance invocations",
                          nameof(parameters));
            }
            type = parameters[0].GetType();
            //}
            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 e;
            }
            catch (AmbiguousMatchException e)
            {
                throw e;
            }
            return(resultObject);
        }
Exemplo n.º 15
0
        internal /*public*/ Object CreateFactory(Type type)
        {
            // Create the dynamic assembly if needed.

            Type factoryType;

            try {
                IStackWalk reflection = new ReflectionPermission(ReflectionPermissionFlag.ReflectionEmit);
                reflection.Assert();

                if (_dynamicModule == null)
                {
                    // Use a unique name for each assembly.
                    String name = GetUniqueCompilationName();

                    AssemblyName assemblyName = new AssemblyName();
                    assemblyName.Name = "A_" + name;

                    // Create a new assembly.
                    AssemblyBuilder newAssembly =
                        Thread.GetDomain().DefineDynamicAssembly(assemblyName,
                                                                 AssemblyBuilderAccess.Run,
                                                                 null, //directory to persist assembly
                                                                 null, //evidence copied from caller
                                                                 null, //requiredPermissions
                                                                 null, //optionalPermissions
                                                                 null, //refused permissions
                                                                 true  //isSynchronized
                                                                 );

                    // Create a single module in the assembly.
                    _dynamicModule = newAssembly.DefineDynamicModule("M_" + name);
                }

                // Give the factory a unique name.

                String      typeName           = GetUniqueCompilationName();
                TypeBuilder factoryTypeBuilder = _dynamicModule.DefineType("T_" + typeName,
                                                                           TypeAttributes.Public,
                                                                           typeof(Object),
                                                                           _interfacesToImplement);

                // Define the CreateInstance method. It must be virtual to be an interface implementation.

                MethodBuilder method = factoryTypeBuilder.DefineMethod("CreateInstance",
                                                                       MethodAttributes.Public |
                                                                       MethodAttributes.Virtual,
                                                                       _returnedType,
                                                                       null);

                // Generate IL. The generated IL corresponds to "return new type()"
                //      newobj <type_constructor>
                //      ret

                ILGenerator     il   = method.GetILGenerator();
                ConstructorInfo cons = type.GetConstructor(_emptyParameterList);
                il.Emit(OpCodes.Newobj, cons);
                il.Emit(OpCodes.Ret);

                // Specify that this method implements CreateInstance from the inherited interface.
                factoryTypeBuilder.DefineMethodOverride(method, _methodToOverride);

                // Bake in the type.
                factoryType = factoryTypeBuilder.CreateType();

                // Create the type. This is the only place where Activator.CreateInstance is used,
                // reducing the calls to it from 1 per adapter instance to 1 per adapter type.
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }
            return(Activator.CreateInstance(factoryType));
        }
Exemplo n.º 16
0
        public void ImperativeMethod()
        {
            ReflectionPermission rp = new ReflectionPermission(ReflectionPermissionFlag.AllFlags);

            rp.Assert();
        }