• Avalanche.Core
Search Results for

    Show / Hide Table of Contents
    • Avalanche.Accessor
      • Introduction
      • IAccessor
        • IAccessor
        • IFieldAccessor
        • IListAccessor
        • IMapAccessor
        • IRecordAccessor
        • IOneOfAccessor
        • IAnyAccessor
      • Dto
        • Introduction
        • IList<T>
        • IDictionary<K,V>
        • FieldInfo
        • OneOfAttribute
        • StructLayoutAttribute
        • Class
      • Articles
        • Dependency Injection
        • AccessorMessages
    • Avalanche.Binding
      • Introduction
    • Avalanche.Configuration
      • Introduction
      • Configuration Binding
      • ConfigurationExtensions
      • MemoryConfiguration
      • PrintTree
      • Saving IOptions
      • Yaml
    • Avalanche.Converter
      • Introduction
      • EnumConverter
      • Func<,>
      • HexConverter
      • PrimitiveConverter
      • StringConverter
    • Avalanche.Core
      • License
    • Avalanche.DataType
      • Introduction
      • DataType
        • IDataType
        • IListType
        • IMapType
        • IRecordType
        • IFieldType
        • IOneOfType
        • IAnyType
        • IStringType
        • IValueType
        • IIntegerType
        • IEnumerationType
        • IFloatingPointType
      • Dto
        • DtoTarget
        • Class
        • Enum
        • FieldInfo
        • IList<T>
        • IDictionary<K,V>
        • OneOfAttribute
        • StructLayoutAttribute
        • IntermediateTarget
      • Articles
        • DataTypeRequest
        • PrintTree
        • DataTypeMessages
    • Avalanche.Emit
      • Introduction
      • TypeBuilder
      • ConstructorBuilder
      • MethodBuilder
      • PropertyBuilder
      • FieldBuilder
      • Emit
      • Utilities
        • CustomAttributeUtilities
        • ToStringRequest
    • Avalanche.FileSystem
      • Introduction
      • Abstractions
        • IFileSystemBrowse
        • IFileSystemCreateDirectory
        • IFileSystemDelete
        • IFileSystemFileAttribute
        • IFileSystemMount
        • IFileSystemMove
        • IFileSystemObserve
        • IFileSystemOpenStream
        • IFileSystemOpenMemory
        • IEvent
        • IEntry
        • IOption
        • IToken
      • FileSystem
      • VirtualFileSystem
      • MemoryFileSystem
      • EmbeddedFileSystem
      • HttpFileSystem
      • Decoration
      • IFileProvider
      • Utilities
        • Dispose
        • FileDictionary
        • FileScanner
        • FilterEnumerable
        • OperationSession
        • PollingFilterWatchToken
        • VisitTree
      • Article
        • Examples
        • PrintTree
    • Avalanche.Identity
      • Introduction
      • Identity
      • IdentityParts
      • IdentityInterner
      • IdentityComparer
      • Print Tree
      • IdentityAccessors
        • Introduction
        • TypeName
    • Avalanche.Localization
      • Introduction
      • Localization
      • LocalizationFile
      • LocalizationFiles
      • LocalizationFileSystem
      • LocalizationFileFormat
      • LocalizationLine
      • LocalizationLines
      • TemplateFormat
      • CultureProvider
      • FallbackCultureProvider
      • ResourceManager
      • LocalizationError
      • Microsoft.Extensions
        • Introduction
        • DependencyInjection
        • FileProvider
        • Logging
        • ITextLocalizer
        • IFileLocalizer
        • Localization
      • Asp.Net
        • Introduction
        • Supplying localization
        • Inject to pages
        • Culture Assigned
        • Minimalistic Api
        • Diagnostics
      • Pluralization
        • Introduction
        • Multiple plural parameters
        • Custom PluralRules
        • Invariant Culture
        • Unit Prefix
        • IPluralRule
        • IPluralNumber
        • IPluralRules
        • CLDRs
        • Unicode.CLDR40
        • Unicode.CLDR41
        • Unicode.CLDR42
        • Unicode.CLDR43
      • Articles
        • Alphabet localization
        • Benchmarks
        • Caching
        • Class Library
        • Demo
        • Diagnostics
        • Embedded resources
        • Emplacement
        • File localization
        • Text localization
        • Printing templates
    • Avalanche.Memory
      • Introduction
      • IMemory<T>
      • BlockMemory<T>
      • FileMemory
      • ListMemory<L, T>
      • MemoryMemory<T>
      • MMFMemory<T>
      • PointerMemory<T>
      • Slice<M, T>
      • StreamMemory
      • Concurrency
        • RWLockedMemory
        • SynchronizedMemory
      • Serialization
        • Benchmarks
        • FixedSize<T>
        • FixedSizeZigZag<T>
        • VariableSized<T>
        • VariableSizedZigZag<T>
        • StringEncoding
      • Articles
        • Fixed-size and Variable-sized
        • IBlockPool<T>
        • Print Tree
        • Read and Write events
        • Stream View
        • Zero Heap
    • Avalanche.Message
      • Introduction
      • IMessage
      • IMessageProvider
      • IMessageDescription
      • IMessageDescriptions
      • MessageDescriptionsBase
      • MessageLevel
      • Message printing
      • Messages and Exceptions
      • Microsoft.Extensions
        • DependencyInjection
      • Articles
        • Aggregate Messages
        • Localization
        • Localization Templates
        • Logging
        • Validation
    • Avalanche.Module
      • Introduction
      • IServiceCollection
      • IServiceBuilder
      • IHostBuilder
      • Dependencies
    • Avalanche.Options
      • Introduction
      • OptionsExtensions
      • OptionsMonitorCast
    • Avalanche.Protobuf
      • Introduction
    • Avalanche.Service
      • Introduction
      • Service
        • Introduction
        • IService
        • IServiceDisposable
        • IServiceDecoration
        • IServiceCast
        • IServiceObservable
        • IServiceContainer
        • Construction
        • Query
        • CancellationToken
        • CachePolicy
        • Scope
      • Handler
        • Introduction
        • IHandler
        • IHandlerCast
        • IHandlerDecoration
        • IHandlerWithOrder
        • CancellationToken
        • Cyclicity
        • Delegates
        • Invokable
        • ExportAttribute
        • OrderAttribute
        • PrintTree
        • Recursion
      • Query
        • Introduction
        • IQuery
        • IQueryCast
        • IQueryDecoration
      • Entry
        • Introduction
        • IEntry
        • IEntryCast
        • IEntryDecoration
        • IEntryObservable
        • IEntryVisitable
        • EntryState
      • Request
        • Introduction
        • IRequest
        • IRequestFor
        • IRequestToBeCached
        • IRequestToBeDisposed
        • RequestAttribute
        • ContextParameterAttribute
        • Print Tree
      • Dependency Injection
        • Introduction
        • Asp.Net
        • ServiceRequest<T>
        • Decorating a service
        • Handler
        • CachePolicy
        • CancellationToken
        • QueryLogger
        • IHostBuilder
      • Examples
        • NodeCount
        • Expression
        • Mapper
      • Articles
        • Benchmarks
        • Error Handling
        • ServiceMessages
    • Avalanche.StatusCode
      • Introduction
      • HResult
        • Introduction
        • HResult.Facilities
        • BasicMessages
        • RpcMessages
        • DispatchMessages
        • StorageMessages
        • ItfMessages
        • Win32Messages
        • WindowsMessages
        • SspiMessages
        • CertMessages
        • MediaServerMessages
        • SetupApiMessages
        • ScardMessages
        • ComPlusMessages
        • ClrMessages
        • UserModeFilterManagerMessages
        • GraphicsMessages
        • TpmServicesMessages
        • TpmSoftwareMessages
        • PlaMessages
        • FveMessages
        • FwpMessages
        • NdisMessages
        • DltMessages
      • System
        • Introduction
        • AccessControlMessages
        • AggregateMessages
        • AppDomainMessages
        • ArgumentMessages
        • ArgumentNullMessages
        • ArgumentOutOfRangeMessages
        • ArithmeticMessages
        • ArrayMessages
        • AssemblyMessages
        • BadImageFormatMessages
        • CodeContractMessages
        • CodePageMessages
        • CollectionsMessages
        • CompilerServiceMessages
        • CryptographyMessages
        • CultureMessages
        • DiagnosticsMessages
        • EventSourceMessages
        • ExecutionEngineMessages
        • FormatMessages
        • HostProtectionMessages
        • IOMessages
        • IndexOutOfRangeMessages
        • InteropServiceMessages
        • InvalidCastMessages
        • InvalidOperationMessages
        • IsolatedStorageMessages
        • LazyMessages
        • MarshalerMessages
        • MemoryMessages
        • MiscellaneousMessages
        • NotImplementedMessages
        • NotSupportedMessages
        • ObjectDisposedMessages
        • OperationCanceledMessages
        • OverflowMessages
        • PlatformMessages
        • PolicyMessages
        • PrincipalMessages
        • ProgramMessages
        • ReferenceMessages
        • ReflectionMessages
        • RegionMessages
        • RemotingMessages
        • ResourcesMessages
        • SecurityMessages
        • SerializationMessages
        • StackMessages
        • TaskMessages
        • TextMessages
        • ThreadingMessages
        • TimeZoneMessages
        • TypeMessages
        • XmlMessages
      • HttpStatusCode
      • OpcUaStatusCode
    • Avalanche.Template
      • Introduction
      • TemplateFormats
      • ITemplatePrintable
      • ITemplateFormatPrintable
      • ITemplateText
      • ITemplateBreakdown
      • ITemplateFormat
      • ITemplateFormats
      • Extract Arguments
      • Emplacement
    • Avalanche.Tokenizer
      • Introduction
      • IToken
      • ITokenizer
      • Tokenizers
    • Avalanche.Utilities
      • Introduction
      • Collections
        • ArrayList
        • ArrayUtilities
        • BijectionMap
        • CachedSelect
        • DictionaryAdapter
        • EnumerableExtensions
        • LocakableDictionary
        • LockableList
        • MapList
        • Pipe
        • RingQueue
        • StructList
        • Tuples
        • TupleUtilities
      • Comparers
        • IGraphComparer
        • IGraphComparable
        • AlphaNumericComparer
        • EnumerableComparer
        • EnumerableGraphComparer
        • ReferenceComparer
        • KeyValuePairComparer
        • DefaultComparerProvider
        • RecordComparer
      • Cloners
        • ICloner
        • IGraphCloner
        • IGraphCloneable
        • ListCloner
        • DictionaryCloner
        • FieldCloner
        • PassthroughCloner
        • RecordCloner
        • ClonerProvider
      • Dispose
        • IDisposeAttachable
        • IDisposeBelatable
      • Provider
        • Introduction
        • ProviderBase
        • Delegate
        • Pipe
        • Cache
        • ResultCapture
        • AsReadOnly
        • SharedResource
        • AsService
        • IProviderEvent
      • Record
        • IRecordDescription
        • IFieldDescription
        • IConstructorDescription
        • IConstructionDescription
        • IParameterDescription
        • IRecordProviders
        • RecordDelegates
          • RecordCreate
          • RecordClone
          • RecordCopy
          • IRecordDelegates
        • FieldDelegates
          • FieldRead
          • FieldWrite
          • RecreateWith
          • IFieldDelegates
      • Reflection
        • EnumDescription
      • String
        • IEscaper
        • UnicodeString
        • Hex
      • Miscellaneous
        • IIdGenerator
        • Permutation
        • IReadOnly
        • IUserDataContainer
        • ITreeNode
        • Void
    • Avalanche.Writer
      • Introduction
      • ConstantWriter
      • Context
      • ConvertWriter
      • DefaultConstructor
      • DelegateWriter
      • PassthroughWriter
      • Referer
      • TypeCast
      • Writer
      • WriterPipe
      • WriterMessages

    IdentityComparer

    IdentityComparer implements IEqualityComparer<IIdentity>.

    IEqualityComparer<IIdentity> equalityComparer = IdentityComparer.Default;
    

    It compares following properties: Part, Parent, Components, Label. Uses label accessor comparer if available.

    string typename = typeof(string).AssemblyQualifiedName;
    // Parse
    IIdentity id2 = IdentityAccessors.Default.TypeName.MemoryAccessors.Parse(typename.AsMemory());
    // Create
    IIdentity id1 = IdentityAccessors.Default.TypeName.Create(typeof(string));
    // 'True'
    WriteLine(equalityComparer.Equals(id1, id2));
    

    Two equal identities produce same hashcode.

    // '495866146'
    WriteLine(equalityComparer.GetHashCode(id1));
    // '495866146'
    WriteLine(equalityComparer.GetHashCode(id2));
    

    IdentityComparer implements also IComparer<IIdentity>.

    // Order comparer
    IComparer<IIdentity> comparer = IdentityComparer.Default;
    // Array of identities
    IIdentity[] array = new[]
    {
        Identities.CreateNamespace("System").AppendName("String"),
        Identities.CreateNamespace("System").AppendName("Int"),
        Identities.CreateNamespace("System"),
        Identities.CreateNamespace("Avalanche").AppendNamespace("Identity").AppendName("IIdentity<T>"),
        Identities.CreateNamespace("Avalanche").AppendNamespace("Identity").AppendName("Identity<T>"),
        Identities.CreateNamespace("Avalanche").AppendNamespace("Service").AppendName("IService"),
        Identities.CreateNamespace("Avalanche"),
    };
    // Sort array
    Array.Sort(array, comparer);
    // Print array
    foreach (IIdentity id in array)
        WriteLine(IdentityAccessors.Default.TypeName.MemoryAccessors.Printing.Print(id));
    
    Avalanche
    Avalanche.Identity.Identity
    Avalanche.Identity.IIdentity
    Avalanche.Service.IService
    System
    System.Int
    System.String
    

    Case Insensitive

    IdentityComparer.CaseInsensitive overrides label accessor string comparers with StringComparer.OrdinalIgnoreCase.

    // Order comparer
    IdentityComparer comparer = IdentityComparer.CaseInsensitive;
    //
    IIdentity id1 = Identities.CreateNamespace("sYsTeM").AppendName("StRiNg");
    IIdentity id2 = Identities.CreateNamespace("SYSTEM").AppendName("string");
    // '-1907792046'
    WriteLine(comparer.GetHashCode(id1));
    WriteLine(comparer.GetHashCode(id2));
    // 'True'
    WriteLine(comparer.Equals(id1, id2));
    // 'False'
    WriteLine(id1.Equals(id2));
    

    CaseInsensitive comparer also sorts insensitive.

    // Order comparer
    IComparer<IIdentity> comparer = IdentityComparer.CaseInsensitive;
    // Array of identities
    IIdentity[] array = new[]
    {
        Identities.CreateNamespace("system").AppendName("String"),
        Identities.CreateNamespace("SysTem").AppendName("Int"),
        Identities.CreateNamespace("system"),
        Identities.CreateNamespace("AvalaNche").AppendNamespace("Identity").AppendName("IIdentity<T>"),
        Identities.CreateNamespace("avalancHe").AppendNamespace("Identity").AppendName("Identity<T>"),
        Identities.CreateNamespace("AvalanchE").AppendNamespace("Service").AppendName("IService"),
        Identities.CreateNamespace("Avalanche"),
    };
    // Sort array
    Array.Sort(array, comparer);
    // Print array
    foreach (IIdentity id in array)
        WriteLine(IdentityAccessors.Default.TypeName.MemoryAccessors.Print(id));
    
    Avalanche
    avalancHe.Identity.Identity
    AvalaNche.Identity.IIdentity
    AvalanchE.Service.IService
    system
    SysTem.Int
    system.String
    

    Other Special Cases

    Comparer can be constructed with argument 'partsToDisregard:' that lists parts that are not to be compared.

    // "System.String, Library, Version=1.2.3, Culture=neutral, PublicKeyToken=0102030405060708"
    IIdentity typeid1 =
        IdentityBase
        .Create<byte[]>(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, IdentityParts.PublicKeyToken, "Hex")
        .Append<string>("neutral", IdentityParts.Culture, LabelAccessors.Default.String)
        .Append<string>("Library", IdentityParts.Library, LabelAccessors.Default.String)
        .Append<string>("1.2.3", IdentityParts.Version, LabelAccessors.Default.String)
        .AppendNamespace("System")
        .AppendName("String");
    // "System.String, Library, Version=2.3.4, Culture=en, PublicKeyToken=0807060504030201"
    IIdentity typeid2 =
        IdentityBase.Create<byte[]>(new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }, IdentityParts.PublicKeyToken, "Hex")
        .Append<string>("en", IdentityParts.Culture, LabelAccessors.Default.String)
        .Append<string>("Library", IdentityParts.Library, LabelAccessors.Default.String)
        .Append<string>("2.3.4", IdentityParts.Version, LabelAccessors.Default.String)
        .AppendNamespace("System")
        .AppendName("String");
    // Compare with regular comparer: "False"
    WriteLine(IdentityComparer.Default.Equals(typeid1, typeid2));
    // Create comparer that ignores: none, version, culture, and token
    IdentityComparer comparer = new IdentityComparer(partsToDisregard: new HashSet<Label8> { "", "Version", "Culture", "PuKeyTkn" });
    // Compare now: "True"
    WriteLine(comparer.Equals(typeid1, typeid2));
    

    Comparer can also be constructed with argument 'partsToRegard:' that is a list of parts only to compare.

    // Create comparer that compares only: name, namespace, library
    IdentityComparer comparer2 = new IdentityComparer(partsToRegard: new HashSet<Label8> { "Name", "Namespce", "Library" });
    // Compare now: "True"
    WriteLine(comparer2.Equals(typeid1, typeid2));
    

    Full Example

    Full example
    using System;
    using System.Collections.Generic;
    using Avalanche.Accessor;
    using Avalanche.Identity;
    using Avalanche.Serialization;
    using Avalanche.Utilities;
    using static System.Console;
    
    class identitycomparer
    {
        public static void Run()
        {
            {
                // <01>
                IEqualityComparer<IIdentity> equalityComparer = IdentityComparer.Default;
                // </01>
    
                // <02>
                string typename = typeof(string).AssemblyQualifiedName;
                // Parse
                IIdentity id2 = IdentityAccessors.Default.TypeName.MemoryAccessors.Parse(typename.AsMemory());
                // Create
                IIdentity id1 = IdentityAccessors.Default.TypeName.Create(typeof(string));
                // 'True'
                WriteLine(equalityComparer.Equals(id1, id2));
                // </02>
    
                // <03>
                // '495866146'
                WriteLine(equalityComparer.GetHashCode(id1));
                // '495866146'
                WriteLine(equalityComparer.GetHashCode(id2));
                // </03>
            }
            {
                // <11>
                // Order comparer
                IComparer<IIdentity> comparer = IdentityComparer.Default;
                // Array of identities
                IIdentity[] array = new[]
                {
                    Identities.CreateNamespace("System").AppendName("String"),
                    Identities.CreateNamespace("System").AppendName("Int"),
                    Identities.CreateNamespace("System"),
                    Identities.CreateNamespace("Avalanche").AppendNamespace("Identity").AppendName("IIdentity<T>"),
                    Identities.CreateNamespace("Avalanche").AppendNamespace("Identity").AppendName("Identity<T>"),
                    Identities.CreateNamespace("Avalanche").AppendNamespace("Service").AppendName("IService"),
                    Identities.CreateNamespace("Avalanche"),
                };
                // Sort array
                Array.Sort(array, comparer);
                // Print array
                foreach (IIdentity id in array)
                    WriteLine(IdentityAccessors.Default.TypeName.MemoryAccessors.Printing.Print(id));
                // </11>
            }
            WriteLine();
            {
                // <12>
                // Order comparer
                IdentityComparer comparer = IdentityComparer.CaseInsensitive;
                //
                IIdentity id1 = Identities.CreateNamespace("sYsTeM").AppendName("StRiNg");
                IIdentity id2 = Identities.CreateNamespace("SYSTEM").AppendName("string");
                // '-1907792046'
                WriteLine(comparer.GetHashCode(id1));
                WriteLine(comparer.GetHashCode(id2));
                // 'True'
                WriteLine(comparer.Equals(id1, id2));
                // 'False'
                WriteLine(id1.Equals(id2));
                // </12>
            }
            WriteLine();
            {
                // <13>
                // Order comparer
                IComparer<IIdentity> comparer = IdentityComparer.CaseInsensitive;
                // Array of identities
                IIdentity[] array = new[]
                {
                    Identities.CreateNamespace("system").AppendName("String"),
                    Identities.CreateNamespace("SysTem").AppendName("Int"),
                    Identities.CreateNamespace("system"),
                    Identities.CreateNamespace("AvalaNche").AppendNamespace("Identity").AppendName("IIdentity<T>"),
                    Identities.CreateNamespace("avalancHe").AppendNamespace("Identity").AppendName("Identity<T>"),
                    Identities.CreateNamespace("AvalanchE").AppendNamespace("Service").AppendName("IService"),
                    Identities.CreateNamespace("Avalanche"),
                };
                // Sort array
                Array.Sort(array, comparer);
                // Print array
                foreach (IIdentity id in array)
                    WriteLine(IdentityAccessors.Default.TypeName.MemoryAccessors.Print(id));
                // </13>
            }
    
            {
                // Create
                // <20>
                // "System.String, Library, Version=1.2.3, Culture=neutral, PublicKeyToken=0102030405060708"
                IIdentity typeid1 =
                    IdentityBase
                    .Create<byte[]>(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, IdentityParts.PublicKeyToken, "Hex")
                    .Append<string>("neutral", IdentityParts.Culture, LabelAccessors.Default.String)
                    .Append<string>("Library", IdentityParts.Library, LabelAccessors.Default.String)
                    .Append<string>("1.2.3", IdentityParts.Version, LabelAccessors.Default.String)
                    .AppendNamespace("System")
                    .AppendName("String");
                // "System.String, Library, Version=2.3.4, Culture=en, PublicKeyToken=0807060504030201"
                IIdentity typeid2 =
                    IdentityBase.Create<byte[]>(new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }, IdentityParts.PublicKeyToken, "Hex")
                    .Append<string>("en", IdentityParts.Culture, LabelAccessors.Default.String)
                    .Append<string>("Library", IdentityParts.Library, LabelAccessors.Default.String)
                    .Append<string>("2.3.4", IdentityParts.Version, LabelAccessors.Default.String)
                    .AppendNamespace("System")
                    .AppendName("String");
                // Compare with regular comparer: "False"
                WriteLine(IdentityComparer.Default.Equals(typeid1, typeid2));
                // Create comparer that ignores: none, version, culture, and token
                IdentityComparer comparer = new IdentityComparer(partsToDisregard: new HashSet<Label8> { "", "Version", "Culture", "PuKeyTkn" });
                // Compare now: "True"
                WriteLine(comparer.Equals(typeid1, typeid2));
                // </20>            
                // <21>
                // Create comparer that compares only: name, namespace, library
                IdentityComparer comparer2 = new IdentityComparer(partsToRegard: new HashSet<Label8> { "Name", "Namespce", "Library" });
                // Compare now: "True"
                WriteLine(comparer2.Equals(typeid1, typeid2));
                // </21>
            }
    
        }
    }
    
    In This Article
    Back to top