我和web表单解析器现在遇到了麻烦创建类型“System.Object的”的对象的今天,关于控制属性,我希望你们中的一些可以帮我!无法从它的字符串表示
我有一个Control
与object
类型名为Value属性。当我每次宣布它在我的aspx时候,我得到的错误
不能创建从‘价值’属性的字符串 表示“...”类型的对象“System.Object的”。
跆拳道,心不是一切对象? :)
然后我尝试对我的财产增加TypeConverter
,但没有运气。
我的控制
[ParseChildren(true, "Value")]
[TypeConverter(typeof(ExpandableObjectConverter))]
[ControlBuilder(typeof(ParamControlBuilder))]
public class Param : Control
{
public string Name { get; set; }
[TypeConverter(typeof(StringToObjectConverter))]
public object Value { get; set; }
protected override void AddParsedSubObject(object obj)
{
base.AddParsedSubObject(obj);
}
public override void DataBind()
{
base.DataBind();
}
protected override void DataBindChildren()
{
base.DataBindChildren();
}
}
的类型转换器
public class StringToObjectConverter : TypeConverter
{
public override bool IsValid(ITypeDescriptorContext context, object value)
{
return true;
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(object))
{
return true;
}
if (destinationType == typeof(InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return value.ToString();
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(object))
{
return (object)value;
}
if (destinationType == typeof(InstanceDescriptor))
{
return new InstanceDescriptor(typeof(object).GetConstructor(new Type[] { }), new object[] { });
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
我想要做的是能在我的aspx页面中写入以下
<my:param name="A object" value="<# value_of_a_method()" %> runat="server" />
<my:param name="A object" value="this_is_just_a_string" runat="server" />
第一个例子工程好吧,第二次失败,提到错误。不,我不能相信,解决这个问题的唯一方法是每次数据绑定,即使是常量字符串像
<my:param name="A object" value='<%# "this_is_just_a_string" %>' runat="server" />
该转换器用于控制,而不是属性。那样,如果你使用它作为书面的,它总是反序列化为一个字符串;将一个字符串转换为对象vs将其反序列化为之前的东西有很大的区别。 –
所以我应该基本上创建我自己的Wrapper类,只是持有一个对象,并把一个转换器,可以转换字符串和对象来自我的包装?然后,我的Value属性的类型将是这个包装而不是Object? –
你还没有真正清楚你想要达到什么目的。坚持和重新创造一个只有“对象”的价值,实际上是非常难... –