转换来源中的目标类型? [英] Destination type in Convert from?
问题描述
我一直在为要构建的许多类使用自定义类型转换器,这些类将在属性网格中显示.在使用它们时,我通常使用大多数论坛和MS帮助中显示的通用Convert From例程.我只想在显示的字符串中显示1个子属性,但是在转换自"例程中,我想访问所有子属性信息.我似乎找不到此信息是否在Context参数中或其他位置可用,或者根本没有.请帮助我找到解决方案.谢谢!
这是转换自"例程的示例.我试图获得对调用类子属性的访问.大多数Convert From例程将所有数据放入调用类字符串中,但是我试图通过使其无法浏览或在调用类字符串中隐藏某些数据.
I have been using custom type converters for many of the classes that I am building to use in a property grid for display. In using them I have generally used the generic Convert From routines shown in most forums and MS help. I want to show only 1 of the sub properties in the string that is shown, but I want to have access to all the sub property information when in the Convert From routine. I can''t seem to find if this information is available in the Context parameter or somewhere else or not at all. Please help me find a solution. Thanks!
This is an example of the Convert From routine. I am trying to gain access to the calling classes sub properties. Most Convert From routines put ALL the data into the calling classes string, but I am trying to hide some of that by not making it browsable or in the calling class string.
Public Overloads Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
If TypeOf value Is String Then
Try
Dim _TID4Register As New TID4Register
_TID4Register = context.Instance 'This produces an error
_TID4Register.Value = CType(value, Byte)
Return _TID4Register
Catch ex As Exception
Throw New ArgumentException("Can not convert '" + value + "' to type Byte")
End Try
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
推荐答案
我不确切知道您会遇到哪个错误,但是您标记的行至少应该是:
I don''t know exactly which error you get, but the line you tagged should at least be :
_TID4Register = context.Instance as TID4Register;
我敢打赌,没有从对象到TID4Register的标准转换,因此必须进行强制转换.
干杯
I bet there''s no standard conversion from object to TID4Register, so a cast is necessary.
Cheers
这篇关于转换来源中的目标类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!