S
Steve Richter
in a generic class, can I code the class so that I can call a static
method of the generic class T?
In the ConvertFrom method of the generic TypeConvert class I want to
write, I have a call to the static Parse method of the conversion
class.
if (InValue is string)
return T.Parse((string)InValue);
else
return base.ConvertFrom(context, culture, InValue);
the compiler gives an error message saying "T is a type parameter
which cannot be used in the given context. Can I call statics of the
generic class?
thanks,
-Steve
public class GenericTypeConverter<T> : TypeConverter where T:class
{
// this TypeConverter can convert from string.
public override bool CanConvertFrom(
ITypeDescriptorContext context,
Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
// convert from string to T
public override object ConvertFrom(
ITypeDescriptorContext context,
CultureInfo culture,
object InValue)
{
if (InValue is string)
return T.Parse((string)InValue);
else
return base.ConvertFrom(context, culture, InValue);
}
// Convert from T to string.
public override object ConvertTo(
ITypeDescriptorContext context,
CultureInfo culture,
object InValue,
Type destinationType)
{
if (destinationType == typeof(string))
{
T vlus = InValue as T ;
return (vlus.ToString());
}
else
return base.ConvertTo(context, culture, InValue,
destinationType);
}
}
method of the generic class T?
In the ConvertFrom method of the generic TypeConvert class I want to
write, I have a call to the static Parse method of the conversion
class.
if (InValue is string)
return T.Parse((string)InValue);
else
return base.ConvertFrom(context, culture, InValue);
the compiler gives an error message saying "T is a type parameter
which cannot be used in the given context. Can I call statics of the
generic class?
thanks,
-Steve
public class GenericTypeConverter<T> : TypeConverter where T:class
{
// this TypeConverter can convert from string.
public override bool CanConvertFrom(
ITypeDescriptorContext context,
Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
// convert from string to T
public override object ConvertFrom(
ITypeDescriptorContext context,
CultureInfo culture,
object InValue)
{
if (InValue is string)
return T.Parse((string)InValue);
else
return base.ConvertFrom(context, culture, InValue);
}
// Convert from T to string.
public override object ConvertTo(
ITypeDescriptorContext context,
CultureInfo culture,
object InValue,
Type destinationType)
{
if (destinationType == typeof(string))
{
T vlus = InValue as T ;
return (vlus.ToString());
}
else
return base.ConvertTo(context, culture, InValue,
destinationType);
}
}