次の方法で共有


INameCreationService インターフェイス

オブジェクトの一意の名前を生成できるサービスを提供します。

この型のすべてのメンバの一覧については、INameCreationService メンバ を参照してください。

Public Interface INameCreationService
[C#]
public interface INameCreationService
[C++]
public __gc __interface INameCreationService
[JScript]
public interface INameCreationService

解説

DesignerLoader は、このサービスを実装して、デザイナがオブジェクトの新しい一意の名前を作成する手段を提供できます。このサービスが使用できない場合、デザイナは既定の実装を使用します。

使用例

[Visual Basic, C#, C++] INameCreationService を実装するコード例を次に示します。このサービスでは、指定したコンテナに含まれる名前のいずれとも一致しない種類の、一意な名前を作成できます。また、指定した名前文字列を検証することもできます。

 
Imports System
Imports System.ComponentModel.Design
Imports System.ComponentModel.Design.Serialization
Imports System.Globalization

Public Class NameCreationService
    Implements System.ComponentModel.Design.Serialization.INameCreationService

    Public Sub New()
    End Sub

    ' Creates an identifier for a particular data type that does not conflict 
    ' with the identifiers of any components in the specified collection
    Public Function CreateName(ByVal container As System.ComponentModel.IContainer, ByVal dataType As System.Type) As String Implements INameCreationService.CreateName
        ' Create a basic type name string
        Dim baseName As String = dataType.Name
        Dim uniqueID As Integer = 1

        Dim unique As Boolean = False
        ' Continue to increment uniqueID numeral until a unique ID is located.
        While Not unique
            unique = True
            ' Check each component in the container for a matching 
            ' base type name and unique ID.
            Dim i As Integer
            For i = 0 To container.Components.Count - 1
                ' Check component name for match with unique ID string.
                If container.Components(i).Site.Name.StartsWith((baseName + uniqueID.ToString())) Then
                    ' If a match is encountered, set flag to recycle 
                    ' collection, increment ID numeral, and restart.
                    unique = False
                    uniqueID += 1
                    Exit For
                End If
            Next i
        End While

        Return baseName + uniqueID.ToString()
    End Function

    ' Returns whether the specified name contains 
    ' all valid character types.
    Public Function IsValidName(ByVal name As String) As Boolean Implements INameCreationService.IsValidName
        Dim i As Integer
        For i = 0 To name.Length - 1
            Dim ch As Char = name.Chars(i)
            Dim uc As UnicodeCategory = [Char].GetUnicodeCategory(ch)
            Select Case uc
                Case UnicodeCategory.UppercaseLetter, UnicodeCategory.LowercaseLetter, UnicodeCategory.TitlecaseLetter, UnicodeCategory.DecimalDigitNumber
                Case Else
                    Return False
            End Select
        Next i
        Return True
    End Function

    ' Throws an exception if the specified name does not contain 
    ' all valid character types.
    Public Sub ValidateName(ByVal name As String) Implements INameCreationService.ValidateName
        Dim i As Integer
        For i = 0 To name.Length - 1
            Dim ch As Char = name.Chars(i)
            Dim uc As UnicodeCategory = [Char].GetUnicodeCategory(ch)
            Select Case uc
                Case UnicodeCategory.UppercaseLetter, UnicodeCategory.LowercaseLetter, UnicodeCategory.TitlecaseLetter, UnicodeCategory.DecimalDigitNumber
                Case Else
                    Throw New Exception("The name '" + name + "' is not a valid identifier.")
            End Select
        Next i
    End Sub

End Class

[C#] 
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.");                
                }
            }
        }
     }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::ComponentModel::Design;
using namespace System::ComponentModel::Design::Serialization;
using namespace System::Globalization;

namespace NameCreationServiceExample
{
public __gc class NameCreationService : public 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->Item[i]->Site->Name->StartsWith(String::Concat( baseName, __box(uniqueID))) )
                {
                    // If a match is encountered, set flag to recycle 
                    // collection, increment ID numeral, and restart.
                    unique = false;
                    uniqueID++;
                    break;
                }
            }
        }

        return String::Concat( baseName, __box(uniqueID));
    }

    // 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->Chars[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->Chars[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(String::Format( S"The name '{0}' is not a valid identifier.", name ));                
            }
        }
    }
};
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.ComponentModel.Design.Serialization

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System (System.dll 内)

参照

INameCreationService メンバ | System.ComponentModel.Design.Serialization 名前空間