Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > TypeConverter HELP Needed!

Reply
Thread Tools

TypeConverter HELP Needed!

 
 
ME
Guest
Posts: n/a
 
      07-09-2004
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.GenerateExpr essionForValue(PropertyInf
o propertyInfo, Object value, Type valueType) +2255

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

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

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

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

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

System.Web.Compilation.TemplateControlCompiler.Bui ldSourceDataTreeFromBuilde
r(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +352
System.Web.Compilation.TemplateControlCompiler.Bui ldMiscClassMembers()
+51
System.Web.Compilation.PageCompiler.BuildMiscClass Members() +10
System.Web.Compilation.BaseCompiler.BuildSourceDat aTree() +1276
System.Web.Compilation.BaseCompiler.GetCompiledTyp e() +129
System.Web.UI.PageParser.CompileIntoType() +62
System.Web.UI.TemplateParser.GetParserCacheItemThr oughCompilation() +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.GetParserCacheItemInt ernal(Boolean
fCreateIfNotFound) +692
System.Web.UI.TemplateParser.GetParserCacheItem() +32
System.Web.UI.TemplateControlParser.CompileAndGetP arserCacheItem(String
virtualPath, String inputFile, HttpContext context) +116
System.Web.UI.TemplateControlParser.GetCompiledIns tance(String
virtualPath, String inputFile, HttpContext context) +36
System.Web.UI.PageParser.GetCompiledPageInstanceIn ternal(String
virtualPath, String inputFile, HttpContext context) +43
System.Web.UI.PageHandlerFactory.GetHandler(HttpCo ntext context, String
requestType, String url, String path) +44
System.Web.HttpApplication.MapHttpHandler(HttpCont ext context, String
requestType, String path, String pathTranslated, Boolean useAppConfig) +699

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




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

/// <summary>
/// ControlType = The Type of control you referencing
/// </summary>
[
NotifyParentProperty(true),
TypeConverter(typeof(ControlGroupControlTypeConver ter))
]
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);
}
}
}


 
Reply With Quote
 
 
 
 
ME
Guest
Posts: n/a
 
      07-09-2004
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:_3rHc.52382$Oq2.24639@attbi_s52...
> 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.GenerateExpr essionForValue(PropertyInf
> o propertyInfo, Object value, Type valueType) +2255
>
>

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

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

+794
>
>

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

+675
>
>

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

+675
>
>

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

+352
>
>

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

+352
> System.Web.Compilation.TemplateControlCompiler.Bui ldMiscClassMembers()
> +51
> System.Web.Compilation.PageCompiler.BuildMiscClass Members() +10
> System.Web.Compilation.BaseCompiler.BuildSourceDat aTree() +1276
> System.Web.Compilation.BaseCompiler.GetCompiledTyp e() +129
> System.Web.UI.PageParser.CompileIntoType() +62
> System.Web.UI.TemplateParser.GetParserCacheItemThr oughCompilation()

+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.GetParserCacheItemInt ernal(Boolean
> fCreateIfNotFound) +692
> System.Web.UI.TemplateParser.GetParserCacheItem() +32
> System.Web.UI.TemplateControlParser.CompileAndGetP arserCacheItem(String
> virtualPath, String inputFile, HttpContext context) +116
> System.Web.UI.TemplateControlParser.GetCompiledIns tance(String
> virtualPath, String inputFile, HttpContext context) +36
> System.Web.UI.PageParser.GetCompiledPageInstanceIn ternal(String
> virtualPath, String inputFile, HttpContext context) +43
> System.Web.UI.PageHandlerFactory.GetHandler(HttpCo ntext context, String
> requestType, String url, String path) +44
> System.Web.HttpApplication.MapHttpHandler(HttpCont ext context, String
> requestType, String path, String pathTranslated, Boolean useAppConfig)

+699
>
>

System.Web.MapHandlerExecutionStep.System.Web.Http Application+IExecutionStep
> .Execute() +95
> System.Web.HttpApplication.ExecuteStep(IExecutionS tep step, Boolean&
> completedSynchronously) +173
>
>
>
>
> //------------------------------------------- HERE IS THE PROPERTY CODE:
>
> /// <summary>
> /// ControlType = The Type of control you referencing
> /// </summary>
> [
> NotifyParentProperty(true),
> TypeConverter(typeof(ControlGroupControlTypeConver ter))
> ]
> 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);
> }
> }
> }
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help! Problems with custom TypeConverter and Persistence... Sky Sigal ASP .Net 1 07-14-2004 01:02 AM
Help! custom TypeConverter/Persistence problems... Sky Sigal ASP .Net Web Controls 0 07-13-2004 10:54 PM
TypeConverter HELP Needed! ME ASP .Net Web Controls 1 07-09-2004 06:41 AM
TypeConverter for pre-existing system classes? foldface@yahoo.co.uk ASP .Net 0 05-27-2004 04:15 PM
make Serializable or create custom TypeConverter ? Christian ASP .Net 1 07-18-2003 12:42 PM



Advertisments