Пример #1
0
        CreatePermission (SecurityElement el, PermissionState permState, bool ignoreTypeLoadFailures)
        {
            if (el == null || !(el.Tag.Equals("Permission") || el.Tag.Equals("IPermission")) )
                throw new ArgumentException( String.Format( null, Environment.GetResourceString( "Argument_WrongElementType" ), "<Permission>" ) ) ;
            Contract.EndContractBlock();
    
            String className;
            int classNameLength;
            int classNameStart;
            
            if (!ParseElementForObjectCreation( el,
                                                BuiltInPermission,
                                                out className,
                                                out classNameStart,
                                                out classNameLength ))
            {
                goto USEREFLECTION;
            }
                                              
            // We have a built in permission, figure out which it is.
                    
            // UIPermission
            // FileIOPermission
            // SecurityPermission
            // PrincipalPermission
            // ReflectionPermission
            // FileDialogPermission
            // EnvironmentPermission
            // GacIdentityPermission
            // UrlIdentityPermission
            // SiteIdentityPermission
            // ZoneIdentityPermission
            // KeyContainerPermission
            // UnsafeForHostPermission
            // HostProtectionPermission
            // StrongNameIdentityPermission
#if !FEATURE_CORECLR                              
            // IsolatedStorageFilePermission
#endif
#if !FEATURE_PAL
            // RegistryPermission
            // PublisherIdentityPermission
#endif // !FEATURE_PAL

            switch (classNameLength)
            {
                case 12:
                    // UIPermission
                    if (String.Compare(className, classNameStart, "UIPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                        return new UIPermission( permState );
                    else
                        goto USEREFLECTION;
                                
                case 16:
                    // FileIOPermission
                    if (String.Compare(className, classNameStart, "FileIOPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                        return new FileIOPermission( permState );
                    else
                        goto USEREFLECTION;
                            
                case 18:
#if !FEATURE_PAL
                    // RegistryPermission
                    // SecurityPermission
                    if (className[classNameStart] == 'R')
                    {
                        if (String.Compare(className, classNameStart, "RegistryPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                            return new RegistryPermission( permState );
                        else
                            goto USEREFLECTION;
                    }
                    else
                    {
                        if (String.Compare(className, classNameStart, "SecurityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                            return new SecurityPermission( permState );
                        else
                            goto USEREFLECTION;
                    }
#else // !FEATURE_PAL
                    if (String.Compare(className, classNameStart, "SecurityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                        return new SecurityPermission( permState );
                    else
                        goto USEREFLECTION;
#endif // !FEATURE_PAL
                  
#if !FEATURE_CORECLR              
                case 19:
                    // PrincipalPermission
                    if (String.Compare(className, classNameStart, "PrincipalPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                        return new PrincipalPermission( permState );
                    else
                        goto USEREFLECTION;
#endif // !FEATURE_CORECLR
                case 20:
                    // ReflectionPermission
                    // FileDialogPermission
                    if (className[classNameStart] == 'R')
                    {
                        if (String.Compare(className, classNameStart, "ReflectionPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                            return new ReflectionPermission( permState );
                        else
                            goto USEREFLECTION;
                    }
                    else
                    {
                        if (String.Compare(className, classNameStart, "FileDialogPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                            return new FileDialogPermission( permState );
                        else
                            goto USEREFLECTION;
                    }

                case 21:
                    // EnvironmentPermission
                    // UrlIdentityPermission
                    // GacIdentityPermission
                    if (className[classNameStart] == 'E')
                    {
                        if (String.Compare(className, classNameStart, "EnvironmentPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                            return new EnvironmentPermission( permState );
                        else
                            goto USEREFLECTION;
                    }
                    else if (className[classNameStart] == 'U')
                    {
                        if (String.Compare(className, classNameStart, "UrlIdentityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                            return new UrlIdentityPermission( permState );
                        else
                            goto USEREFLECTION;
                    }
                    else
                    {
                        if (String.Compare(className, classNameStart, "GacIdentityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                            return new GacIdentityPermission( permState );
                        else
                            goto USEREFLECTION;
                    }

                            
                case 22:
                    // SiteIdentityPermission
                    // ZoneIdentityPermission
                    // KeyContainerPermission
                    if (className[classNameStart] == 'S')
                    {
                        if (String.Compare(className, classNameStart, "SiteIdentityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                            return new SiteIdentityPermission( permState );
                        else
                            goto USEREFLECTION;
                    }
                    else if (className[classNameStart] == 'Z')
                    {
                        if (String.Compare(className, classNameStart, "ZoneIdentityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                            return new ZoneIdentityPermission( permState );
                        else
                            goto USEREFLECTION;
                    }
                    else
                    {
#if !FEATURE_PAL
                        if (String.Compare(className, classNameStart, "KeyContainerPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                            return new KeyContainerPermission( permState );
                        else
#endif // !FEATURE_PAL
                            goto USEREFLECTION;
                    }


                case 24:
                    // HostProtectionPermission
                    if (String.Compare(className, classNameStart, "HostProtectionPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                        return new HostProtectionPermission( permState );
                    else
                        goto USEREFLECTION;

#if FEATURE_X509 && FEATURE_CAS_POLICY
                case 27:
                    // PublisherIdentityPermission
                    if (String.Compare(className, classNameStart, "PublisherIdentityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                        return new PublisherIdentityPermission( permState );
                    else
                        goto USEREFLECTION;
#endif // FEATURE_X509 && FEATURE_CAS_POLICY

                case 28:
                    // StrongNameIdentityPermission
                    if (String.Compare(className, classNameStart, "StrongNameIdentityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                        return new StrongNameIdentityPermission( permState );
                    else
                        goto USEREFLECTION;
#if !FEATURE_CORECLR                                                          
                case 29:
                    // IsolatedStorageFilePermission
                    if (String.Compare(className, classNameStart, "IsolatedStorageFilePermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                        return new IsolatedStorageFilePermission( permState );
                    else
                        goto USEREFLECTION;
#endif                            
                default:
                    goto USEREFLECTION;
            }
    
USEREFLECTION:

            Object[] objs = new Object[1];
            objs[0] = permState;

            Type permClass = null;
            IPermission perm = null;

            new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Assert();
            permClass = GetClassFromElement(el, ignoreTypeLoadFailures);
            if (permClass == null)
                return null;
            if (!(typeof(IPermission).IsAssignableFrom(permClass)))
                throw new ArgumentException( Environment.GetResourceString("Argument_NotAPermissionType") );

            perm = (IPermission) Activator.CreateInstance(permClass, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, objs, null );

            return perm;
        }
Пример #2
0
        CreatePermission(SecurityElement el, PermissionState permState, bool ignoreTypeLoadFailures)
        {
            if (el == null || !(el.Tag.Equals("Permission") || el.Tag.Equals("IPermission")))
            {
                throw new ArgumentException(String.Format(null, Environment.GetResourceString("Argument_WrongElementType"), "<Permission>"));
            }
            Contract.EndContractBlock();

            String className;
            int    classNameLength;
            int    classNameStart;

            if (!ParseElementForObjectCreation(el,
                                               BuiltInPermission,
                                               out className,
                                               out classNameStart,
                                               out classNameLength))
            {
                goto USEREFLECTION;
            }

            // We have a built in permission, figure out which it is.

            // UIPermission
            // FileIOPermission
            // SecurityPermission
            // PrincipalPermission
            // ReflectionPermission
            // FileDialogPermission
            // EnvironmentPermission
            // GacIdentityPermission
            // UrlIdentityPermission
            // SiteIdentityPermission
            // ZoneIdentityPermission
            // KeyContainerPermission
            // UnsafeForHostPermission
            // HostProtectionPermission
            // StrongNameIdentityPermission
#if !FEATURE_CORECLR
            // IsolatedStorageFilePermission
#endif
            // RegistryPermission
            // PublisherIdentityPermission

            switch (classNameLength)
            {
            case 12:
                // UIPermission
                if (String.Compare(className, classNameStart, "UIPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                {
                    return(new UIPermission(permState));
                }
                else
                {
                    goto USEREFLECTION;
                }

            case 16:
                // FileIOPermission
                if (String.Compare(className, classNameStart, "FileIOPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                {
                    return(new FileIOPermission(permState));
                }
                else
                {
                    goto USEREFLECTION;
                }

            case 18:
                // RegistryPermission
                // SecurityPermission
                if (className[classNameStart] == 'R')
                {
                    if (String.Compare(className, classNameStart, "RegistryPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                    {
                        return(new RegistryPermission(permState));
                    }
                    else
                    {
                        goto USEREFLECTION;
                    }
                }
                else
                {
                    if (String.Compare(className, classNameStart, "SecurityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                    {
                        return(new SecurityPermission(permState));
                    }
                    else
                    {
                        goto USEREFLECTION;
                    }
                }

#if !FEATURE_CORECLR
            case 19:
                // PrincipalPermission
                if (String.Compare(className, classNameStart, "PrincipalPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                {
                    return(new PrincipalPermission(permState));
                }
                else
                {
                    goto USEREFLECTION;
                }
#endif // !FEATURE_CORECLR
            case 20:
                // ReflectionPermission
                // FileDialogPermission
                if (className[classNameStart] == 'R')
                {
                    if (String.Compare(className, classNameStart, "ReflectionPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                    {
                        return(new ReflectionPermission(permState));
                    }
                    else
                    {
                        goto USEREFLECTION;
                    }
                }
                else
                {
                    if (String.Compare(className, classNameStart, "FileDialogPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                    {
                        return(new FileDialogPermission(permState));
                    }
                    else
                    {
                        goto USEREFLECTION;
                    }
                }

            case 21:
                // EnvironmentPermission
                // UrlIdentityPermission
                // GacIdentityPermission
                if (className[classNameStart] == 'E')
                {
                    if (String.Compare(className, classNameStart, "EnvironmentPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                    {
                        return(new EnvironmentPermission(permState));
                    }
                    else
                    {
                        goto USEREFLECTION;
                    }
                }
                else if (className[classNameStart] == 'U')
                {
                    if (String.Compare(className, classNameStart, "UrlIdentityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                    {
                        return(new UrlIdentityPermission(permState));
                    }
                    else
                    {
                        goto USEREFLECTION;
                    }
                }
                else
                {
                    if (String.Compare(className, classNameStart, "GacIdentityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                    {
                        return(new GacIdentityPermission(permState));
                    }
                    else
                    {
                        goto USEREFLECTION;
                    }
                }


            case 22:
                // SiteIdentityPermission
                // ZoneIdentityPermission
                // KeyContainerPermission
                if (className[classNameStart] == 'S')
                {
                    if (String.Compare(className, classNameStart, "SiteIdentityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                    {
                        return(new SiteIdentityPermission(permState));
                    }
                    else
                    {
                        goto USEREFLECTION;
                    }
                }
                else if (className[classNameStart] == 'Z')
                {
                    if (String.Compare(className, classNameStart, "ZoneIdentityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                    {
                        return(new ZoneIdentityPermission(permState));
                    }
                    else
                    {
                        goto USEREFLECTION;
                    }
                }
                else
                {
                    if (String.Compare(className, classNameStart, "KeyContainerPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                    {
                        return(new KeyContainerPermission(permState));
                    }
                    else
                    {
                        goto USEREFLECTION;
                    }
                }


            case 24:
                // HostProtectionPermission
                if (String.Compare(className, classNameStart, "HostProtectionPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                {
                    return(new HostProtectionPermission(permState));
                }
                else
                {
                    goto USEREFLECTION;
                }

#if FEATURE_X509 && FEATURE_CAS_POLICY
            case 27:
                // PublisherIdentityPermission
                if (String.Compare(className, classNameStart, "PublisherIdentityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                {
                    return(new PublisherIdentityPermission(permState));
                }
                else
                {
                    goto USEREFLECTION;
                }
#endif // FEATURE_X509 && FEATURE_CAS_POLICY

            case 28:
                // StrongNameIdentityPermission
                if (String.Compare(className, classNameStart, "StrongNameIdentityPermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                {
                    return(new StrongNameIdentityPermission(permState));
                }
                else
                {
                    goto USEREFLECTION;
                }

#if !FEATURE_CORECLR
            case 29:
                // IsolatedStorageFilePermission
                if (String.Compare(className, classNameStart, "IsolatedStorageFilePermission", 0, classNameLength, StringComparison.Ordinal) == 0)
                {
                    return(new IsolatedStorageFilePermission(permState));
                }
                else
                {
                    goto USEREFLECTION;
                }
#endif
            default:
                goto USEREFLECTION;
            }

USEREFLECTION:

            Object[] objs = new Object[1];
            objs[0] = permState;

            Type        permClass = null;
            IPermission perm      = null;

            new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Assert();
            permClass = GetClassFromElement(el, ignoreTypeLoadFailures);
            if (permClass == null)
            {
                return(null);
            }
            if (!(typeof(IPermission).IsAssignableFrom(permClass)))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_NotAPermissionType"));
            }

            perm = (IPermission)Activator.CreateInstance(permClass, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, objs, null);

            return(perm);
        }
Пример #3
0
 public override System.Security.CodeAccessPermission CreatePermission(System.Security.Permissions.PermissionState state) => throw ADP.OleDb();