INameCreationService Interface
.NET Framework (current version)
Provides a service that can generate unique names for objects.
Assembly: System (in System.dll)
| Name | Description | |
|---|---|---|
![]() | CreateName(IContainer^, Type^) | Creates a new name that is unique to all components in the specified container. |
![]() | IsValidName(String^) | Gets a value indicating whether the specified name is valid. |
![]() | ValidateName(String^) | Gets a value indicating whether the specified name is valid. |
A DesignerLoader can implement this service to provide a way for a designer to create new, unique names for objects. If this service is not available, the designer uses a default implementation.
The following example code provides an example INameCreationService implementation. The service can create a unique name based on a type that does not match any names in the specified container. It can also validate a specified name string.
#using <System.dll> using namespace System; using namespace System::ComponentModel::Design; using namespace System::ComponentModel::Design::Serialization; using namespace System::Globalization; namespace NameCreationServiceExample { public ref 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. virtual 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( String::Concat( baseName, uniqueID ) ) ) { // If a match is encountered, set flag to recycle // collection, increment ID numeral, and restart. unique = false; uniqueID++; break; } } } return String::Concat( baseName, uniqueID ); } // Returns whether the specified name contains // all valid character types. virtual 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. virtual 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 gcnew Exception( String::Format( "The name '{0}' is not a valid identifier.", name ) ); } } } }; }
.NET Framework
Available since 1.1
Available since 1.1
Show:
