Unable to generate code for a value of type...

Discussion in 'ASP .Net' started by Chris Bower, Nov 17, 2003.

  1. Chris Bower

    Chris Bower Guest

    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?
    Chris Bower, Nov 17, 2003
    #1
    1. Advertising

  2. Chris Bower

    Chris Bower Guest

    As an update:
    I changed the CanConvertTo so that it will return True for an
    InstanceDescriptor and then messed around with some code to create an
    InstanceDescriptor in the ConvertTo function:

    If destinationType Is GetType(InstanceDescriptor) Then
    Dim mi As MethodInfo = GetType([Enum]).GetMethod("Parse", New
    Type() {GetType(Type), GetType(String)})

    Dim r As Rights = CType(value, Rights)
    Dim id As New InstanceDescriptor(mi, New Object()
    {GetType(Rights), r.ToString})
    Return id
    End If

    Now when I load the page I get an Object reference not set to an instance of
    an object error. I know for sure that the InstanceDescriptor can be used to
    create an instance of the Rights enum, as I tested it by using Invoke so I'm
    confused as to why this doesn't work.

    I also realize that using an InstanceDescriptor isn't the way I should
    really be going since this is an enum and doesn't have a parameterized
    constructor or anything, but I can't think of anything else. Any thoughts?
    Chris Bower, Nov 17, 2003
    #2
    1. Advertising

  3. Chris Bower

    MSFT Guest

    Hi Chris,

    I got same error when I tested your code. Anyway, the problem disappeared
    after I change your property name:

    <Description("The rights required to view this control."), _
    Editor(GetType(RightsEditor), GetType(UITypeEditor)),
    DefaultValue("Everyone")> _
    Public Property MyRights() As Rights
    Get
    Return _rights
    End Get
    Set(ByVal Value As Rights)
    _rights = Value
    End Set
    End Property

    I may test this in your actual code to see if it will work.

    Luke
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    MSFT, Nov 18, 2003
    #3
  4. Chris Bower

    Chris Bower Guest

    Hmm.. I tried that with one of my controls but the only thing that changed
    was the error message:
    Unable to generate code for a value of type
    'Intranet.CustomControls.Rights'. This error occurred while trying to
    generate the property value for DisplayRights.

    I take it you're looking at the sample I put together for Michael? Has this
    worked with that sample?

    "MSFT" <> wrote in message
    news:...
    > Hi Chris,
    >
    > I got same error when I tested your code. Anyway, the problem disappeared
    > after I change your property name:
    >
    > <Description("The rights required to view this control."), _
    > Editor(GetType(RightsEditor), GetType(UITypeEditor)),
    > DefaultValue("Everyone")> _
    > Public Property MyRights() As Rights
    > Get
    > Return _rights
    > End Get
    > Set(ByVal Value As Rights)
    > _rights = Value
    > End Set
    > End Property
    >
    > I may test this in your actual code to see if it will work.
    >
    > Luke
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    Chris Bower, Nov 18, 2003
    #4
  5. Chris Bower

    Chris Bower Guest

    For posterity's sake:
    This turned out to be a bug in the 1.1 framework. There a KB article about
    it here: http://support.microsoft.com/default.aspx?scid=kb;en-us;825082

    "Chris Bower" <> wrote in message
    news:...
    > Hmm.. I tried that with one of my controls but the only thing that changed
    > was the error message:
    > Unable to generate code for a value of type
    > 'Intranet.CustomControls.Rights'. This error occurred while trying to
    > generate the property value for DisplayRights.
    >
    > I take it you're looking at the sample I put together for Michael? Has

    this
    > worked with that sample?
    >
    > "MSFT" <> wrote in message
    > news:...
    > > Hi Chris,
    > >
    > > I got same error when I tested your code. Anyway, the problem

    disappeared
    > > after I change your property name:
    > >
    > > <Description("The rights required to view this control."), _
    > > Editor(GetType(RightsEditor), GetType(UITypeEditor)),
    > > DefaultValue("Everyone")> _
    > > Public Property MyRights() As Rights
    > > Get
    > > Return _rights
    > > End Get
    > > Set(ByVal Value As Rights)
    > > _rights = Value
    > > End Set
    > > End Property
    > >
    > > I may test this in your actual code to see if it will work.
    > >
    > > Luke
    > > Microsoft Online Support
    > >
    > > Get Secure! www.microsoft.com/security
    > > (This posting is provided "AS IS", with no warranties, and confers no
    > > rights.)
    > >

    >
    >
    Chris Bower, Nov 18, 2003
    #5
    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. A Traveler

    Unable to generate code...

    A Traveler, Oct 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    380
    A Traveler
    Oct 13, 2004
  2. Jack Bates
    Replies:
    0
    Views:
    265
    Jack Bates
    May 2, 2011
  3. xyz
    Replies:
    0
    Views:
    292
  4. Chris Bower

    Unable to generate code for a value of type...

    Chris Bower, Nov 17, 2003, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    240
    Chris Bower
    Nov 18, 2003
  5. KMILO
    Replies:
    0
    Views:
    159
    KMILO
    Apr 25, 2005
Loading...

Share This Page