YAXLib.StringUtils.ExttractPathAndAliasFromLocationString C# (CSharp) Method

ExttractPathAndAliasFromLocationString() public static method

Extracts the path and alias from location string. A pure path location string: level1/level2 A location string augmented with alias: level1/level2#somename Here path is "level1/level2" and alias is "somename".
public static ExttractPathAndAliasFromLocationString ( string locationString, string &path, string &alias ) : void
locationString string The location string.
path string The path to be extracted.
alias string The alias to be extracted.
return void
        public static void ExttractPathAndAliasFromLocationString(string locationString, out string path, out string alias)
        {
            int poundIndex = locationString.IndexOf('#');
            if(poundIndex >= 0)
            {
                if (poundIndex == 0)
                    path = "";
                else
                    path = locationString.Substring(0, poundIndex).Trim();

                if (poundIndex == locationString.Length - 1)
                    alias = "";
                else
                    alias = locationString.Substring(poundIndex + 1).Trim();
            }
            else
            {
                path = locationString;
                alias = "";
            }
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Processes the specified attribute which is an instance of <c>YAXAttribute</c>.
        /// </summary>
        /// <param name="attr">The attribute to process.</param>
        private void ProcessYaxAttribute(object attr)
        {
            if (attr is YAXCommentAttribute)
            {
                string comment = (attr as YAXCommentAttribute).Comment;
                if (!String.IsNullOrEmpty(comment))
                {
                    string[] comments = comment.Split(new [] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < comments.Length; i++)
                    {
                        comments[i] = String.Format(" {0} ", comments[i].Trim());
                    }

                    this.Comment = comments;
                }
            }
            else if (attr is YAXSerializableFieldAttribute)
            {
                IsAttributedAsSerializable = true;
            }
            else if (attr is YAXAttributeForClassAttribute)
            {
                // it is required that YAXCustomSerializerAttribute is processed earlier
                if (ReflectionUtils.IsBasicType(MemberType) || CustomSerializerType != null ||
                    (m_collectionAttributeInstance != null && m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially))
                {
                    IsSerializedAsAttribute = true;
                    SerializationLocation   = ".";
                }
            }
            else if (attr is YAXValueForClassAttribute)
            {
                // it is required that YAXCustomSerializerAttribute is processed earlier
                if (ReflectionUtils.IsBasicType(MemberType) || CustomSerializerType != null ||
                    (m_collectionAttributeInstance != null && m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially))
                {
                    IsSerializedAsValue   = true;
                    SerializationLocation = ".";
                }
            }
            else if (attr is YAXAttributeForAttribute)
            {
                // it is required that YAXCustomSerializerAttribute is processed earlier
                if (ReflectionUtils.IsBasicType(MemberType) || CustomSerializerType != null ||
                    (m_collectionAttributeInstance != null && m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially))
                {
                    IsSerializedAsAttribute = true;
                    string path, alias;
                    StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXAttributeForAttribute).Parent, out path, out alias);

                    SerializationLocation = path;
                    if (!String.IsNullOrEmpty(alias))
                    {
                        Alias = StringUtils.RefineSingleElement(alias);
                    }
                }
            }
            else if (attr is YAXElementForAttribute)
            {
                IsSerializedAsElement = true;

                string path, alias;
                StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXElementForAttribute).Parent, out path, out alias);

                SerializationLocation = path;
                if (!String.IsNullOrEmpty(alias))
                {
                    Alias = StringUtils.RefineSingleElement(alias);
                }
            }
            else if (attr is YAXValueForAttribute)
            {
                // it is required that YAXCustomSerializerAttribute is processed earlier
                if (ReflectionUtils.IsBasicType(this.MemberType) || CustomSerializerType != null ||
                    (m_collectionAttributeInstance != null && m_collectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.Serially))
                {
                    IsSerializedAsValue = true;

                    string path, alias;
                    StringUtils.ExttractPathAndAliasFromLocationString((attr as YAXValueForAttribute).Parent, out path, out alias);

                    SerializationLocation = path;
                    if (!String.IsNullOrEmpty(alias))
                    {
                        Alias = StringUtils.RefineSingleElement(alias);
                    }
                }
            }
            else if (attr is YAXDontSerializeAttribute)
            {
                IsAttributedAsDontSerialize = true;
            }
            else if (attr is YAXSerializeAsAttribute)
            {
                Alias = StringUtils.RefineSingleElement((attr as YAXSerializeAsAttribute).SerializeAs);
            }
            else if (attr is YAXCollectionAttribute)
            {
                m_collectionAttributeInstance = attr as YAXCollectionAttribute;
            }
            else if (attr is YAXDictionaryAttribute)
            {
                m_dictionaryAttributeInstance = attr as YAXDictionaryAttribute;
            }
            else if (attr is YAXErrorIfMissedAttribute)
            {
                var temp = attr as YAXErrorIfMissedAttribute;
                DefaultValue  = temp.DefaultValue;
                TreatErrorsAs = temp.TreatAs;
            }
            else if (attr is YAXFormatAttribute)
            {
                Format = (attr as YAXFormatAttribute).Format;
            }
            else if (attr is YAXNotCollectionAttribute)
            {
                // arrays are always treated as collections
                if (!ReflectionUtils.IsArray(MemberType))
                {
                    IsAttributedAsNotCollection = true;
                }
            }
            else if (attr is YAXCustomSerializerAttribute)
            {
                Type serType = (attr as YAXCustomSerializerAttribute).CustomSerializerType;

                Type genTypeArg;
                bool isDesiredInterface = ReflectionUtils.IsDerivedFromGenericInterfaceType(serType, typeof(ICustomSerializer <>), out genTypeArg);

                if (!isDesiredInterface)
                {
                    throw new YAXException("The provided custom serialization type is not derived from the proper interface");
                }
                else if (genTypeArg != this.MemberType)
                {
                    throw new YAXException("The generic argument of the class and the member type do not match");
                }
                else
                {
                    CustomSerializerType = serType;
                }
            }
            else if (attr is YAXPreserveWhitespaceAttribute)
            {
                PreservesWhitespace = true;
            }
            else if (attr is YAXSerializableTypeAttribute)
            {
                // this should not happen
                throw new Exception("This attribute is not applicable to fields and properties!");
            }
            else if (attr is YAXNamespaceAttribute)
            {
                var nsAttrib = (attr as YAXNamespaceAttribute);
                Namespace       = nsAttrib.Namespace;
                NamespacePrefix = nsAttrib.Prefix;
            }
            else
            {
                throw new Exception("Added new attribute type to the library but not yet processed!");
            }
        }
All Usage Examples Of YAXLib.StringUtils::ExttractPathAndAliasFromLocationString