Tired. Annoyed. Really need help with Control serialization issues. Please...

S

Sky Sigal

I coming unglued... really need some help. 3 days chasing my tail all over
MSDN's documentation ...and I'm getting nowhere.

I have a problem with TypeConverters and
storage of expandableobjects to attributes in tags (think Style tag -> Style
object).

The problem that I am chasing is:

Html side:

<CC1:MyControl id=whatever Stuff="A:Some stuff;B:Another value;C:Bad hair
days"/>



The code behind is 3 classes:
* cStuff which is a complex property -- an instance of a 3 string class: (A,
B,C);
* StuffConverter -- a TypeConverter enherited from ExpandableObjectConverter
that converts an instance of cStuff to "key:value;key:value;key;value"...
(ie: Stuff="A:Some stuff;B:Another value;C:Bad hair days") and back again.
* MyControl -- a generic test control to see how it works.
* Use of the following attributes on various elements, trying to keep it
glued together:
// [NotifyParentProperty(true)]
//[Serializable(),TypeConverter(typeof(StuffConverter))]
// [PersistenceMode(PersistenceMode.Attribute)]
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]




It doesn't matter what I do -- this is not working as expected.
There are 2 areas of error:

a) In the IDE, it doesn't serialize the values back to the backend xml/html
attribute. I can edit both the backend tag -- or the IDE's property and it
renders it correctly in realtime. In other words it can string->object fine.
But it won't save it back to the html (serialize).

b) Problem 2 -- when I run it I get a runtime error //Unable to generate
code for a value of type 'XAct.Web.Controls.cStuff'. This error occurred
while trying to generate the property value for Stuff.



The closest match I have seen to a post/answer on the web is at
http://www.dotnet247.com/247reference/msgs/18/94895.aspx -- but I am not
sure if this applies since that was more complex and had to do with data
binding.

Can any kind soul take a look at this? The code below should contain all
parts and be able to be dropped into one *.cs file to compile and test as a
control.













using System;
using System.Web.UI.Design;
using System.Web;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Collections;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.WebControls;
//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/
html/vsnetpropbrow.asp



namespace XAct.Web.Controls {


//======================================
//'STUFF' CLASS DEF
//======================================

[Serializable(),TypeConverter(typeof(StuffConverter))]
public class cStuff{
private string _A=string.Empty;
private string _B=string.Empty;
private string _C=string.Empty;
[NotifyParentProperty(true)]
public string A {get {return _A;}set{_A=value;}}
[NotifyParentProperty(true),DefaultValue("Fish")]
public string B {get {return _B;}set{_B=value;}}
[NotifyParentProperty(true)]
public string C {get {return _C;}set{_C=value;}}
}


//======================================
//TEST CONTROL CLASS
//======================================
public class MyControl : WebControl {
public MyControl(){
this.Controls.Add(new LiteralControl("ANGRY TODAY:<br/>"));
}
private cStuff _Stuff = new cStuff();

//Variation Tried:
// [PersistenceMode(PersistenceMode.Attribute)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
// Accepts changes from IDE -- updates PropertySummary, updates
// Rendering -- fails to save as serialized attrib.
// Fails in IDE: "Unable to generate code for a value of type
// XAct.Web.Controls.cStuff'.
// This error occurred while trying to generate the
// property value for Stuff."

//Tried:
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
// Synchs IDE to Render -- doesn't Serialize, and Fails in RunTime

//Tried:
//[PersistenceMode(PersistenceMode.Attribute)]
// Synchs IDE to Render -- doesn't Serialize, and Fails in RunTime

//BTW: Works if I remove the stuff="" tag from the HTML it doesn't complain.
//In other words, it is failing in runtime when trying to parse the tag. Why
//it is failing to serialize in the ide, I don't know.


[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible )]
public cStuff Stuff {get {return _Stuff;}set{_Stuff=value;}}



protected override void Render(System.Web.UI.HtmlTextWriter output) {
//To see what it THINKS it should be:
StuffConverter oSC = new StuffConverter();
string tSerialized =
(string)oSC.ConvertTo(null,null,_Stuff,typeof(string));
output.Write("Onscreen Changes: " + this._Stuff.ToString() + ":" +
this._Stuff.A + ":" + ":[" + tSerialized + "]");
base.Render(output);
}
}


//======================================
//TYPE CONVERTER CLASS
//======================================
public class StuffConverter : ExpandableObjectConverter {
System.Type _Type = typeof(cStuff);
public override bool CanConvertFrom(ITypeDescriptorContext context, Type t)
{
if (t == typeof(string)) {return true;}
return base.CanConvertFrom(context, t);
}
//Convert From String:
public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo info,object value) {
if (value is string) {
try {
//_Type = context.PropertyDescriptor.PropertyType;
string[] tParts;
tParts = SplitPlus((string) value, ";");
object o = _Type.Assembly.CreateInstance(_Type.ToString());
string tKey = string.Empty;
string tValue = string.Empty;
int tPos = 0;
foreach (string tPart in tParts){
if (tPart == string.Empty){continue;}
tPos = tPart.IndexOf(':');
if (tPos == -1){
tKey = tPart;
tValue = string.Empty;
}else{
tKey = tPart.Substring(0,tPos);
tValue = tPart.Substring(tPos+1);
System.Reflection.PropertyInfo oPI =
_Type.GetProperty(tKey,BindingFlags.Instance | BindingFlags.Public |
System.Reflection.BindingFlags.IgnoreCase);
if (oPI != null){
oPI.SetValue(o,tValue,null);
}
}
}
return System.Convert.ChangeType(o,_Type);
}
catch (Exception E){
throw new Exception("MERDE!:::" + E.Message );
}
}
return base.ConvertFrom(context, info, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo
culture, object value, Type destType) {
//Works in IDE _Type = context.PropertyDescriptor.PropertyType;
object o=null;
try {
o = System.Convert.ChangeType(value,_Type);
}catch{}
if ((o !=null) && (destType == typeof(string))) {
string tResult = string.Empty;
/*
tResult += "a:" + context.Instance.GetType().ToString() + "|";
tResult += "b:" + context.GetType().ToString() + "|";
tResult += "c:" + context.Container.ToString() + "|";
tResult += "d:" + context.ToString () + "|";
tResult += "e:" + context.PropertyDescriptor.Converter.ToString() + "|";
tResult += "f:" + context.PropertyDescriptor.PropertyType.ToString() + "|";

a:XAct.Web.Controls.LoginPanel|
b:System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry|
c:Microsoft.VisualStudio.Designer.Host.DesignerHost|
d:System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry
Captions|
e:XAct.Web.Controls.StuffConverter|
f:XAct.Web.Controls.LoginPanel+cCaptions
*/

string tDivChar = string.Empty;
PropertyInfo[] oPIs = _Type.GetProperties();
foreach(PropertyInfo oPI in oPIs){
string tKey = oPI.Name;
try {
object oVal = oPI.GetValue(o,null);
string tValue = string.Empty;
if (oVal != null){
tValue = oVal.ToString();
if (tValue != string.Empty){tResult += tDivChar + tKey + ":" + tValue;}
if (tDivChar == string.Empty){tDivChar = ";";}
}
}
catch{}
}
return tResult;
}
return base.ConvertTo(context, culture, value, destType);
}

public override bool
GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext
context){
return false;
}

/// <summary>
/// Split function that only splits if not within brackets or quotes.
/// </summary>
/// <param name="qString"></param>
/// <param name="qDivChar"></param>
/// <returns></returns>
public static string[] SplitPlus(string qString, string qDivChar) {
if (qDivChar== String.Empty){qDivChar = ",";}
ArrayList tResults = new ArrayList();
string tChar="";
string tWord = "";
bool tEscaped=false;
string tLastChar = "";
System.Collections.Stack tQuotes=new System.Collections.Stack();
for (int i=0;i<qString.Length;i++) {
tChar = qString.ToString();
if (tQuotes.Count == 0) {
//We are outside of quotes, so look for quote beginnings...
if ((tChar == "(") ||
(tChar == "{") ||
(tChar == "[") ||
(tChar == "'") ||
(tChar == "\"")) {
tQuotes.Push(tChar);
tLastChar=tChar;
}
if ((tChar == qDivChar)) {
tResults.Add(tWord);
tWord="";
tChar = "";
}
}
else {
//We are within quotes...need to look for close chars:
if (tEscaped ==false) {
if (tChar == "\\") {tEscaped=true;}
else {
tLastChar =(string)tQuotes.Peek();
if ((tChar == "\"") && (tChar == tLastChar)) {
tQuotes.Pop();
}
else if ((tChar == "\'") && (tChar == tLastChar)) {
tQuotes.Pop();
}
else if ((tChar == "]") && (tLastChar == "[")) {
tQuotes.Pop();
}
else if ((tChar == "}") && (tLastChar == "{")) {
tQuotes.Pop();
}
if ((tChar == ")") && (tLastChar == "(")) {
tQuotes.Pop();
}
}
}
else {
tEscaped = false;
}
}
tWord = tWord+tChar;
}
if (tWord!= String.Empty) {tResults.Add(tWord);}
return (string[])tResults.ToArray(typeof(string));
}
}//Class:End




}
 
J

John Saunders

Sky Sigal said:
I coming unglued... really need some help. 3 days chasing my tail all over
MSDN's documentation ...and I'm getting nowhere.

I have a problem with TypeConverters and
storage of expandableobjects to attributes in tags (think Style tag -> Style
object).

The problem that I am chasing is:

Html side:

<CC1:MyControl id=whatever Stuff="A:Some stuff;B:Another value;C:Bad hair
days"/>



The code behind is 3 classes:
* cStuff which is a complex property -- an instance of a 3 string class: (A,
B,C);
* StuffConverter -- a TypeConverter enherited from ExpandableObjectConverter
that converts an instance of cStuff to "key:value;key:value;key;value"...
(ie: Stuff="A:Some stuff;B:Another value;C:Bad hair days") and back again.
* MyControl -- a generic test control to see how it works.
* Use of the following attributes on various elements, trying to keep it
glued together:
// [NotifyParentProperty(true)]
//[Serializable(),TypeConverter(typeof(StuffConverter))]
// [PersistenceMode(PersistenceMode.Attribute)]
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]




It doesn't matter what I do -- this is not working as expected.
There are 2 areas of error:

a) In the IDE, it doesn't serialize the values back to the backend xml/html
attribute. I can edit both the backend tag -- or the IDE's property and it
renders it correctly in realtime. In other words it can string->object fine.
But it won't save it back to the html (serialize).

b) Problem 2 -- when I run it I get a runtime error //Unable to generate
code for a value of type 'XAct.Web.Controls.cStuff'. This error occurred
while trying to generate the property value for Stuff.



The closest match I have seen to a post/answer on the web is at
http://www.dotnet247.com/247reference/msgs/18/94895.aspx -- but I am not
sure if this applies since that was more complex and had to do with data
binding.

Can any kind soul take a look at this? The code below should contain all
parts and be able to be dropped into one *.cs file to compile and test as a
control.

If I were you, I'd try simplifying the cStuff class down to a single string,
and with the TypeConverter attributes removed. If that works, try adding the
TypeConverter attributes, but change the TypeConverter to handle only a
single string. If that works, add one more string...
 
S

Sky Sigal

Fair enough answer :) ...although I thought that the example I posted WAS
the simpler version of what I am trying to get at in the long run...:)

To rephrase(?) -- what I think I am fishing around for is a definite answer
to the following question:

a) I've seen that
[PersistenceMode(PersistenceMode.InnerProperty)]

[TypeConverter(typeof(StuffConverter))]
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

will work, putting into the html/xml of the control stuff like:
<mycontrol
<MyProp
MySubPropA="Uno"
MySubPropA="Dos"
/>
....
/>

And I've seen that this can be set to .Attribute
[PersistenceMode(PersistenceMode.Attribute)]

if the propery is a non-complex type (string/int, etc).


But can it -- for SURE (ie bug is definatly on my side rather than a
limitation of the Framework) be that a complex type can be stored as an
attribute?

That's what I am trying to see. I think...(Could be a totally different
thing).

For example -- I am very mystified as to why the error happens at run-time.
Almost as if it is trying to serialize/send this to the outgoing HTML,
rather than take care of it in the IDE. Or is that it is hitting the
exception in the IDE -- but not flagging it as it is a different thread --
but that is why the IDE is not saving the changes back to the XML/HTML of
the control?


Voila.

If so -- could it be that the [Serialize()] attribute has nothing to do with
what I am working with and should be removed? Or am I missing a second half?
Some method that Serialize() will be looking for at run-time?


But yes -- today or this weekend I will try once again -- with an even
simpler TypeConverter -- something that parses a simpler string
"X=100;Y=200" or something like that...

Thanks for getting back to me btw :)
Sky




John Saunders said:
Sky Sigal said:
I coming unglued... really need some help. 3 days chasing my tail all over
MSDN's documentation ...and I'm getting nowhere.

I have a problem with TypeConverters and
storage of expandableobjects to attributes in tags (think Style tag -> Style
object).

The problem that I am chasing is:

Html side:

<CC1:MyControl id=whatever Stuff="A:Some stuff;B:Another value;C:Bad hair
days"/>



The code behind is 3 classes:
* cStuff which is a complex property -- an instance of a 3 string class: (A,
B,C);
* StuffConverter -- a TypeConverter enherited from ExpandableObjectConverter
that converts an instance of cStuff to "key:value;key:value;key;value"...
(ie: Stuff="A:Some stuff;B:Another value;C:Bad hair days") and back again.
* MyControl -- a generic test control to see how it works.
* Use of the following attributes on various elements, trying to keep it
glued together:
// [NotifyParentProperty(true)]
//[Serializable(),TypeConverter(typeof(StuffConverter))]
// [PersistenceMode(PersistenceMode.Attribute)]
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
It doesn't matter what I do -- this is not working as expected.
There are 2 areas of error:

a) In the IDE, it doesn't serialize the values back to the backend xml/html
attribute. I can edit both the backend tag -- or the IDE's property and it
renders it correctly in realtime. In other words it can string->object fine.
But it won't save it back to the html (serialize).

b) Problem 2 -- when I run it I get a runtime error //Unable to generate
code for a value of type 'XAct.Web.Controls.cStuff'. This error occurred
while trying to generate the property value for Stuff.



The closest match I have seen to a post/answer on the web is at
http://www.dotnet247.com/247reference/msgs/18/94895.aspx -- but I am not
sure if this applies since that was more complex and had to do with data
binding.

Can any kind soul take a look at this? The code below should contain all
parts and be able to be dropped into one *.cs file to compile and test
as
a
control.

If I were you, I'd try simplifying the cStuff class down to a single string,
and with the TypeConverter attributes removed. If that works, try adding the
TypeConverter attributes, but change the TypeConverter to handle only a
single string. If that works, add one more string...
 
J

John Saunders

Sky Sigal said:
Fair enough answer :) ...although I thought that the example I posted WAS
the simpler version of what I am trying to get at in the long run...:)

To rephrase(?) -- what I think I am fishing around for is a definite answer
to the following question:

a) I've seen that
[PersistenceMode(PersistenceMode.InnerProperty)]

[TypeConverter(typeof(StuffConverter))]
[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

will work, putting into the html/xml of the control stuff like:
<mycontrol
<MyProp
MySubPropA="Uno"
MySubPropA="Dos"
/>
...
/>

And I've seen that this can be set to .Attribute
[PersistenceMode(PersistenceMode.Attribute)]

if the propery is a non-complex type (string/int, etc).


But can it -- for SURE (ie bug is definatly on my side rather than a
limitation of the Framework) be that a complex type can be stored as an
attribute?

That's what I am trying to see. I think...(Could be a totally different
thing).

For example -- I am very mystified as to why the error happens at run-time.
Almost as if it is trying to serialize/send this to the outgoing HTML,
rather than take care of it in the IDE. Or is that it is hitting the
exception in the IDE -- but not flagging it as it is a different thread --
but that is why the IDE is not saving the changes back to the XML/HTML of
the control?


Voila.

If so -- could it be that the [Serialize()] attribute has nothing to do with
what I am working with and should be removed? Or am I missing a second half?
Some method that Serialize() will be looking for at run-time?


But yes -- today or this weekend I will try once again -- with an even
simpler TypeConverter -- something that parses a simpler string
"X=100;Y=200" or something like that...

Try simpler than that. Just a single string, no parsing or anything else.
First find out if a single string can be persisted, then get fancier from
there.
--
John Saunders
johnwsaundersiii at hotmail

John Saunders said:
Sky Sigal said:
I coming unglued... really need some help. 3 days chasing my tail all over
MSDN's documentation ...and I'm getting nowhere.

I have a problem with TypeConverters and
storage of expandableobjects to attributes in tags (think Style tag -> Style
object).

The problem that I am chasing is:

Html side:

<CC1:MyControl id=whatever Stuff="A:Some stuff;B:Another value;C:Bad hair
days"/>



The code behind is 3 classes:
* cStuff which is a complex property -- an instance of a 3 string
class:
(A,
B,C);
* StuffConverter -- a TypeConverter enherited from ExpandableObjectConverter
that converts an instance of cStuff to "key:value;key:value;key;value"...
(ie: Stuff="A:Some stuff;B:Another value;C:Bad hair days") and back again.
* MyControl -- a generic test control to see how it works.
* Use of the following attributes on various elements, trying to keep it
glued together:
// [NotifyParentProperty(true)]
//[Serializable(),TypeConverter(typeof(StuffConverter))]
// [PersistenceMode(PersistenceMode.Attribute)]
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
and
it as

If I were you, I'd try simplifying the cStuff class down to a single string,
and with the TypeConverter attributes removed. If that works, try adding the
TypeConverter attributes, but change the TypeConverter to handle only a
single string. If that works, add one more string...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top