![]() ![]() Private readonly JsonConverter _valueConverter Private class DictionaryEnumConverterInner : Typeof(DictionaryEnumConverterInner).MakeGenericType(īindingFlags.Instance | BindingFlags.Public, JsonConverter converter = (JsonConverter)Activator.CreateInstance( Type valueType = type.GetGenericArguments() ![]() Type keyType = type.GetGenericArguments() Public override JsonConverter CreateConverter( Return typeToConvert.GetGenericArguments().IsEnum If (typeToConvert.GetGenericTypeDefinition() != typeof(Dictionary)) Public override bool CanConvert(Type typeToConvert) Public class DictionaryTKeyEnumTValueConverter : JsonConverterFactory The inner converter gets an existing converter to handle whichever type is provided at run time for TValue. The CanConvert method returns true only for a Dictionary with two generic parameters, the first of which is an Enum type. The code follows the factory pattern because the first generic type parameter is Enum and the second is open. The following code shows a custom converter that works with Dictionary. "MM/dd/yyyy", CultureInfo.InvariantCulture)) Writer.WriteStringValue(dateTimeValue.ToString( "MM/dd/yyyy", CultureInfo.InvariantCulture) ![]() Public class DateTimeOffsetJsonConverter : JsonConverterĭateTimeOffset.ParseExact(reader.GetString()!, The converter uses mm/dd/yyyy format for DateTimeOffset properties. The following sample is a converter that overrides default serialization for an existing data type. The factory pattern creates a class that determines, at run time, which specific type is required and dynamically creates the appropriate converter. The basic pattern creates a class that can handle one type. Some examples of types that can be handled by the basic pattern include: For example, converters for the following types require the factory pattern: The basic pattern is for non-generic and closed generic types. The factory pattern is for converters that handle type Enum or open generics. There are two patterns for creating a custom converter: the basic pattern and the factory pattern. For more information, see Visual Basic support. Visual Basic can't be used to write custom converters but can call converters that are implemented in C# libraries.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |