• 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.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
    • 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.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
      • Examples
        • NodeCount
        • Expression
        • Mapper
      • Articles
        • Benchmarks
        • Error Handling
        • ServiceMessages
    • Avalanche.StatusCode
      • Introduction
      • HResult
        • Introduction
        • HResult.Facilities
        • BasicMessages
        • RpcMessages
        • DispatchMessages
        • ItfMessages
        • Win32Messages
        • ClrMessages
      • 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
      • String
        • IEscaper
        • UnicodeString
      • Miscellaneous
        • IIdGenerator
        • Permutation
        • IReadOnly
        • IUserDataContainer
        • Void
    • Avalanche.Writer
      • Introduction
      • ConstantWriter
      • Context
      • ConvertWriter
      • DefaultConstructor
      • DelegateWriter
      • PassthroughWriter
      • Referer
      • TypeCast
      • Writer
      • WriterPipe
      • WriterMessages

    TypeCast

    .TypeCast<From, To>() and .TypeCast(fromType, toType) type-casts writer 'From' and 'To' types with implicit type-casting.

    If value types are casted into object, then caster refers inside box.

    // Create reader
    IWriterFromRefToRef<MyStruct, string> labelReader = DelegateWriter.Reader((ref MyStruct myStruct) => myStruct.Label);
    // Create writer
    IWriterFromRef<string, MyStruct> labelWriter = DelegateWriter.Writer((string label, ref MyStruct myStruct) => myStruct.Label = label);
    
    // Convert to box reader
    IWriterToRef<object, string> boxLabelReader =
        (IWriterToRef<object, string>)
        labelReader.TypeCast<object, string>();
    // Convert to box writer
    IWriter<string, object> boxLabelWriter =
        (IWriter<string, object>)
        labelWriter.TypeCast(typeof(string), typeof(object));
    
    // Create struct
    MyStruct myStruct = new MyStruct("Label");
    // Copy to heap
    object box = myStruct;
    
    // Write to box
    boxLabelWriter.Write("NewLabel", box);
    // Read from box
    string label = boxLabelReader.Read(box);
    

    Cast

    .CastFrom<NewFrom>(casterOldFromToNewFrom), .CastFrom(newFromType, casterOldFromToNewFrom) casts 'From' type by forwarding casting to sub-writer.

    // Create Int32 parser
    IWriterToRef<string, int> int32parser = DelegateWriter.Reader((string str) => int.Parse(str));
    // Create Int32 printer
    IWriterToRef<int, string> int32printer = DelegateWriter.Reader((int value) => value.ToString());
    
    // Create writer
    IWriterFromRef<string, MyStruct> labelWriter = DelegateWriter.Writer((string label, ref MyStruct myStruct) => myStruct.Label = label);
    
    // Cast to int32 writer of Label:string on MyStruct
    IWriterFromRefToRef<int, MyStruct> labelIntWriter =
        (IWriterFromRefToRef<int, MyStruct>)
        labelWriter.CastFrom(typeof(int), int32printer, int32parser);
    
    // Create struct
    MyStruct myStruct = new MyStruct("100");
    // Write label with integer
    labelIntWriter.Write(200, ref myStruct);
    

    .CastTo<NewTo>(casterOldToToNewTo, casterNewToToOldTo?), .CastTo(newToType, casterOldToToNewTo, casterNewToToOldTo?) casts 'To' type.

    // Create Int32 parser
    IWriterToRef<string, int> int32parser = DelegateWriter.Reader((string str) => int.Parse(str));
    // Create Int32 printer
    IWriterToRef<int, string> int32printer = DelegateWriter.Reader((int value) => value.ToString());
    
    // Create reader
    IWriterFromRefToRef<MyStruct, string> labelReader = DelegateWriter.Reader((ref MyStruct myStruct) => myStruct.Label);
    
    // Cast to int32 reader of Label:string on MyStruct
    IWriterFromRefToRef<MyStruct, int> labelIntReader =
        (IWriterFromRefToRef<MyStruct, int>)
        labelReader.CastTo(typeof(int), int32parser, int32printer);
    
    // Create struct
    MyStruct myStruct = new MyStruct("100");
    // Read label as integer
    int label = labelIntReader.Read(ref myStruct);
    

    .CastFromAndTo<NewFrom, NewTo>() type-casts writer 'From' and 'To' types by forwarding casting to a sub-writers.

    // Create Int32 parser
    IWriterToRef<string, int> int32parser = DelegateWriter.Reader((string str) => int.Parse(str));
    // Create Int32 printer
    IWriterToRef<int, string> int32printer = DelegateWriter.Reader((int value) => value.ToString());
    // Create Boxer
    IWriterToRef<object, MyStruct> myStructUnboxer = (IWriterToRef<object, MyStruct>)TypeCastT.Create<object, MyStruct>();
    // Create Unboxer
    IWriterToRef<MyStruct, object> myStructBoxer = (IWriterToRef<MyStruct, object>)TypeCastT.Create<MyStruct, object>();
    
    // Create writer
    IWriterFromRef<string, MyStruct> labelWriter = DelegateWriter.Writer((string label, ref MyStruct myStruct) => myStruct.Label = label);
    
    // 
    IWriterToRef<int, object> boxLabelIntWriter =
        (IWriterToRef<int, object>)
        labelWriter.CastFromAndTo(
            newFrom: typeof(int),
            newTo: typeof(object),
            casterNewFromToOldFrom: int32printer,
            casterOldToToNewTo: myStructBoxer,
            casterNewToToOldTo: myStructUnboxer,
            casterOldFromToNewFrom: int32parser);
    
    // Create struct
    MyStruct myStruct = new MyStruct("100");
    // Box it
    object box = myStruct;
    // Writer label as integer
    boxLabelIntWriter.Write(25, ref box);
    

    Full Example

    Full example
    using Avalanche.Service;
    using Avalanche.Writer;
    
    public class typecast
    {
        public static void Run()
        {
            {
                // <01>
                // Create reader
                IWriterFromRefToRef<MyStruct, string> labelReader = DelegateWriter.Reader((ref MyStruct myStruct) => myStruct.Label);
                // Create writer
                IWriterFromRef<string, MyStruct> labelWriter = DelegateWriter.Writer((string label, ref MyStruct myStruct) => myStruct.Label = label);
    
                // Convert to box reader
                IWriterToRef<object, string> boxLabelReader =
                    (IWriterToRef<object, string>)
                    labelReader.TypeCast<object, string>();
                // Convert to box writer
                IWriter<string, object> boxLabelWriter =
                    (IWriter<string, object>)
                    labelWriter.TypeCast(typeof(string), typeof(object));
    
                // Create struct
                MyStruct myStruct = new MyStruct("Label");
                // Copy to heap
                object box = myStruct;
    
                // Write to box
                boxLabelWriter.Write("NewLabel", box);
                // Read from box
                string label = boxLabelReader.Read(box);
                // </01>
            }
    
            {
                // <11>
                // Create Int32 parser
                IWriterToRef<string, int> int32parser = DelegateWriter.Reader((string str) => int.Parse(str));
                // Create Int32 printer
                IWriterToRef<int, string> int32printer = DelegateWriter.Reader((int value) => value.ToString());
    
                // Create writer
                IWriterFromRef<string, MyStruct> labelWriter = DelegateWriter.Writer((string label, ref MyStruct myStruct) => myStruct.Label = label);
    
                // Cast to int32 writer of Label:string on MyStruct
                IWriterFromRefToRef<int, MyStruct> labelIntWriter =
                    (IWriterFromRefToRef<int, MyStruct>)
                    labelWriter.CastFrom(typeof(int), int32printer, int32parser);
    
                // Create struct
                MyStruct myStruct = new MyStruct("100");
                // Write label with integer
                labelIntWriter.Write(200, ref myStruct);
                // </11>
            }
            {
                // <12>
                // Create Int32 parser
                IWriterToRef<string, int> int32parser = DelegateWriter.Reader((string str) => int.Parse(str));
                // Create Int32 printer
                IWriterToRef<int, string> int32printer = DelegateWriter.Reader((int value) => value.ToString());
    
                // Create reader
                IWriterFromRefToRef<MyStruct, string> labelReader = DelegateWriter.Reader((ref MyStruct myStruct) => myStruct.Label);
    
                // Cast to int32 reader of Label:string on MyStruct
                IWriterFromRefToRef<MyStruct, int> labelIntReader =
                    (IWriterFromRefToRef<MyStruct, int>)
                    labelReader.CastTo(typeof(int), int32parser, int32printer);
    
                // Create struct
                MyStruct myStruct = new MyStruct("100");
                // Read label as integer
                int label = labelIntReader.Read(ref myStruct);
                // </12>
            }
            {
                // <13>
                // Create Int32 parser
                IWriterToRef<string, int> int32parser = DelegateWriter.Reader((string str) => int.Parse(str));
                // Create Int32 printer
                IWriterToRef<int, string> int32printer = DelegateWriter.Reader((int value) => value.ToString());
                // Create Boxer
                IWriterToRef<object, MyStruct> myStructUnboxer = (IWriterToRef<object, MyStruct>)TypeCastT.Create<object, MyStruct>();
                // Create Unboxer
                IWriterToRef<MyStruct, object> myStructBoxer = (IWriterToRef<MyStruct, object>)TypeCastT.Create<MyStruct, object>();
    
                // Create writer
                IWriterFromRef<string, MyStruct> labelWriter = DelegateWriter.Writer((string label, ref MyStruct myStruct) => myStruct.Label = label);
    
                // 
                IWriterToRef<int, object> boxLabelIntWriter =
                    (IWriterToRef<int, object>)
                    labelWriter.CastFromAndTo(
                        newFrom: typeof(int),
                        newTo: typeof(object),
                        casterNewFromToOldFrom: int32printer,
                        casterOldToToNewTo: myStructBoxer,
                        casterNewToToOldTo: myStructUnboxer,
                        casterOldFromToNewFrom: int32parser);
    
                // Create struct
                MyStruct myStruct = new MyStruct("100");
                // Box it
                object box = myStruct;
                // Writer label as integer
                boxLabelIntWriter.Write(25, ref box);
                // </13>
            }
        }
    
        public record struct MyStruct(string Label);
    }
    
    In This Article
    Back to top Copyright © Toni Kalajainen