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