By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,780 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,780 IT Pros & Developers. It's quick & easy.

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

P: n/a
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(DesignerSerializat ionVisibility.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/247referenc.../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(DesignerSerializat ionVisibility.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(DesignerSerializat ionVisibility.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(DesignerSerializat ionVisibility.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(stri ng));
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.Proper tyDescriptorGridEntry|
c:Microsoft.VisualStudio.Designer.Host.DesignerHos t|
d:System.Windows.Forms.PropertyGridInternal.Proper tyDescriptorGridEntry
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.I TypeDescriptorContext
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[i].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


}

Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Sky Sigal" <as*******@xact-solutions.removethis.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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(DesignerSerializat ionVisibility.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/247referenc.../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...
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #2

P: n/a
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(DesignerSerializat ionVisibility.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" <jo**************@notcoldmail.com> wrote in message
news:um**************@TK2MSFTNGP10.phx.gbl...
"Sky Sigal" <as*******@xact-solutions.removethis.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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(DesignerSerializat ionVisibility.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/247referenc.../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...
--
John Saunders
johnwsaundersiii at hotmail

Nov 18 '05 #3

P: n/a
"Sky Sigal" <as*******@xact-solutions.removethis.com> wrote in message
news:ui**************@tk2msftngp13.phx.gbl...
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(DesignerSerializat ionVisibility.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" <jo**************@notcoldmail.com> wrote in message
news:um**************@TK2MSFTNGP10.phx.gbl...
"Sky Sigal" <as*******@xact-solutions.removethis.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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(DesignerSerializat ionVisibility.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/247referenc.../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...
--
John Saunders
johnwsaundersiii at hotmail


Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.