TypeConverter HELP Needed!

M

ME

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);
}
}
}
 
M

ME

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

Thanks,

Matt

ME said:
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) +794System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +675System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +675System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilde
r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +352System.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) +699System.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);
}
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top