Class ValueTypeConverter<T>
Abstract base-class for easily creating a TypeConverter for a struct (value type).
[RequiresUnreferencedCode("Uses reflection to discover properties.")]
public abstract class ValueTypeConverter<T> : TypeConverter where T : struct
Type Parameters
TThe struct to create the TypeConverter for.
- Inheritance
-
objectValueTypeConverter<T>
- Inherited Members
- Extension Methods
Examples
Add this attribute to the struct:
[TypeConverter(typeof(ClassDerivedFromThisOne))]
Remarks
Providing a TypeConverter for a struct improves the runtime experience with PropertyGrids.
Properties
NoArguments
The number of arguments the constructor of T has.
protected abstract int NoArguments { get; }
Property Value
Methods
CanConvertFrom(ITypeDescriptorContext?, Type)
Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context.
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
Parameters
contextITypeDescriptorContextAn ITypeDescriptorContext that provides a format context.
sourceTypeTypeA Type that represents the type you want to convert from.
Returns
CanConvertTo(ITypeDescriptorContext?, Type?)
Returns whether this converter can convert the object to the specified type, using the specified context.
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
Parameters
contextITypeDescriptorContextAn ITypeDescriptorContext that provides a format context.
destinationTypeTypeA Type that represents the type you want to convert to.
Returns
ConvertFrom(ITypeDescriptorContext?, CultureInfo?, object)
Converts the given object to the type of this converter, using the specified context and culture information.
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
Parameters
contextITypeDescriptorContextAn ITypeDescriptorContext that provides a format context.
cultureCultureInfoThe CultureInfo to use as the current culture.
valueobjectThe object to convert.
Returns
- object
An object that represents the converted value.
Exceptions
- NotSupportedException
The conversion cannot be performed.
ConvertTo(ITypeDescriptorContext?, CultureInfo?, object?, Type)
Converts the given value object to the specified type, using the specified context and culture information.
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
Parameters
contextITypeDescriptorContextAn ITypeDescriptorContext that provides a format context.
cultureCultureInfoA CultureInfo. If null is passed, the current culture is assumed.
valueobjectThe object to convert.
destinationTypeTypeThe Type to convert the
valueparameter to.
Returns
- object
An object that represents the converted value.
Exceptions
- ArgumentNullException
The
destinationTypeparameter is null.- NotSupportedException
The conversion cannot be performed.
CreateInstance(ITypeDescriptorContext?, IDictionary)
Creates an instance of the type that this TypeConverter is associated with, using the specified context, given a set of property values for the object.
public override object CreateInstance(ITypeDescriptorContext? context, IDictionary propertyValues)
Parameters
contextITypeDescriptorContextAn ITypeDescriptorContext that provides a format context.
propertyValuesIDictionaryAn IDictionary of new property values.
Returns
- object
An object representing the given IDictionary, or null if the object cannot be created. This method always returns null.
GetArguments(T)
protected abstract object[] GetArguments(T value)
Parameters
valueT
Returns
- object[]
The arguments for the constructor of
T.
GetConstructor()
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
protected abstract ConstructorInfo GetConstructor()
Returns
- ConstructorInfo
The constructor used to create new instances of
T(deserialization).
GetCreateInstanceSupported(ITypeDescriptorContext?)
Returns whether changing a value on this object requires a call to CreateInstance(IDictionary) to create a new value, using the specified context.
public override bool GetCreateInstanceSupported(ITypeDescriptorContext? context)
Parameters
contextITypeDescriptorContextAn ITypeDescriptorContext that provides a format context.
Returns
- bool
true if changing a property on this object requires a call to CreateInstance(IDictionary) to create a new value; otherwise, false.
GetElementSeparator(CultureInfo)
The separator to place between individual elements.
protected virtual string GetElementSeparator(CultureInfo culture)
Parameters
cultureCultureInfo
Returns
GetObject(IDictionary)
protected abstract T GetObject(IDictionary propertyValues)
Parameters
propertyValuesIDictionary
Returns
- T
A new instance of
T.
GetObject(string[], CultureInfo)
protected abstract T GetObject(string[] values, CultureInfo culture)
Parameters
valuesstring[]cultureCultureInfo
Returns
- T
A new instance of
T.
GetProperties(ITypeDescriptorContext?, object, Attribute[]?)
Returns a collection of properties for the type of array specified by the value parameter, using the specified context and attributes.
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext? context, object value, Attribute[]? attributes)
Parameters
contextITypeDescriptorContextAn ITypeDescriptorContext that provides a format context.
valueobjectAn object that specifies the type of array for which to get properties.
attributesAttribute[]An array of type Attribute that is used as a filter.
Returns
- PropertyDescriptorCollection
A PropertyDescriptorCollection with the properties that are exposed for this data type, or null if there are no properties.
GetPropertiesSupported(ITypeDescriptorContext?)
Returns whether this object supports properties, using the specified context.
public override bool GetPropertiesSupported(ITypeDescriptorContext? context)
Parameters
contextITypeDescriptorContextAn ITypeDescriptorContext that provides a format context.
Returns
- bool
true if GetProperties(object) should be called to find the properties of this object; otherwise, false.
GetValues(T, ITypeDescriptorContext?, CultureInfo)
protected abstract string[] GetValues(T value, ITypeDescriptorContext? context, CultureInfo culture)
Parameters
valueTcontextITypeDescriptorContextcultureCultureInfo
Returns
- string[]
The elements of
Tconverted to strings.