Hi, i'm working in a form generation tool=2E I've got a base class=
TpForm to build all the windows in the application and now i=
want to be able to design subforms, but the problem is that=
these subforms are downloaded by reflection from different=
repositories in design and run-time; so, what i've done is build=
a custom TpFormSerializer that replace the codeDOM object=
creation expresion in InitializeComponent(), and attachi it to=
TpForm class=2E For example, if i have the line
this=2ESUBV194i1 =3D new=
Fujitsu=2ETpnet=2EUser=2EInterface=2EWin=2ESUBV194i1()
this would be replaced with
this=2ESUBV194i1 =3D AddSubForm("SUBV194i1", "SUBV194i1");
and i also change the subform declaration statement, so subform=
type dependence is not neccesary in the designer project, in=
this way:
private Fujitsu=2ETpnet=2EUser=2EInterface=2EWin=2ESUBV194I1=
SUBV194i1;
with
private TpForm SUBV194i1;
The problem is that in the deserialize method i simply invoke the=
base serialization manager and it seems that the IDesignerHost's=
CreateComponent method is not able to create the subform at=
startup,
am i missing something? Do i have to custom the deserialization=
process as well in any manner?
using System;
using System=2ECodeDom;
using System=2ECodeDom=2ECompiler;
using System=2EComponentModel;
using System=2EComponentModel=2EDesign;
using System=2EComponentModel=2EDesign=2ESerialization;
using System=2EDiagnostics;
using System=2EDrawing;
using System=2EReflection;
using System=2EWindows=2EForms;
using System=2EText;
using System=2EIO;
using Microsoft=2ECSharp;
using Fujitsu=2ETpnet=2ECommon;
namespace Fujitsu=2ETpnet=2EUser=2EInterface=2EDesign
{
=09/// <summary>
=09/// Serializador personalizado del tipo TpForm=2E
=09/// </summary>
=09public class TpFormSerializer : CodeDomSerializer
=09{
=09=09private IDesignerSerializationManager _manager =3D null;
=09=09public override object Serialize(IDesignerSerializationManager=
manager, object value)
=09=09{
=09=09=09CodeDomSerializer baseSerializer =3D null;
=09=09=09this=2E_manager =3D manager;
=09=09=09object codeObject =3D null;
=09=09=09try
=09=09=09{
=09=09=09=09//Obtener el serializador por defecto
=09=09=09=09baseSerializer =3D=
(CodeDomSerializer)manager=2EGetSerializer(typeof(UserControl),=
typeof(CodeDomSerializer));
=09=09=09=09//Invocar al serializador por defecto
=09=09=09=09codeObject =3D baseSerializer=2ESerialize(manager, value);=09=09=09
=09=09=09=09//Obtenemos el nombre del objeto (ventana)
=09=09=09=09string name =3D=
value=2EGetType()=2EGetProperty("Name")=2EGetValue(value,=
null)=2EToString();
=09=09=09=09//Obtenemos la declaraci?n del tipo del contexto
=09=09=09=09CodeTypeDeclaration typeDeclaration =3D=
(CodeTypeDeclaration)manager=2EContext[typeof(CodeTypeDeclaration)=
];
=09=09=09=09//Interceptamos el ?rbol de c?digo que declara el objeto
=09=09=09=09TraceCode(typeDeclaration);
=09=09=09=09typeDeclaration =3D=
(CodeTypeDeclaration)SerializeFormDeclaration(typeDeclaration,=
name);
=09=09=09=09TraceCode(typeDeclaration);
=09=09=09=09//Interceptamos el ?rbol de c?digo que crea el objeto
=09=09=09=09codeObject =3D SerializeFormCreation(codeObject, name);
=09=09=09}
=09=09=09catch(Exception e)
=09=09=09{
=09=09=09=09MessageBox=2EShow("iTool - Error serializando ventana: {" +=
e=2EMessage + "}");
=09=09=09}
=09=09=09return codeObject;
=09=09}
=09=09public override object=
Deserialize(IDesignerSerializationManager manager, object=
codeObject)
=09=09{
=09=09=09object objResp =3D null;
//=09=09=09try
//=09=09=09{
=09=09=09=09//Obtener el serializador por defecto
=09=09=09=09CodeDomSerializer baseSerializer =3D=
(CodeDomSerializer)manager=2EGetSerializer(typeof(UserControl),=
typeof(CodeDomSerializer));
=09=09=09=09TraceCode(codeObject);
=09=09=09=09CodeTypeDeclaration typeDeclaration =3D=
(CodeTypeDeclaration)manager=2EContext[typeof(CodeTypeDeclaration)=
];
=09=09=09=09//Invocar la deserializaci?n b?sica
=09=09=09=09objResp =3D baseSerializer=2EDeserialize(manager, codeObject);
//=09=09=09}
//=09=09=09catch(Exception e)
//=09=09=09{
//=09=09=09=09MessageBox=2EShow("iTool - Error deserializando ventana: {" +=
e=2EMessage + "}");
//=09=09=09}
=09=09=09return objResp;
=09=09}
=09=09
=09=09#region Private methods
=09
=09=09private object SerializeFormCreation(object codeObject, string=
name)
=09=09{
=09=09=09CodeStatementCollection tree =3D=
(CodeStatementCollection)codeObject;
=09=09=09foreach(CodeStatement statement in tree)
=09=09=09{
=09=09=09=09if(CheckForCreationStatement(statement, name))
=09=09=09=09{
=09=09=09=09=09//Creaci?n de la llamada al m?todo personalizado que crea la=
ventana
=09=09=09=09=09CodeMethodInvokeExpression methodInvoke =3D new=
CodeMethodInvokeExpression(
=09=09=09=09=09=09new CodeThisReferenceExpression(),
=09=09=09=09=09=09"AddSubForm",
=09=09=09=09=09=09new CodeExpression[] {new CodePrimitiveExpression(name),=
new CodePrimitiveExpression(name)});
=09=09=09=09=09//Reemplazar parte derecha de la sentencia de asignaci?n
=09=09=09=09=09((CodeAssignStatement)statement)=2ERight =3D methodInvoke;
=09=09=09=09=09break;
=09=09=09=09}
=09=09=09}
=09=09=09//Retornar arb?l de c?digo de la ventana
=09=09=09return codeObject;
=09=09}
=09
=09=09private object SerializeFormDeclaration(object codeObject,=
string name)
=09=09{
=09=09=09if(codeObject is CodeTypeDeclaration)
=09=09=09{
=09=09=09=09string comment =3D "El c?digo de esta pantalla ha sido generado=
de forma autom?tica con la herramienta ";
=09=09=09=09comment +=3D "\"iTool - Dise?ador de ventanas\"";
=09=09=09=09CodeTypeDeclaration tree =3D (CodeTypeDeclaration)codeObject;
=09=09=09=09tree=2EComments=2EClear();
=09=09=09=09tree=2EComments=2EAdd(new CodeCommentStatement(comment));=09=09=09=09
=09=09=09=09foreach(CodeMemberField field in tree=2EMembers)
=09=09=09=09{
=09=09=09=09=09if(CheckForCodeMemberField(field, name))
=09=09=09=09=09{=09=09=09=09=09=09
=09=09=09=09=09=09field=2EType =3D new CodeTypeReference("TpForm");
=09=09=09=09=09=09break;
=09=09=09=09=09}
=09=09=09=09}
=09=09=09}
=09=09=09return codeObject;
=09=09}
=09=09
=09=09private bool CheckForCreationStatement(object codeObject,=
string name)
=09=09{=09
=09=09=09bool bResult =3D false;
=09=09=09try
=09=09=09{
=09=09=09=09if(codeObject is CodeAssignStatement) =09=09=09=09=09
=09=09=09=09{=09=09=09=09=09
=09=09=09=09=09CodeExpression left =3D=
((CodeAssignStatement)codeObject)=2ELeft;
=09=09=09=09=09if(left is CodeFieldReferenceExpression)
=09=09=09=09=09{
=09=09=09=09=09=09if(((CodeFieldReferenceExpression)left)=2EFieldName=2EEquals(na=
me))
=09=09=09=09=09=09=09return true;
=09=09=09=09=09}=09=09=09=09=09
=09=09=09=09}
=09=09=09}
=09=09=09catch(Exception e)
=09=09=09{
=09=09=09=09bResult =3D false;
=09=09=09=09MessageBox=2EShow("iTool - Error serializando ventana: {" +=
e=2EMessage + "}");
=09=09=09}
=09=09=09return bResult;
=09=09}
=09=09
=09=09private bool CheckForCodeMemberField(object codeObject, string=
name)
=09=09{
=09=09=09bool bResult =3D false;
=09=09=09string comment =3D "(iTool) Declaraci?n de subventana -" + name=
+ "-";
=09=09=09try
=09=09=09{
=09=09=09=09if(codeObject is CodeMemberField)
=09=09=09=09{
=09=09=09=09=09CodeMemberField field =3D (CodeMemberField)codeObject;
=09=09=09=09=09if(field=2EName=2EEquals(name))
=09=09=09=09=09{
=09=09=09=09=09=09field=2EComments=2EClear();
=09=09=09=09=09=09field=2EComments=2EAdd(new CodeCommentStatement(comment));
=09=09=09=09=09=09return true;
=09=09=09=09=09}
=09=09=09=09}
=09=09=09}
=09=09=09catch(Exception e)
=09=09=09{
=09=09=09=09bResult =3D false;
=09=09=09=09MessageBox=2EShow("iTool - Error serializando ventana: {" +=
e=2EMessage + "}");
=09=09=09}
=09=09=09return bResult;
=09=09}
=09=09#endregion
=09}
}
-----------------------
Posted by a user from =2ENET 247 (http://www=2Edotnet247=2Ecom/)
<Id>BwC+U94s9Um6CR3dmqlx/Q=3D=3D</Id>