csharp 用于.NET的TcmUri对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 用于.NET的TcmUri对象相关的知识,希望对你有一定的参考价值。

using System;
using System.Text.RegularExpressions;
using Tridion.ContentManager.CoreService.Client;

namespace TridionUtils
{
    /// <summary>
    /// Creates TcmUri class from a string. Makes it easy to retrieve TcmUri properties like PublicationId, ItemId, ItemTypeId and Version.
    /// Usage: var PubliationId = new TcmUri("tcm:45-3456-64").PublicationId;
    /// </summary>
    public class TcmUri
    {
        public const string Null = "tcm:0-0-0";
        public int ItemId { get; set; }
        private int _publicationId;
        public bool IsVersionless;

        public int PublicationId
        { 
            get { if(_publicationId == 0) return ItemId; else return _publicationId;}
            set { _publicationId = value;}
        }
        public int ItemTypeId { get; set; }
        public int Version { get; set; }
        public ItemType ItemType
        {
            get { return (ItemType)ItemTypeId; }
        }
        public bool isValid { get; set; }
        
        public TcmUri(string uri)
        {
            Regex re = new Regex(@"tcm:(\d+)-(\d+)-?(\d*)-?v?(\d*)");
            Match m = re.Match(uri);
            if (m.Success)
            {
                isValid = true;
                _publicationId = Convert.ToInt32(m.Groups[1].Value);
                ItemId = Convert.ToInt32(m.Groups[2].Value);
                if (m.Groups.Count > 3 && !string.IsNullOrEmpty(m.Groups[3].Value))
                {
                    ItemTypeId = Convert.ToInt32(m.Groups[3].Value);
                }
                else
                {
                    ItemTypeId = (int)ItemType.Component;
                }
                if (m.Groups.Count > 4 && !string.IsNullOrEmpty(m.Groups[4].Value))
                {
                    Version = Convert.ToInt32(m.Groups[4].Value);
                    IsVersionless = false;
                }
                else
                {
                    Version = 0;
                    IsVersionless = true;
                }
            }
            else
            {
                isValid = false;
            }
            //add logging if fails, outputting the uri parameter
        }

        public TcmUri(int publicationId, int itemId, int itemTypeId, int version)
        {
            PublicationId = publicationId;
            ItemId = itemId;
            ItemTypeId = itemTypeId;
            Version = version;
        }

        public TcmUri(int itemId, Tridion.ContentManager.CoreService.Client.ItemType itemType, int publicationId)
        {
            string uri = string.Format("tcm:{0}-{1}-{2}", publicationId, itemId, itemType);
            new TcmUri(uri);
        }

        public override string ToString()
        {
            return IsComponentUri() ?
                string.Format("tcm:{0}-{1}", PublicationId, ItemId) : string.Format("tcm:{0}-{1}-{2}", PublicationId, ItemId, ItemTypeId);
        }

        private bool IsComponentUri()
        {
            return ItemType == ItemType.Component;
        }

        internal static bool IsValid(string value)
        {
                TcmUri toTest = new TcmUri(value);
                return toTest.isValid;
        }
    }
}

以上是关于csharp 用于.NET的TcmUri对象的主要内容,如果未能解决你的问题,请参考以下文章

csharp 用于处理.net模型状态的可重用ValidationFilter属性

csharp 用于ASP.NET MVC的Castle Windsor IoC容器设置

csharp .NET文件,用于显示如何使用和配置TraceSource进行日志记录。

csharp ASP .NET授权模块,用于从呈现的页面中删除不可访问的控件

csharp 用于处理DataTables.net客户端表呈现插件的Ajax调用的C#代码。

csharp 用于检查对象是否为null的扩展方法