R
Rolf Welskes
Hello,
the problem seems to be complex and is in all developments of web-controls
which uses own TypeConverter.
For this I have here a simple demo-program of the problem:
The Control-code: A class MyString which is a class which is similar to a
string
[TypeConverter(typeof(MyStringTypeConverter))]
public class MyString
{
string str;
public MyString(string str) { this.str = str; }
public override string ToString() { return str; }
public string Str
{
get{ return str; }
set { str = value;}
}
}
the typeconverter used:
class MyStringTypeConverter : TypeConverter
{
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(string))
{return true;}
else if (destinationType == typeof(InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
return new MyString((string)value);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
if (destinationType == typeof(string))
{
MyString cls = (MyString)value;
return cls.ToString();
}
else if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(MyString).GetConstructor(new
Type[] { typeof(string) });
MyString cls = (MyString)value;
return new InstanceDescriptor(ci, new object[] {
cls.ToString() });
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
the control-class:
[DefaultProperty("Text")]
[ToolboxData("<{0}:TestCtrl02 runat=server></{0}:TestCtrl02>")]
public class TestCtrl02 : Control
{
[Bindable(true), Category("Appearance"),Localizable(true)]
public MyString Text
{
get{
MyString s = (MyString)ViewState["Text"];
return ((s == null) ? new MyString("") : s); }
set
{ ViewState["Text"] = value; }
}
protected override void Render(HtmlTextWriter writer)
............
}
using in a page:
.............
<cc1:TestCtrl02 ID="testCtrl" runat="server" Text="my text" >
</cc1:TestCtrl02>
...................
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ok seems many code, but is the shortest possible to see the problem.
The problem:
The program is compiled, we now
start Visual Studio.
Now if we go in the html-view of the page we see:
<cc1:TestCtrl02 ID="testCtrl" runat="server" Text="my text" >
</cc1:TestCtrl02>
Now we change to the design view:
the property Text now has in property view under property Text 'my text'.
If we change the text in the property and go to the html-view all is fine.
So, all works fine.
NOW: We make a RE-build of the project. Nothing more.
Now we open the page-file in html-view. All is fine.
Now we change to the design view: Error Creating Control 'my text' cannot be
set on property Text.
If we now close Visual Studio and start it again, All works fine.
If we now delete the text, and make a RE-build, so we have
<cc1:TestCtrl02 ID="testCtrl" runat="server" >
</cc1:TestCtrl02>
we can go to design view without problems.
If we now enter in the Text-property : my text we get an error:
Property value is not valid: MyString cannot be converted to type MyString.
We now close Visual Studio and start it again and do the same.
Now it works fine.
So I know such problem allway are there when I use TypeConverter I think the
following:
After a rebuild the own type converters are not seen from the visual studio
environment.
So what maybe the reason, what can be a solution.
Developing web-controls with typconverters and to start Visual Studio after
each rebuild is impossible.
sorry it's much text, but the problem is fundamental and very important for
me.
Thank you for any help.
Rolf Welskes
the problem seems to be complex and is in all developments of web-controls
which uses own TypeConverter.
For this I have here a simple demo-program of the problem:
The Control-code: A class MyString which is a class which is similar to a
string
[TypeConverter(typeof(MyStringTypeConverter))]
public class MyString
{
string str;
public MyString(string str) { this.str = str; }
public override string ToString() { return str; }
public string Str
{
get{ return str; }
set { str = value;}
}
}
the typeconverter used:
class MyStringTypeConverter : TypeConverter
{
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(string))
{return true;}
else if (destinationType == typeof(InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
return new MyString((string)value);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
if (destinationType == typeof(string))
{
MyString cls = (MyString)value;
return cls.ToString();
}
else if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(MyString).GetConstructor(new
Type[] { typeof(string) });
MyString cls = (MyString)value;
return new InstanceDescriptor(ci, new object[] {
cls.ToString() });
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
the control-class:
[DefaultProperty("Text")]
[ToolboxData("<{0}:TestCtrl02 runat=server></{0}:TestCtrl02>")]
public class TestCtrl02 : Control
{
[Bindable(true), Category("Appearance"),Localizable(true)]
public MyString Text
{
get{
MyString s = (MyString)ViewState["Text"];
return ((s == null) ? new MyString("") : s); }
set
{ ViewState["Text"] = value; }
}
protected override void Render(HtmlTextWriter writer)
............
}
using in a page:
.............
<cc1:TestCtrl02 ID="testCtrl" runat="server" Text="my text" >
</cc1:TestCtrl02>
...................
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ok seems many code, but is the shortest possible to see the problem.
The problem:
The program is compiled, we now
start Visual Studio.
Now if we go in the html-view of the page we see:
<cc1:TestCtrl02 ID="testCtrl" runat="server" Text="my text" >
</cc1:TestCtrl02>
Now we change to the design view:
the property Text now has in property view under property Text 'my text'.
If we change the text in the property and go to the html-view all is fine.
So, all works fine.
NOW: We make a RE-build of the project. Nothing more.
Now we open the page-file in html-view. All is fine.
Now we change to the design view: Error Creating Control 'my text' cannot be
set on property Text.
If we now close Visual Studio and start it again, All works fine.
If we now delete the text, and make a RE-build, so we have
<cc1:TestCtrl02 ID="testCtrl" runat="server" >
</cc1:TestCtrl02>
we can go to design view without problems.
If we now enter in the Text-property : my text we get an error:
Property value is not valid: MyString cannot be converted to type MyString.
We now close Visual Studio and start it again and do the same.
Now it works fine.
So I know such problem allway are there when I use TypeConverter I think the
following:
After a rebuild the own type converters are not seen from the visual studio
environment.
So what maybe the reason, what can be a solution.
Developing web-controls with typconverters and to start Visual Studio after
each rebuild is impossible.
sorry it's much text, but the problem is fundamental and very important for
me.
Thank you for any help.
Rolf Welskes