System.Security.Util.XMLUtil.ParseElementForObjectCreation C# (CSharp) Method

ParseElementForObjectCreation() private static method

private static ParseElementForObjectCreation ( SecurityElement el, String requiredNamespace, String &className, int &classNameStart, int &classNameLength ) : bool
el System.Security.SecurityElement
requiredNamespace String
className String
classNameStart int
classNameLength int
return bool
        ParseElementForObjectCreation( SecurityElement el,
                                       String requiredNamespace,
                                       out String className,
                                       out int classNameStart,
                                       out int classNameLength )
        {
            className = null;
            classNameStart = 0;
            classNameLength = 0;
            
            int requiredNamespaceLength = requiredNamespace.Length;

            String fullClassName = el.Attribute( "class" );
            
            if (fullClassName == null)
            {
                throw new ArgumentException( Environment.GetResourceString( "Argument_NoClass" ) );
            }
            
            if (fullClassName.IndexOf('\'') >= 0)
            {
                fullClassName = fullClassName.Replace( '\'', '\"' );
            }
            
            if (!PermissionToken.IsMscorlibClassName( fullClassName ))
            {
                return false;
            }

            int commaIndex = fullClassName.IndexOf( ',' );
            int namespaceClassNameLength;
            
            // If the classname is tagged with assembly information, find where
            // the assembly information begins.
            
            if (commaIndex == -1)
            {
                namespaceClassNameLength = fullClassName.Length;
            }
            else
            {
                namespaceClassNameLength = commaIndex;
            }

            // Only if the length of the class name is greater than the namespace info
            // on our requiredNamespace do we continue
            // with our check.
            
            if (namespaceClassNameLength > requiredNamespaceLength)
            {
                // Make sure we are in the required namespace.
                if (fullClassName.StartsWith(requiredNamespace, StringComparison.Ordinal))
                {
                    className = fullClassName;
                    classNameLength = namespaceClassNameLength - requiredNamespaceLength;
                    classNameStart = requiredNamespaceLength;
                    return true;
                }
            }
            
            return false;
        }

Usage Example

        public static CodeGroup CreateCodeGroup(SecurityElement el)
        {
            if (el == null || !el.Tag.Equals("CodeGroup"))
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongElementType"), "<CodeGroup>"));
            }
            string strA;
            int    indexA;
            int    num;

            if (XMLUtil.ParseElementForObjectCreation(el, "System.Security.Policy.", out strA, out indexA, out num))
            {
                switch (num)
                {
                case 12:
                    if (string.Compare(strA, indexA, "NetCodeGroup", 0, num, StringComparison.Ordinal) == 0)
                    {
                        return(new NetCodeGroup());
                    }
                    break;

                case 13:
                    if (string.Compare(strA, indexA, "FileCodeGroup", 0, num, StringComparison.Ordinal) == 0)
                    {
                        return(new FileCodeGroup());
                    }
                    break;

                case 14:
                    if (string.Compare(strA, indexA, "UnionCodeGroup", 0, num, StringComparison.Ordinal) == 0)
                    {
                        return(new UnionCodeGroup());
                    }
                    break;

                default:
                    if (num == 19)
                    {
                        if (string.Compare(strA, indexA, "FirstMatchCodeGroup", 0, num, StringComparison.Ordinal) == 0)
                        {
                            return(new FirstMatchCodeGroup());
                        }
                    }
                    break;
                }
            }
            new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Assert();
            Type classFromElement = XMLUtil.GetClassFromElement(el, true);

            if (classFromElement == null)
            {
                return(null);
            }
            if (!typeof(CodeGroup).IsAssignableFrom(classFromElement))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_NotACodeGroupType"));
            }
            return((CodeGroup)Activator.CreateInstance(classFromElement, true));
        }
All Usage Examples Of System.Security.Util.XMLUtil::ParseElementForObjectCreation