无法从它的字符串重新presentation创建类型的对象'System.Object的“ [英] Cannot create an object of type 'System.Object' from its string representation

查看:380
本文介绍了无法从它的字符串重新presentation创建类型的对象'System.Object的“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和web表单解析器今天遇到了一些困难,对控件的属性,我希望你们中的一些可以帮我!

我有一个控制对象一个名为Value属性类型。每次当我宣布它在我的aspx时候,我得到的错误


  

无法从它的字符串创建类型的对象'System.Object的
  再presentation'...'的'值'属性。


跆拳道,心不是一切对象? :)

然后我尝试对我的财产增加的TypeConverter ,但没有运气。

我的控制

  [ParseChildren(真正的价值)]
[的TypeConverter(typeof运算(ExpandableObjectConverter))]
[ControlBuilder(typeof运算(ParamControlBuilder))]
公共类参数:控制
{
    公共字符串名称{;组; }    [的TypeConverter(typeof运算(StringToObjectConverter))]
    公共对象值{搞定;组; }    保护覆盖无效AddParsedSubObject(obj对象)
    {
        base.AddParsedSubObject(OBJ);
    }    公共覆盖无效的DataBind()
    {
        base.DataBind();
    }    保护覆盖无效DataBindChildren()
    {
        base.DataBindChildren();
    }
}

该类型转换器

 公共类StringToObjectConverter:类型转换器
{
    公众覆盖BOOL的IsValid(ITypeDescriptorContext背景下,对象的值)
    {
        返回true;
    }    公众覆盖布尔CanConvertFrom(ITypeDescriptorContext背景下,类型sourceType的)
    {
        如果(sourceType的== typeof运算(字符串))
        {
            返回true;
        }        返回base.CanConvertFrom(上下文,sourceType的);
    }    公众覆盖布尔CanConvertTo(ITypeDescriptorContext背景下,类型destinationType)
    {
        如果(destinationType == typeof运算(对象))
        {
            返回true;
        }        如果(destinationType == typeof运算(初始化InstanceDescriptor))
        {
            返回true;
        }        返回base.CanConvertTo(背景下,destinationType);
    }    公共覆盖对象ConvertFrom(ITypeDescriptorContext方面,CultureInfo的文化,对象的值)
    {
        返回value.ToString();
    }    公众覆盖对象的ConvertTo(ITypeDescriptorContext方面,CultureInfo的文化,对象的值,类型destinationType)
    {
        如果(destinationType == typeof运算(对象))
        {
            返回(对象)的价值;
        }        如果(destinationType == typeof运算(初始化InstanceDescriptor))
        {
            返回新初始化InstanceDescriptor(typeof运算(对象).GetConstructor(新类型[] {}),新的对象[] {});
        }        返回base.ConvertTo(上下文,文化,价值,destinationType);
    }
}

我想要做的是能在我的aspx页面中写入以下

 <我:PARAM NAME =目的VALUE =<#value_of_a_method()%GT; =服务器/>
<我:PARAM NAME =目的VALUE =this_is_just_a_string=服务器/>

第一个示例工作正常,第二个失败,并提到的错误。不,我不能相信,解决这个问题的唯一方法是每次数据绑定,即使是常量字符串像

 <我:PARAM NAME =目的值='<%#this_is_just_a_string%GT;' =服务器/>


解决方案

怎么样添加 ValueAsString 属性,实际上是一种序列化和prevent 被序列化?

ValueAsString 然后将负责序列化和反序列化适当地(如XML或JSON序列化)。

更新
事实证明,这是因为ASP.Net构建控件的方式,没有办法做你正在寻找做什么。

有可能实现一种机制来获取对象类型,如字符串或内部类,要处理,但如果包括原始类型,如混合整型,框架甚至不会执行类型转换器为您Object值。

例如,在以下两种情况下(用VB语法),既PARAMS将被创建,但是对于参数1的值将是什么

 < CTI:参数ID =参数1VALUE =<%#CINT(1)%GT; =服务器/>
< CTI:参数ID =参数2值=测试=服务器/>

有关完整,这里就是我如何能够实现一个目标转换器,而无需ASP.Net发脾气意识到它总是需要一个实际的类实例,而不是只是一个对象后:

首先,创建一个潜在的逻辑可以实例化一个中级班。这个类将为每个对象类型的类,它可以实现一个构造函数:

 公共类MyObjectConverter
{
    公共MyObjectConverter():基地()
    {
    }
    公共MyObjectConverter(字符串oValue):这个()
    {
        THIS.VALUE = oValue;
    }    公共对象值{搞定;组; }    公共重写字符串的ToString()
    {
        如果(THIS.VALUE == NULL)
        {
            返回的String.Empty;
        }
        其他
        {
            返回this.Value.ToString();
        }
    }
}

接下来,修改StringToObjectConverter的的ConvertTo方法,以便它将使用适当的构造新的类的实例(对象的构造还是混淆了code这里):

 公众覆盖对象的ConvertTo(ITypeDescriptorContext方面,CultureInfo的文化,对象的值,类型destinationType)
{
    如果(destinationType == typeof运算(对象))
    {
        返回(对象)的价值;
    }    如果(destinationType == typeof运算(初始化InstanceDescriptor))
    {
        返回新初始化InstanceDescriptor(typeof运算(MyObjectConverter).GetConstructor(新类型[] {value.GetType()}),新的对象[] {值});
    }    返回base.ConvertTo(上下文,文化,价值,destinationType);
}

最后,在参数对象,修改Value属性来处理新的类型:

 私有对象m_Value;
[的TypeConverter(typeof运算(StringToObjectConverter))]
公共对象值
{
    得到
    {
        返回m_Value;
    }
    组
    {
        如果(价值MyObjectConverter)
        {
            m_Value =((MyObjectConverter)值).value的;
        }
        其他
        {
            m_Value =价值;
        }
    }
}

我想有可能生成动态类或构造函数或对象转换器类,但这并不是说我有经验的东西,所以我真的不能说是否与否这是一个可行的解决方案。

因此​​,要总结,也有使用这种方法的两个问题:

1)不能使用它原始数据类型。然而,即使使用对象Value属性不使串入公式不支持此功能。

2)你必须明确定义构造函数的所有数据类型,你想支持。

Me and the webforms parser are having some difficulties today, regarding control properties I hope some of you can help me with!

I have a Control with a property named Value with object as type. When every time I declare it in my aspx I get the error

Cannot create an object of type 'System.Object' from its string representation '...' for the 'Value' property.

Wtf, isnt everything Objects? :)

I then tried to add a TypeConverter on my property, but no luck.

My Control

[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();
    }
}

The TypeConverter

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);
    }
}

What i want to do is to be able to write in my aspx page the following

<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" />

The first example works fine, the second fails with mentioned error. And no, i can't believe that the only way around this is to databind everytime, even for constant strings like

<my:param name="A object" value='<%# "this_is_just_a_string" %>' runat="server" />

解决方案

What about adding a ValueAsString property that is actually the one serialized and prevent Value from being serialized?

ValueAsString would then be responsible for serializing and deserializing Value appropriately (such as XML or JSON serialization).

Update It turns out that, due to the way that ASP.Net builds the controls, there isn't a way to do what you are looking to do.

It is possible to implement a mechanism to get object types, such as strings or internal classes, to be processed, but if you include primitive types such as Ints in the mix, the framework won't even execute the TypeConverter for your Object Value.

For example, in the following two cases (using VB syntax), both Params will be created, but the Value for Param1 will be nothing:

<cti:Param ID="Param1" Value="<%# CInt(1) %>" runat="server" />
<cti:Param ID="Param2" Value="test" runat="server" />

For completeness, here is how I was able to implement an object converter without having ASP.Net throw a fit after realizing that it always needs an actual class to instantiate as opposed to just an Object:

First, create an intermediate class that the underlying logic can instantiate. This class will have one constructor for each object type class that can be implemented:

public class MyObjectConverter
{
    public MyObjectConverter() : base()
    {
    }
    public MyObjectConverter(string oValue) : this()
    {
        this.Value = oValue;
    }

    public object Value { get; set; }

    public override string ToString()
    {
        if (this.Value == null)
        {
            return string.Empty;
        }
        else
        {
            return this.Value.ToString();
        }
    }
}

Next, modify the StringToObjectConverter's ConvertTo method so that it creates an instance of the new class using the appropriate constructor (an object constructor still confuses the code here):

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(MyObjectConverter).GetConstructor(new Type[] {value.GetType()}), new object[] {value});
    }

    return base.ConvertTo(context, culture, value, destinationType);
}

Finally, in the Param object, modify the Value property to handle the new type:

private object m_Value;
[TypeConverter(typeof(StringToObjectConverter))]
public object Value
{
    get
    {
        return m_Value;
    }
    set
    {
        if (value is MyObjectConverter)
        {
            m_Value = ((MyObjectConverter)value).Value;
        }
        else
        {
            m_Value = value;
        }
    }
}

I suppose it might be possible to generate dynamic classes or constructors or the object converter class, but that is not something that I have experience with, so I can't really speak to whether or not that is a viable solution.

So, to summarize, there are two issues with this approach:

1) You cannot use it for primitive data types. However, even using an Object Value property without bringing strings into the equation does not support this functionality.

2) You must explicitly define constructors for all of the data types that you wish to support.

这篇关于无法从它的字符串重新presentation创建类型的对象'System.Object的“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆