当前位置: 首页>>代码示例>>C#>>正文


C# INameCreationService接口代码示例

本文整理汇总了C#中System.ComponentModel.Design.Serialization.INameCreationService接口的典型用法代码示例。如果您正苦于以下问题:C# INameCreationService接口的具体用法?C# INameCreationService怎么用?C# INameCreationService使用的例子?那么恭喜您, 这里精选的接口代码示例或许可以为您提供帮助。


INameCreationService接口属于System.ComponentModel.Design.Serialization命名空间,在下文中一共展示了INameCreationService接口的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: NameCreationService

//引入命名空间
using System;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Globalization;

namespace NameCreationServiceExample
{
    public class NameCreationService : System.ComponentModel.Design.Serialization.INameCreationService
    {
        public NameCreationService()
        {
        }

        // Creates an identifier for a particular data type that does not conflict 
        // with the identifiers of any components in the specified collection.
        public string CreateName(System.ComponentModel.IContainer container, System.Type dataType)
        {
            // Create a basic type name string.
            string baseName = dataType.Name;
            int uniqueID = 1;

            bool unique = false;            
            // Continue to increment uniqueID numeral until a 
            // unique ID is located.
            while( !unique )
            {
                unique = true;
                // Check each component in the container for a matching 
                // base type name and unique ID.
                for(int i=0; i<container.Components.Count; i++)
                {
                    // Check component name for match with unique ID string.
                    if( container.Components[i].Site.Name.StartsWith(baseName+uniqueID.ToString()) )
                    {
                        // If a match is encountered, set flag to recycle 
                        // collection, increment ID numeral, and restart.
                        unique = false;
                        uniqueID++;
                        break;
                    }
                }
            }
            
            return baseName+uniqueID.ToString();
        }

        // Returns whether the specified name contains 
        // all valid character types.
        public bool IsValidName(string name)
        {            
            for(int i = 0; i < name.Length; i++)
            {
                char ch = name[i];
                UnicodeCategory uc = Char.GetUnicodeCategory(ch);
                switch (uc) 
                {
                    case UnicodeCategory.UppercaseLetter:       
                    case UnicodeCategory.LowercaseLetter:     
                    case UnicodeCategory.TitlecaseLetter:                                                  
                    case UnicodeCategory.DecimalDigitNumber:                         
                        break;
                    default:
                        return false;                
                }
            }
            return true;        
         }

        // Throws an exception if the specified name does not contain 
        // all valid character types.
        public void ValidateName(string name)
        {
            for(int i = 0; i < name.Length; i++)
            {
                char ch = name[i];
                UnicodeCategory uc = Char.GetUnicodeCategory(ch);
                switch (uc) 
                {
                    case UnicodeCategory.UppercaseLetter:       
                    case UnicodeCategory.LowercaseLetter:     
                    case UnicodeCategory.TitlecaseLetter:                                                  
                    case UnicodeCategory.DecimalDigitNumber:                         
                        break;
                    default:
                        throw new Exception("The name '"+name+"' is not a valid identifier.");                
                }
            }
        }
     }
}
开发者ID:.NET开发者,项目名称:System.ComponentModel.Design.Serialization,代码行数:91,代码来源:INameCreationService


注:本文中的System.ComponentModel.Design.Serialization.INameCreationService接口示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。