コード例 #1
0
        //------------------------------------------------------------
        // SecurityUtil.AppendSecurityAttribute (1)
        //
        /// <summary>
        ///
        /// </summary>
        /// <param name="secAttrType"></param>
        /// <param name="positionalArgs"></param>
        /// <param name="argNames"></param>
        /// <param name="argValues"></param>
        /// <param name="permissionSets"></param>
        /// <param name="excp"></param>
        /// <returns></returns>
        //------------------------------------------------------------
        public static bool AppendSecurityAttribute(
            Type secAttrType,
            Object[] positionalArgs,
            String[] argNames,
            Object[] argValues,
            Dictionary <SecurityAction, PermissionSet> permissionSets,
            out Exception excp)
        {
            excp = null;
            if (permissionSets == null)
            {
                return(false);
            }
            if (secAttrType == null ||
                positionalArgs == null || positionalArgs.Length == 0)
            {
                return(false);
            }

            SecurityAction secAction;

            try
            {
                secAction = (SecurityAction)positionalArgs[0];
            }
            catch (InvalidCastException ex)
            {
                excp = ex;
                return(false);
            }

            //switch (secAction)
            //{
            //    case SecurityAction.Assert:
            //    case SecurityAction.Demand:
            //    case SecurityAction.Deny:
            //    case SecurityAction.InheritanceDemand:
            //    case SecurityAction.LinkDemand:
            //    case SecurityAction.PermitOnly:
            //    case SecurityAction.RequestMinimum:
            //    case SecurityAction.RequestOptional:
            //    case SecurityAction.RequestRefuse:
            //        break;
            //    default:
            //        return false;
            //}

            // Create the attribute instance with its positional arguments

            Object activatedObj = ReflectionUtil.CreateInstance(
                secAttrType,
                positionalArgs,
                out excp);

            if (activatedObj == null || excp != null)
            {
                return(false);
            }

            // Assign the named arguments to the fields or properties.

            int namedCount = 0;

            if (argNames != null && (namedCount = argNames.Length) > 0)
            {
                if (argValues == null || argValues.Length != namedCount)
                {
                    return(false);
                }
                Object[] narg = new Object[1];

                for (int i = 0; i < argNames.Length; ++i)
                {
                    string name = argNames[i];
                    if (String.IsNullOrEmpty(name))
                    {
                        return(false);
                    }

                    MemberInfo[] membs = secAttrType.GetMember(name);
                    if (membs.Length != 1)
                    {
                        return(false);
                    }

                    narg[0] = argValues[i];
                    MemberTypes  membType  = membs[0].MemberType;
                    BindingFlags bindFlags = 0;
                    if (membType == MemberTypes.Property)
                    {
                        bindFlags = BindingFlags.SetProperty;
                    }
                    else if (membType == MemberTypes.Field)
                    {
                        bindFlags = BindingFlags.SetField;
                    }
                    else
                    {
                        return(false);
                    }

                    if (!ReflectionUtil.InvokeMember(
                            secAttrType,
                            name,
                            bindFlags,
                            null,
                            activatedObj,
                            narg,
                            out excp))
                    {
                        return(false);
                    }
                }
            }

            SecurityAttribute secAttr = activatedObj as SecurityAttribute;

            if (secAttr == null)
            {
                return(false);
            }

            IPermission secPerm = secAttr.CreatePermission();

            if (secAttr != null)
            {
                PermissionSet permSet = null;
                if (!permissionSets.TryGetValue(secAction, out permSet))
                {
                    permSet = new PermissionSet(null);
                    permissionSets.Add(secAction, permSet);
                }
                else if (permSet == null)
                {
                    permSet = new PermissionSet(null);
                    permissionSets[secAction] = permSet;
                }
                DebugUtil.Assert(permSet != null);

                permSet.AddPermission(secPerm);
                return(true);
            }
            return(false);
        }
コード例 #2
0
        //------------------------------------------------------------
        // SecurityUtil.AppendSecurityAttribute (2)
        //
        /// <summary>
        ///
        /// </summary>
        /// <param name="secAttrType"></param>
        /// <param name="positionalArgs"></param>
        /// <param name="argNames"></param>
        /// <param name="argValues"></param>
        /// <param name="permissionSets"></param>
        /// <param name="excp"></param>
        /// <returns></returns>
        //------------------------------------------------------------
        public static bool AppendSecurityAttribute(
            Type secAttrType,
            Object[] positionalArgs,
            List <string> fieldNames,
            List <object> fieldValues,
            List <string> propertyNames,
            List <object> propertyValues,
            Dictionary <SecurityAction, PermissionSet> permissionSets,
            out Exception excp)
        {
            excp = null;
            if (permissionSets == null)
            {
                return(false);
            }
            if (secAttrType == null ||
                positionalArgs == null || positionalArgs.Length == 0)
            {
                return(false);
            }

            int fieldCount = fieldNames != null ? fieldNames.Count : 0;

            if (fieldCount != (fieldValues != null ? fieldValues.Count : 0))
            {
                return(false);
            }
            int propertyCount = propertyNames != null ? propertyNames.Count : 0;

            if (propertyCount != (propertyValues != null ? propertyValues.Count : 0))
            {
                return(false);
            }

            SecurityAction secAction;

            try
            {
                secAction = (SecurityAction)positionalArgs[0];
            }
            catch (InvalidCastException ex)
            {
                excp = ex;
                return(false);
            }

            //switch (secAction)
            //{
            //    case SecurityAction.Assert:
            //    case SecurityAction.Demand:
            //    case SecurityAction.Deny:
            //    case SecurityAction.InheritanceDemand:
            //    case SecurityAction.LinkDemand:
            //    case SecurityAction.PermitOnly:
            //    case SecurityAction.RequestMinimum:
            //    case SecurityAction.RequestOptional:
            //    case SecurityAction.RequestRefuse:
            //        break;
            //    default:
            //        return false;
            //}

            // Create the attribute instance with its positional arguments

            Object activatedObj = ReflectionUtil.CreateInstance(
                secAttrType,
                positionalArgs,
                out excp);

            if (activatedObj == null || excp != null)
            {
                return(false);
            }

            // Assign the named arguments to the fields.

            Object[] narg = new Object[1];

            if (fieldCount > 0)
            {
                for (int i = 0; i < fieldCount; ++i)
                {
                    narg[0] = fieldValues[i];
                    if (!ReflectionUtil.InvokeMember(
                            secAttrType,
                            fieldNames[i],
                            BindingFlags.SetField,
                            null,
                            activatedObj,
                            narg,
                            out excp))
                    {
                        return(false);
                    }
                }
            }

            // Assign the named arguments to the properties.

            if (propertyCount > 0)
            {
                for (int i = 0; i < propertyCount; ++i)
                {
                    narg[0] = propertyValues[i];
                    if (!ReflectionUtil.InvokeMember(
                            secAttrType,
                            propertyNames[i],
                            BindingFlags.SetProperty,
                            null,
                            activatedObj,
                            narg,
                            out excp))
                    {
                        return(false);
                    }
                }
            }

            SecurityAttribute secAttr = activatedObj as SecurityAttribute;

            if (secAttr == null)
            {
                return(false);
            }

            IPermission secPerm = secAttr.CreatePermission();

            if (secAttr != null)
            {
                PermissionSet permSet = null;
                if (!permissionSets.TryGetValue(secAction, out permSet))
                {
                    permSet = new PermissionSet(null);
                    permissionSets.Add(secAction, permSet);
                }
                else if (permSet == null)
                {
                    permSet = new PermissionSet(null);
                    permissionSets[secAction] = permSet;
                }
                DebugUtil.Assert(permSet != null);

                permSet.AddPermission(secPerm);
                return(true);
            }
            return(false);
        }