TypeConverter HELP Needed!

Discussion in 'ASP .Net Web Controls' started by ME, Jul 9, 2004.

  1. ME

    ME Guest

    Ok, I have a property called ControlType. The property is of type 'Type'.
    It stores just the type of control a user selected. I am really confused
    how I would implement a converter on this type of property since all I need
    returned is the Type of control being referenced and not the control itself.
    Trouble is when I use the following code in my custom control it takes a
    dive when the webform tries to display its html. So I humbly ask for some
    advice on how to solve this issue!

    Thanks,

    Matt

    //------------------------------------------- THE ERROR RETURN IS:

    Unable to generate code for a value of type 'System.Type'. This error
    occurred while trying to generate the property value for ControlType.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.Web.HttpException: Unable to generate code for a
    value of type 'System.Type'. This error occurred while trying to generate
    the property value for ControlType.

    Source Error:

    An unhandled exception was generated during the execution of the current web
    request. Information regarding the origin and location of the exception can
    be identified using the exception stack trace below.

    [HttpException (0x80004005): Unable to generate code for a value of type
    'System.Type'. This error occurred while trying to generate the property
    value for ControlType.]

    System.Web.Compilation.CodeDomUtility.GenerateExpressionForValue(PropertyInf
    o propertyInfo, Object value, Type valueType) +2255

    System.Web.Compilation.TemplateControlCompiler.BuildBuildMethod(ControlBuild
    er builder, Boolean fTemplate, PropertySetterEntry pse) +2539

    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
    r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +794

    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
    r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +675

    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
    r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +675

    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
    r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +352

    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
    r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +352
    System.Web.Compilation.TemplateControlCompiler.BuildMiscClassMembers()
    +51
    System.Web.Compilation.PageCompiler.BuildMiscClassMembers() +10
    System.Web.Compilation.BaseCompiler.BuildSourceDataTree() +1276
    System.Web.Compilation.BaseCompiler.GetCompiledType() +129
    System.Web.UI.PageParser.CompileIntoType() +62
    System.Web.UI.TemplateParser.GetParserCacheItemThroughCompilation() +126

    [HttpException (0x80004005): Unable to generate code for a value of type
    'System.Type'. This error occurred while trying to generate the property
    value for ControlType.]
    System.Web.UI.TemplateParser.GetParserCacheItemInternal(Boolean
    fCreateIfNotFound) +692
    System.Web.UI.TemplateParser.GetParserCacheItem() +32
    System.Web.UI.TemplateControlParser.CompileAndGetParserCacheItem(String
    virtualPath, String inputFile, HttpContext context) +116
    System.Web.UI.TemplateControlParser.GetCompiledInstance(String
    virtualPath, String inputFile, HttpContext context) +36
    System.Web.UI.PageParser.GetCompiledPageInstanceInternal(String
    virtualPath, String inputFile, HttpContext context) +43
    System.Web.UI.PageHandlerFactory.GetHandler(HttpContext context, String
    requestType, String url, String path) +44
    System.Web.HttpApplication.MapHttpHandler(HttpContext context, String
    requestType, String path, String pathTranslated, Boolean useAppConfig) +699

    System.Web.MapHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep
    ..Execute() +95
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
    completedSynchronously) +173




    //------------------------------------------- HERE IS THE PROPERTY CODE:

    /// <summary>
    /// ControlType = The Type of control you referencing
    /// </summary>
    [
    NotifyParentProperty(true),
    TypeConverter(typeof(ControlGroupControlTypeConverter))
    ]
    public Type ControlType
    {
    get
    {
    return controltype;
    }
    set
    {
    controltype=(System.Type) value;
    }
    }


    //------------------------------------------- HERE IS THE CONVERTER CODE:

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.Design;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Collections;
    using System.IO;
    using System.Text;
    using System.Drawing.Design;
    using System.Reflection;
    using System.ComponentModel.Design.Serialization;
    namespace ControlGroup
    {
    public class ControlGroupControlTypeConverter :
    System.ComponentModel.TypeConverter
    {
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type
    sourceType)
    {
    if (sourceType == typeof(string))
    {
    return true;
    }
    return base.CanConvertFrom (context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context,
    System.Globalization.CultureInfo culture, object value)
    {
    if (value is string)
    {
    string v=(string) value;
    if (v == typeof(System.Web.UI.WebControls.TextBox).ToString())
    {
    return typeof(System.Web.UI.WebControls.TextBox);
    }
    }
    return base.ConvertFrom (context, culture, value);
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type
    destinationType)
    {
    if (destinationType == typeof(InstanceDescriptor))
    {
    return true;
    }
    return base.CanConvertTo (context, destinationType);
    }
    public override object ConvertTo(ITypeDescriptorContext context,
    System.Globalization.CultureInfo culture, object value, Type
    destinationType)
    {
    if (destinationType == typeof(InstanceDescriptor) && value is Type)
    {
    Type t =(Type) value;
    ConstructorInfo ci = typeof(Type).GetConstructor(new Type[0]);
    // if (ci != null)
    // {
    return new InstanceDescriptor(ci,new object[0]);
    // }
    }
    return base.ConvertTo (context, culture, value, destinationType);
    }
    }
    }
     
    ME, Jul 9, 2004
    #1
    1. Advertising

  2. ME

    ME Guest

    Also, I think the problem is with my converter but I am not understanding
    how the ConvertTo override works.

    Thanks,

    Matt

    "ME" <trash.trash@comcastDOTnet> wrote in message
    news:v4rHc.51897$XM6.11312@attbi_s53...
    > Ok, I have a property called ControlType. The property is of type 'Type'.
    > It stores just the type of control a user selected. I am really confused
    > how I would implement a converter on this type of property since all I

    need
    > returned is the Type of control being referenced and not the control

    itself.
    > Trouble is when I use the following code in my custom control it takes a
    > dive when the webform tries to display its html. So I humbly ask for some
    > advice on how to solve this issue!
    >
    > Thanks,
    >
    > Matt
    >
    > //------------------------------------------- THE ERROR RETURN IS:
    >
    > Unable to generate code for a value of type 'System.Type'. This error
    > occurred while trying to generate the property value for ControlType.
    > Description: An unhandled exception occurred during the execution of the
    > current web request. Please review the stack trace for more information
    > about the error and where it originated in the code.
    >
    > Exception Details: System.Web.HttpException: Unable to generate code for a
    > value of type 'System.Type'. This error occurred while trying to generate
    > the property value for ControlType.
    >
    > Source Error:
    >
    > An unhandled exception was generated during the execution of the current

    web
    > request. Information regarding the origin and location of the exception

    can
    > be identified using the exception stack trace below.
    >
    > [HttpException (0x80004005): Unable to generate code for a value of type
    > 'System.Type'. This error occurred while trying to generate the property
    > value for ControlType.]
    >
    >

    System.Web.Compilation.CodeDomUtility.GenerateExpressionForValue(PropertyInf
    > o propertyInfo, Object value, Type valueType) +2255
    >
    >

    System.Web.Compilation.TemplateControlCompiler.BuildBuildMethod(ControlBuild
    > er builder, Boolean fTemplate, PropertySetterEntry pse) +2539
    >
    >

    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
    > r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse)

    +794
    >
    >

    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
    > r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse)

    +675
    >
    >

    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
    > r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse)

    +675
    >
    >

    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
    > r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse)

    +352
    >
    >

    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
    > r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse)

    +352
    > System.Web.Compilation.TemplateControlCompiler.BuildMiscClassMembers()
    > +51
    > System.Web.Compilation.PageCompiler.BuildMiscClassMembers() +10
    > System.Web.Compilation.BaseCompiler.BuildSourceDataTree() +1276
    > System.Web.Compilation.BaseCompiler.GetCompiledType() +129
    > System.Web.UI.PageParser.CompileIntoType() +62
    > System.Web.UI.TemplateParser.GetParserCacheItemThroughCompilation()

    +126
    >
    > [HttpException (0x80004005): Unable to generate code for a value of type
    > 'System.Type'. This error occurred while trying to generate the property
    > value for ControlType.]
    > System.Web.UI.TemplateParser.GetParserCacheItemInternal(Boolean
    > fCreateIfNotFound) +692
    > System.Web.UI.TemplateParser.GetParserCacheItem() +32
    > System.Web.UI.TemplateControlParser.CompileAndGetParserCacheItem(String
    > virtualPath, String inputFile, HttpContext context) +116
    > System.Web.UI.TemplateControlParser.GetCompiledInstance(String
    > virtualPath, String inputFile, HttpContext context) +36
    > System.Web.UI.PageParser.GetCompiledPageInstanceInternal(String
    > virtualPath, String inputFile, HttpContext context) +43
    > System.Web.UI.PageHandlerFactory.GetHandler(HttpContext context, String
    > requestType, String url, String path) +44
    > System.Web.HttpApplication.MapHttpHandler(HttpContext context, String
    > requestType, String path, String pathTranslated, Boolean useAppConfig)

    +699
    >
    >

    System.Web.MapHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep
    > .Execute() +95
    > System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
    > completedSynchronously) +173
    >
    >
    >
    >
    > //------------------------------------------- HERE IS THE PROPERTY CODE:
    >
    > /// <summary>
    > /// ControlType = The Type of control you referencing
    > /// </summary>
    > [
    > NotifyParentProperty(true),
    > TypeConverter(typeof(ControlGroupControlTypeConverter))
    > ]
    > public Type ControlType
    > {
    > get
    > {
    > return controltype;
    > }
    > set
    > {
    > controltype=(System.Type) value;
    > }
    > }
    >
    >
    > //------------------------------------------- HERE IS THE CONVERTER CODE:
    >
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.Design;
    > using System.ComponentModel;
    > using System.ComponentModel.Design;
    > using System.Collections;
    > using System.IO;
    > using System.Text;
    > using System.Drawing.Design;
    > using System.Reflection;
    > using System.ComponentModel.Design.Serialization;
    > namespace ControlGroup
    > {
    > public class ControlGroupControlTypeConverter :
    > System.ComponentModel.TypeConverter
    > {
    > public override bool CanConvertFrom(ITypeDescriptorContext context, Type
    > sourceType)
    > {
    > if (sourceType == typeof(string))
    > {
    > return true;
    > }
    > return base.CanConvertFrom (context, sourceType);
    > }
    >
    > public override object ConvertFrom(ITypeDescriptorContext context,
    > System.Globalization.CultureInfo culture, object value)
    > {
    > if (value is string)
    > {
    > string v=(string) value;
    > if (v == typeof(System.Web.UI.WebControls.TextBox).ToString())
    > {
    > return typeof(System.Web.UI.WebControls.TextBox);
    > }
    > }
    > return base.ConvertFrom (context, culture, value);
    > }
    >
    > public override bool CanConvertTo(ITypeDescriptorContext context, Type
    > destinationType)
    > {
    > if (destinationType == typeof(InstanceDescriptor))
    > {
    > return true;
    > }
    > return base.CanConvertTo (context, destinationType);
    > }
    > public override object ConvertTo(ITypeDescriptorContext context,
    > System.Globalization.CultureInfo culture, object value, Type
    > destinationType)
    > {
    > if (destinationType == typeof(InstanceDescriptor) && value is Type)
    > {
    > Type t =(Type) value;
    > ConstructorInfo ci = typeof(Type).GetConstructor(new Type[0]);
    > // if (ci != null)
    > // {
    > return new InstanceDescriptor(ci,new object[0]);
    > // }
    > }
    > return base.ConvertTo (context, culture, value, destinationType);
    > }
    > }
    > }
    >
    >
     
    ME, Jul 9, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian
    Replies:
    1
    Views:
    475
    Gary Varga
    Jul 18, 2003
  2. Replies:
    0
    Views:
    428
  3. Sky Sigal
    Replies:
    1
    Views:
    500
    Sky Sigal
    Jul 14, 2004
  4. ME

    TypeConverter HELP Needed!

    ME, Jul 9, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    147
  5. Sky Sigal

    Help! custom TypeConverter/Persistence problems...

    Sky Sigal, Jul 13, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    163
    Sky Sigal
    Jul 13, 2004
Loading...

Share This Page