2

所以希望这是一个愚蠢的问题(这会让它很容易回答)。

我正在尝试制作一个复合服务器控件,该控件封装了 TextBox、一些验证器和其他内容,具体取决于所需字段的类型。

我的控件有一个“DataType”值,可以让我确定要显示的内容。例如,如果 DataType 是“Date”,我会渲染 AjaxControlToolkit.CalendarExtender 等。

我的通用“Value”属性是一个对象,它将返回 DataType 属性要求的任何内容,因此在上面的示例中,Value 将是 Date 类型。

所以这是我的问题,我需要将传入的 Value 属性转换为 DataType 在运行时要求的任何内容。

正如你所看到的,我试图为它编写一个 TypeConverter 但它似乎不起作用,我在编译过程中最终出现了这个错误:

无法为“System.Object”类型的值生成代码。尝试为 Value 生成属性值时发生此错误。

任何帮助将非常感激!

这是我试图调用我的控件的方式:

<custom:SomeTextControl ID="dateFoo" runat="server" DataType="Date" Value="08/11/2009" />

这是我的课:

Public Class SomeTextControl
Inherits Control

  Private _Value as Object
  <Bindable(True), TypeConverter(GetType(ObjectConverter))> _
  Public Property Value() As Object
    Get
        Return _Value
    End Get
    Set(ByVal value As Object)
        _Value = value
    End Set
  End Property
End Class

Public Class ObjectConverter
    Inherits TypeConverter

    Public Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
        Dim o As Object
        o = value
        Return o
    End Function

    Public Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As System.Type) As Boolean
        If sourceType Is GetType(String) Then
            Return True
        End If
        Return MyBase.CanConvertFrom(context, sourceType)
    End Function

    Public Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
        If destinationType Is GetType(String) Then
            Return True
        End If
        Return MyBase.CanConvertTo(context, destinationType)
    End Function
End Class
4

1 回答 1

1

所以似乎我自己半想通了......对于过分热心地提出问题来说太多了。

无论如何,似乎我忘记检查destinationType 是否是InstanceDescriptor(duh)。一旦我知道了,我需要再过几分钟才能确定我必须将我的对象设置为字符串(或其他东西,但我选择了字符串,因为无论如何我都会将它吐回 TextBox ) 因为 Object 不采用构造函数。

让我知道你们是否有更好的想法。

所以我最终得到了这个 TypeConverter:

Friend Class ObjectConverter
    Inherits TypeConverter

    Public Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
        Return value
    End Function

    Public 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(InstanceDescriptor) Then
            Return True
        End If
        Return MyBase.CanConvertFrom(context, sourceType)
    End Function

    Public 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(InstanceDescriptor) Then
            Return True
        End If
        Return MyBase.CanConvertTo(context, destinationType)
    End Function

    Public 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 destinationType Is GetType(InstanceDescriptor) Then
            Dim constructor As ConstructorInfo = GetType(String).GetConstructor(New Type() {GetType(Char())})
            Dim instance As New InstanceDescriptor(constructor, New Object() {CType(value.ToString, Char())})
            Return instance
        End If

        Return MyBase.ConvertTo(context, culture, value, destinationType)
    End Function
End Class
于 2009-08-21T17:37:43.317 回答