[Mono-list] typeof(string)

Gaurav Vaish gvaish@iitk.ac.in
Fri, 15 Mar 2002 22:03:19 +0530


This is a multi-part message in MIME format.

------=_NextPart_000_000B_01C1CC6D.37224960
Content-Type: text/plain;
	charset="Windows-1252"
Content-Transfer-Encoding: 7bit

Hello,
    I have a method with following definition:
        public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)

    I want to check whether destinationType is String::GetType(), how do I do
that?

    I tried checking TypeConverter.cs, where it is supposed to be initially used
(wrt to the class quoted here), but the class is unimplemented as yet.

    (destinationType is string) is totally invalid.

    (destinationType is typeof(string)) gives the following errors:

d:\Temp\mcs\class\System.Web\System.Web.UI.WebControls\FontNamesConverter.cs(54,
27): error CS1031: Type expected
d:\Temp\mcs\class\System.Web\System.Web.UI.WebControls\FontNamesConverter.cs(54,
41): error CS1002: ; expected
d:\Temp\mcs\class\System.Web\System.Web.UI.WebControls\FontNamesConverter.cs(54,
41): error CS1525: Invalid expression term ')'
d:\Temp\mcs\class\System.Web\System.Web.UI.WebControls\FontNamesConverter.cs(54,
42): error CS1002: ; expected


Attaching file for inspection. Useful code is:

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo
culture, object value, Type destinationType)
{
    if(destinationType is typeof(string))
    {
        if(value == null || ((string[])value) == null)
        return String.Empty;
        return String.Join(",", (string[])value);
    }
    throw GetConvertToException(value, destinationType);
}


Cheers,
Gaurav Vaish
http://home.iitk.ac.in/student/gvaish
http://calendar.yahoo.com/mastergaurav
---------------------------------

------=_NextPart_000_000B_01C1CC6D.37224960
Content-Type: application/octet-stream;
	name="FontNamesConverter.cs"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="FontNamesConverter.cs"

/**
 * Namespace: System.Web.UI.WebControls
 * Class:     FontNamesConverter
 *
 * Author:  Gaurav Vaish
 * Maintainer: gvaish@iitk.ac.in
 * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
 * Implementation: yes
 * Status:  100%
 *
 * (C) Gaurav Vaish (2002)
 */

using System;
using System.Globalization;
using System.ComponentModel;
using System.Web;
using System.Web.UI;

namespace System.Web.UI.WebControls
{
	public class FontNamesConverter : TypeConverter
	{
		public FontNamesConverter(): base()
		{
		}

		 public override bool CanConvertFrom(ITypeDescriptorContext context, =
Type sourceType)
		 {
		 	return (sourceType =3D=3D typeof(string));
		 }

		 public override object ConvertFrom(ITypeDescriptorContext context, =
CultureInfo culture, object value)
		 {
		 	if(value is string)
		 	{
		 		string fontNames =3D (string)value;
		 		if(fontNames.Length =3D=3D 0)
		 		{
		 			return (new string[0]);
		 		}
		 		string[] names =3D fontNames.Split(new char[] { ','});
		 		for(int i=3D0; i < names.Length; i++)
		 		{
					names[i] =3D names[i].Trim();
				}
		 		return names;
		 	}
		 	throw GetConvertFromException(value);
		 }

		 public override object ConvertTo(ITypeDescriptorContext context, =
CultureInfo culture, object value, Type destinationType)
		 {
		 	if(destinationType is typeof(string))
		 	{
		 		if(value =3D=3D null || ((string[])value) =3D=3D null)
		 			return String.Empty;
		 		return String.Join(",", (string[])value);
		 	}
		 	throw GetConvertToException(value, destinationType);
		 }
	}
}

------=_NextPart_000_000B_01C1CC6D.37224960--