• Avalanche.Core
Search Results for

    Show / Hide Table of Contents
    • Avalanche.Accessor
      • Introduction
      • IAccessor
        • IAccessor
        • IListAccessor
        • IMapAccessor
        • IRecordAccessor
        • IContentAccessor
        • IOneOfAccessor
        • IAnyAccessor
      • .Net
        • Introduction
        • IList<T>
        • IDictionary<K,V>
        • FieldInfo
        • OneOfAttribute
        • StructLayoutAttribute
        • Class
      • Protobuf
        • Introduction
      • 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
        • IRealType
      • .Net
        • Introduction
        • IList<T>
        • IDictionary<K,V>
        • FieldInfo
        • Enum
        • OneOfAttribute
        • StructLayoutAttribute
        • Class
      • Protobuf
        • Introduction
      • Articles
        • DataTypeRequest
        • PrintTree
        • DataTypeMessages
    • Avalanche.Emit
      • Introduction
      • TypeBuilder
      • ConstructorBuilder
      • MethodBuilder
      • PropertyBuilder
      • FieldBuilder
      • Emit
      • Utilities
    • Avalanche.FileSystem
      • Introduction
      • Abstractions
        • IFileSystem
          • IFileSystemBrowse
          • IFileSystemCreateDirectory
          • IFileSystemDelete
          • IFileSystemFileAttribute
          • IFileSystemMount
          • IFileSystemMove
          • IFileSystemObserve
          • IFileSystemOpen
        • IEvent
        • IEntry
        • IOption
        • IToken
      • FileSystem
      • VirtualFileSystem
      • MemoryFileSystem
      • EmbeddedFileSystem
      • HttpFileSystem
      • Decoration
      • IFileProvider
      • Events
      • Utilities
        • Dispose
        • File Scanner
        • Visit Tree
        • File Operation
        • FilterEnumerable
        • PollingFilterWatchToken
    • 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
      • Articles
        • Alphabet localization
        • Benchmarks
        • Caching
        • Class Library
        • Demo
        • Diagnostics
        • Embedded resources
        • Emplacement
        • File localization
        • Text localization
        • Printing templates
    • Avalanche.Message
      • Introduction
      • IMessage
      • IMessageProvider
      • IMessageDescription
      • IMessageDescriptions
      • MessageLevel
      • Message printing
      • Messages and Exceptions
      • Microsoft.Extensions
        • DependencyInjection
      • Articles
        • Aggregate Messages
        • Localization
        • Logging
        • Validation
    • Avalanche.Options
      • Introduction
      • OptionsExtensions
      • OptionsMonitorCast
    • 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
        • Tuples
        • StructList
        • ArrayList
        • BijectionMap
        • LocakableDictionary
        • LockableList
        • MapList
        • Pipe
        • RingQueue
        • EnumerableExtensions
        • TupleUtilities
        • ArrayUtilities
      • 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
        • 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

    Dictionary Cloner

    DictionaryCloner<Dictionary, Key, Value> implements ICloner<Dictionary> cloner.

    // Create cloner
    ICloner<IDictionary<int, string>> cloner = new DictionaryCloner<int, string>().SetReadOnly();
    // Create dictionary
    IDictionary<int, string> dictionary = new Dictionary<int, string> { { 1, "A" }, { 2, "B" }, { 3, "C" } };
    // Create clone
    IDictionary<int, string> clone = cloner.Clone(dictionary);
    

    DictionaryCloner.Create(dictionaryType) also creates dictionary same cloner.

    // Create cloner
    ICloner<Dictionary<int, string>> cloner =
        (ICloner<Dictionary<int, string>>)
        DictionaryCloner.Create(typeof(Dictionary<int, string>)).SetReadOnly();
    

    DictionaryCloner<Interface, Key, Value, Implementation> implements ICloner<Interface> cloner.

    // Create cloner
    ICloner<IDictionary<int, string>> cloner =
        new DictionaryCloner<IDictionary<int, string>, int, string, LockableDictionary<int, string>>()
        .SetReadOnly();
    
    // Create dictionary
    Dictionary<int, string> dictionary = new Dictionary<int, string> { { 1, "A" }, { 2, "B" }, { 3, "C" } };
    
    // Create clone
    IDictionary<int, string> clone = cloner.Clone(dictionary);
    

    DictionaryCloner.Create(interfaceType, implementationType) also creates dictionary cloner.

    // Create cloner
    ICloner<IDictionary<int, string>> cloner =
        (ICloner<IDictionary<int, string>>)
        DictionaryCloner.Create(typeof(IDictionary<int, string>), typeof(LockableDictionary<int, string>))
        .SetReadOnly();
    

    ThrowOnNull

    .SetThrowOnNull(false) sets policy to pass null as is. This is default policy.

    // Create cloner
    ICloner<IDictionary<int, string>> cloner =
        (ICloner<IDictionary<int, string>>)
        DictionaryCloner.Create(typeof(IDictionary<int, string>))
        .SetThrowOnNull(false)
        .SetReadOnly();
    // Create clone (of null)
    IDictionary<int, string>? clone = cloner.Clone(null!); // -> null
    

    .SetThrowOnNull(true) sets policy to throw ArgumentNullException on null input value.

    try
    {
        // Create cloner
        ICloner<IDictionary<int, string>> cloner =
            (ICloner<IDictionary<int, string>>)
        DictionaryCloner.Create(typeof(IDictionary<int, string>))
            .SetThrowOnNull(true)
            .SetReadOnly();
        // Create clone (of null)
        IDictionary<int, string>? clone = cloner.Clone(null!); // -> null
    }
    catch (ArgumentNullException)
    {
    }
    

    Deep Cloner

    .SetKeyCloner(keyCloner) and .SetValueCloner(valueCloner) assign cloners that clones each element. If assigned the clone is deep, otherwise shallow.

    // Create record cloner
    ICloner<MyRecord> recordCloner = (ICloner<MyRecord>)RecordCloner.Create(typeof(MyRecord));
    // Create array cloner
    ICloner<MyRecord[]> cloner =
        (ICloner<MyRecord[]>)
        ArrayCloner.Create(typeof(MyRecord))
        .SetElementCloner(recordCloner)
        .SetReadOnly();
    // Create record
    MyRecord myRecord = new MyRecord();
    myRecord.IdMap[10] = 15;
    // Create array
    MyRecord[] array = new[] { myRecord };
    // Create array clone
    MyRecord[] clones = cloner.Clone(array);
    // Assert not same reference
    WriteLine(Object.ReferenceEquals(clones, array));       // False
    WriteLine(Object.ReferenceEquals(clones[0], array[0])); // False
    
    public record MyRecord
    {
        public IDictionary<int, int> IdMap = new Dictionary<int, int>();
    }
    

    Graph Cloner

    Use .SetCyclical(true) to indicate cyclical graph clone.

    // Create node cloner
    ICloner<Node> nodeCloner = (ICloner<Node>)RecordCloner.Create(typeof(Node)).SetCyclical(true);
    // Create array cloner
    ICloner<Node[]> graphCloner =
        (ICloner<Node[]>)
        ArrayCloner.Create(typeof(Node))
        .SetElementCloner(nodeCloner)
        .SetCyclical(true)
        .SetReadOnly();
    // Create graph
    Node node1 = new Node { Id = 1 };
    Node node2 = new Node { Id = 2 };
    Node node3 = new Node { Id = 3 };
    node1.Edges[2] = node2;
    node2.Edges[3] = node3;
    node3.Edges[1] = node1;
    Node[] graph = new[] { node1, node2, node3 };
    // Clone
    Node[] clone = graphCloner.Clone(graph);
    // Assert not same reference
    WriteLine(Object.ReferenceEquals(graph, clone));       // False
    WriteLine(Object.ReferenceEquals(graph[0], clone[0])); // False
    
    public class Node
    {
        public int Id;
        public IDictionary<int, Node> Edges = new Dictionary<int, Node>();
    }
    


    Cyclic query ⇒ Cyclic query

    Full Example

    Full example
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Avalanche.Utilities;
    using static System.Console;
    
    public class dictionarycloner
    {
        public static void Run()
        {
            // Dictionary Cloner
            {
                // <11>
                // Create cloner
                ICloner<IDictionary<int, string>> cloner = new DictionaryCloner<int, string>().SetReadOnly();
                // Create dictionary
                IDictionary<int, string> dictionary = new Dictionary<int, string> { { 1, "A" }, { 2, "B" }, { 3, "C" } };
                // Create clone
                IDictionary<int, string> clone = cloner.Clone(dictionary);
                // </11>
                // Print clone
                Print(clone);
            }
            {
                // <12>
                // Create cloner
                ICloner<Dictionary<int, string>> cloner =
                    (ICloner<Dictionary<int, string>>)
                    DictionaryCloner.Create(typeof(Dictionary<int, string>)).SetReadOnly();
                // </12>
                // Create dictionary
                Dictionary<int, string> dictionary = new Dictionary<int, string> { { 1, "A" }, { 2, "B" }, { 3, "C" } };
                // Create clone
                Dictionary<int, string> clone = cloner.Clone(dictionary);
                // Print clone
                Print(clone);
            }
            {
                // <13>
                // Create cloner
                ICloner<IDictionary<int, string>> cloner =
                    new DictionaryCloner<IDictionary<int, string>, int, string, LockableDictionary<int, string>>()
                    .SetReadOnly();
    
                // Create dictionary
                Dictionary<int, string> dictionary = new Dictionary<int, string> { { 1, "A" }, { 2, "B" }, { 3, "C" } };
    
                // Create clone
                IDictionary<int, string> clone = cloner.Clone(dictionary);
                // </13>
                // Print clone
                Print(clone);
            }
            {
                // <14>
                // Create cloner
                ICloner<IDictionary<int, string>> cloner =
                    (ICloner<IDictionary<int, string>>)
                    DictionaryCloner.Create(typeof(IDictionary<int, string>), typeof(LockableDictionary<int, string>))
                    .SetReadOnly();
                // </14>
                // Create dictionary
                Dictionary<int, string> dictionary = new Dictionary<int, string> { { 1, "A" }, { 2, "B" }, { 3, "C" } };
                // Create clone
                IDictionary<int, string> clone = cloner.Clone(dictionary);
                // Print clone
                Print(clone);
            }
    
    
            // ThrowOnNull
            {
                // <31>
                // Create cloner
                ICloner<IDictionary<int, string>> cloner =
                    (ICloner<IDictionary<int, string>>)
                    DictionaryCloner.Create(typeof(IDictionary<int, string>))
                    .SetThrowOnNull(false)
                    .SetReadOnly();
                // Create clone (of null)
                IDictionary<int, string>? clone = cloner.Clone(null!); // -> null
                // </31>
            }
            {
                // <32>
                try
                {
                    // Create cloner
                    ICloner<IDictionary<int, string>> cloner =
                        (ICloner<IDictionary<int, string>>)
                    DictionaryCloner.Create(typeof(IDictionary<int, string>))
                        .SetThrowOnNull(true)
                        .SetReadOnly();
                    // Create clone (of null)
                    IDictionary<int, string>? clone = cloner.Clone(null!); // -> null
                }
                catch (ArgumentNullException)
                {
                }
                // </32>
            }
    
            // Deep Cloner
            {
                // <41>
                // Create record cloner
                ICloner<MyRecord> recordCloner = (ICloner<MyRecord>)RecordCloner.Create(typeof(MyRecord));
                // Create array cloner
                ICloner<MyRecord[]> cloner =
                    (ICloner<MyRecord[]>)
                    ArrayCloner.Create(typeof(MyRecord))
                    .SetElementCloner(recordCloner)
                    .SetReadOnly();
                // Create record
                MyRecord myRecord = new MyRecord();
                myRecord.IdMap[10] = 15;
                // Create array
                MyRecord[] array = new[] { myRecord };
                // Create array clone
                MyRecord[] clones = cloner.Clone(array);
                // Assert not same reference
                WriteLine(Object.ReferenceEquals(clones, array));       // False
                WriteLine(Object.ReferenceEquals(clones[0], array[0])); // False
                // </41>
            }
    
            // Graph Cloner
            {
                // <51>
                // Create node cloner
                ICloner<Node> nodeCloner = (ICloner<Node>)RecordCloner.Create(typeof(Node)).SetCyclical(true);
                // Create array cloner
                ICloner<Node[]> graphCloner =
                    (ICloner<Node[]>)
                    ArrayCloner.Create(typeof(Node))
                    .SetElementCloner(nodeCloner)
                    .SetCyclical(true)
                    .SetReadOnly();
                // Create graph
                Node node1 = new Node { Id = 1 };
                Node node2 = new Node { Id = 2 };
                Node node3 = new Node { Id = 3 };
                node1.Edges[2] = node2;
                node2.Edges[3] = node3;
                node3.Edges[1] = node1;
                Node[] graph = new[] { node1, node2, node3 };
                // Clone
                Node[] clone = graphCloner.Clone(graph);
                // Assert not same reference
                WriteLine(Object.ReferenceEquals(graph, clone));       // False
                WriteLine(Object.ReferenceEquals(graph[0], clone[0])); // False
                // </51>
            }
    
        }
    
        static void Print<T>(IEnumerable<T> enumr) => Console.WriteLine(String.Join(", ", enumr));
        static void PrintArray<T>(IEnumerable<IEnumerable<T>> enumr) => Console.WriteLine($"[{String.Join("], [", enumr.Select(array => String.Join(", ", array)))}]");
    
        // <98>
        public record MyRecord
        {
            public IDictionary<int, int> IdMap = new Dictionary<int, int>();
        }
        // </98>
        // <99>
        public class Node
        {
            public int Id;
            public IDictionary<int, Node> Edges = new Dictionary<int, Node>();
        }
        // </99>
    }
    
    In This Article
    Back to top Copyright © Toni Kalajainen