C
Chris Bower
Reposted from aspnet.buildingcontrols:
Ok, I've got a bunch of derived controls that all have a property Rights of
type Rights (Rights is an Enumerator). I wrote a custom TypeConverter so
that I can use comma separated values in design-time. The TypeConverter
works great in design-time. It converts to and from just fine... However,
when I try to load any page in the site now I get the following error
(Following the error is code for the TypeConverter:
Exception Details: System.Web.HttpException: Unable to generate code for a
value of type 'Intranet.CustomControls.Rights'. This error occurred while
trying to generate the property value for Rights.
Stack Trace:
[HttpException (0x80004005): Unable to generate code for a value of type
'Intranet.CustomControls.Rights'. This error occurred while trying to
generate the property value for Rights.]
System.Web.Compilation.CodeDomUtility.GenerateExpressionForValue(PropertyInf
o propertyInfo, Object value, Type valueType) +2253
System.Web.Compilation.TemplateControlCompiler.BuildBuildMethod(ControlBuild
er builder, Boolean fTemplate, PropertySetterEntry pse) +2538
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) +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() +59
System.Web.UI.TemplateParser.GetParserCacheItemThroughCompilation() +126
Following is the code for my TypeConverter:
Public Class RightsConverter
Inherits TypeConverter
Public Overloads Overrides Function ConvertFrom(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, ByVal value As Object) As Object
If IsNothing(value) Then
Return 0
End If
Dim x As String
If value.GetType Is GetType(String) Then
Dim data As String = CStr(value)
Dim r As Rights
r = CType([Enum].Parse(GetType(Rights), data), Rights)
Return r
End If
End Function
Public Overloads Overrides Function ConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, ByVal value As Object, ByVal
destinationType As System.Type) As Object
If Not IsNothing(value) Then
If Not value.GetType Is GetType(Rights) Then
Throw New ArgumentException("Invalid Rights Enum", "value")
End If
End If
If destinationType Is GetType(String) Then
If IsNothing(value) Then
Return String.Empty
End If
Dim r As Rights = CType(value, Rights)
Dim rVal As String
Dim sa() As String
Dim name As String
Dim i As Integer
For Each name In [Enum].GetNames(GetType(Rights))
i = [Enum].Parse(GetType(Rights), name)
If (i And r) > 0 Then
If IsNothing(sa) Then
ReDim sa(0)
sa(0) = name
Else
ReDim Preserve sa(sa.Length)
sa(sa.GetUpperBound(0)) = name
End If
End If
Next
If Not IsNothing(sa) Then
For i = 0 To sa.GetUpperBound(0)
If i > 0 Then
rVal &= ", " & sa(i)
Else
rVal = sa(i)
End If
Next
Return rVal
Else
Return ""
End If
End If
End Function
Public Overloads Overrides Function CanConvertFrom(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As
System.Type) As Boolean
If sourceType Is GetType(String) Then
Return True
ElseIf sourceType Is GetType(Integer) Then
Return True
Else
MyBase.CanConvertFrom(context, sourceType)
End If
End Function
Public Overloads Overrides Function CanConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As
System.Type) As Boolean
If destinationType Is GetType(String) Then
Return True
ElseIf destinationType Is GetType(Integer) Then
Return True
Else
MyBase.CanConvertFrom(context, destinationType)
End If
End Function
End Class
Here is what the property looks like on the controls:
<Description("The rights required to view this control."), _
Editor(GetType(RightsEditor), GetType(UITypeEditor)),
DefaultValue("Everyone")> _
Public Property Rights() As Rights
Get
Return _rights
End Get
Set(ByVal Value As Rights)
_rights = Value
End Set
End Property
Anyone have any ideas as to what could be wrong?
Ok, I've got a bunch of derived controls that all have a property Rights of
type Rights (Rights is an Enumerator). I wrote a custom TypeConverter so
that I can use comma separated values in design-time. The TypeConverter
works great in design-time. It converts to and from just fine... However,
when I try to load any page in the site now I get the following error
(Following the error is code for the TypeConverter:
Exception Details: System.Web.HttpException: Unable to generate code for a
value of type 'Intranet.CustomControls.Rights'. This error occurred while
trying to generate the property value for Rights.
Stack Trace:
[HttpException (0x80004005): Unable to generate code for a value of type
'Intranet.CustomControls.Rights'. This error occurred while trying to
generate the property value for Rights.]
System.Web.Compilation.CodeDomUtility.GenerateExpressionForValue(PropertyInf
o propertyInfo, Object value, Type valueType) +2253
System.Web.Compilation.TemplateControlCompiler.BuildBuildMethod(ControlBuild
er builder, Boolean fTemplate, PropertySetterEntry pse) +2538
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) +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() +59
System.Web.UI.TemplateParser.GetParserCacheItemThroughCompilation() +126
Following is the code for my TypeConverter:
Public Class RightsConverter
Inherits TypeConverter
Public Overloads Overrides Function ConvertFrom(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, ByVal value As Object) As Object
If IsNothing(value) Then
Return 0
End If
Dim x As String
If value.GetType Is GetType(String) Then
Dim data As String = CStr(value)
Dim r As Rights
r = CType([Enum].Parse(GetType(Rights), data), Rights)
Return r
End If
End Function
Public Overloads Overrides Function ConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, ByVal value As Object, ByVal
destinationType As System.Type) As Object
If Not IsNothing(value) Then
If Not value.GetType Is GetType(Rights) Then
Throw New ArgumentException("Invalid Rights Enum", "value")
End If
End If
If destinationType Is GetType(String) Then
If IsNothing(value) Then
Return String.Empty
End If
Dim r As Rights = CType(value, Rights)
Dim rVal As String
Dim sa() As String
Dim name As String
Dim i As Integer
For Each name In [Enum].GetNames(GetType(Rights))
i = [Enum].Parse(GetType(Rights), name)
If (i And r) > 0 Then
If IsNothing(sa) Then
ReDim sa(0)
sa(0) = name
Else
ReDim Preserve sa(sa.Length)
sa(sa.GetUpperBound(0)) = name
End If
End If
Next
If Not IsNothing(sa) Then
For i = 0 To sa.GetUpperBound(0)
If i > 0 Then
rVal &= ", " & sa(i)
Else
rVal = sa(i)
End If
Next
Return rVal
Else
Return ""
End If
End If
End Function
Public Overloads Overrides Function CanConvertFrom(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As
System.Type) As Boolean
If sourceType Is GetType(String) Then
Return True
ElseIf sourceType Is GetType(Integer) Then
Return True
Else
MyBase.CanConvertFrom(context, sourceType)
End If
End Function
Public Overloads Overrides Function CanConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As
System.Type) As Boolean
If destinationType Is GetType(String) Then
Return True
ElseIf destinationType Is GetType(Integer) Then
Return True
Else
MyBase.CanConvertFrom(context, destinationType)
End If
End Function
End Class
Here is what the property looks like on the controls:
<Description("The rights required to view this control."), _
Editor(GetType(RightsEditor), GetType(UITypeEditor)),
DefaultValue("Everyone")> _
Public Property Rights() As Rights
Get
Return _rights
End Get
Set(ByVal Value As Rights)
_rights = Value
End Set
End Property
Anyone have any ideas as to what could be wrong?