1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
|
//
// System.ComponentModel.Design.Serialization.CodeDomSerializerBase
//
// Authors:
// Ivan N. Zlatev (contact i-nZ.net)
//
// (C) 2007 Ivan N. Zlatev
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Reflection;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.CodeDom;
namespace System.ComponentModel.Design.Serialization
{
[EditorBrowsable (EditorBrowsableState.Never)]
public abstract class CodeDomSerializerBase
{
// MSDN says that if the deserialization fails a CodeExpression is returned
//
private sealed class DeserializationErrorMarker : CodeExpression
{
public override bool Equals (object o)
{
return false;
}
public override int GetHashCode ()
{
return base.GetHashCode ();
}
}
private static readonly DeserializationErrorMarker _errorMarker = new DeserializationErrorMarker ();
internal CodeDomSerializerBase ()
{
}
private class ExpressionTable : Hashtable // just so that we have a specific type to append to the context stack
{
}
protected CodeExpression SerializeToExpression (IDesignerSerializationManager manager, object value)
{
if (manager == null)
throw new ArgumentNullException ("manager");
CodeExpression expression = null;
if (value != null)
expression = this.GetExpression (manager, value); // 1 - IDesignerSerializationManager.GetExpression
if (expression == null) {
CodeDomSerializer serializer = this.GetSerializer (manager, value); // 2 - manager.GetSerializer().Serialize()
if (serializer != null) {
object serialized = serializer.Serialize (manager, value);
expression = serialized as CodeExpression; // 3 - CodeStatement or CodeStatementCollection
if (expression == null) {
CodeStatement statement = serialized as CodeStatement;
CodeStatementCollection statements = serialized as CodeStatementCollection;
if (statement != null || statements != null) {
CodeStatementCollection contextStatements = null;
StatementContext context = manager.Context[typeof (StatementContext)] as StatementContext;
if (context != null && value != null)
contextStatements = context.StatementCollection[value];
if (contextStatements == null)
contextStatements = manager.Context[typeof (CodeStatementCollection)] as CodeStatementCollection;
if (contextStatements != null) {
if (statements != null)
contextStatements.AddRange (statements);
else
contextStatements.Add (statement);
}
}
}
if (expression == null && value != null)
expression = this.GetExpression (manager, value); // 4
} else {
ReportError (manager, "No serializer found for type '" + value.GetType ().Name + "'");
}
}
return expression;
}
protected CodeDomSerializer GetSerializer (IDesignerSerializationManager manager, object value)
{
DesignerSerializerAttribute attrInstance, attrType;
attrType = attrInstance = null;
CodeDomSerializer serializer = null;
if (value == null)
serializer = this.GetSerializer (manager, null);
else {
AttributeCollection attributes = TypeDescriptor.GetAttributes (value);
foreach (Attribute a in attributes) {
DesignerSerializerAttribute designerAttr = a as DesignerSerializerAttribute;
if (designerAttr != null && manager.GetType (designerAttr.SerializerBaseTypeName) == typeof (CodeDomSerializer)) {
attrInstance = designerAttr;
break;
}
}
attributes = TypeDescriptor.GetAttributes (value.GetType ());
foreach (Attribute a in attributes) {
DesignerSerializerAttribute designerAttr = a as DesignerSerializerAttribute;
if (designerAttr != null && manager.GetType (designerAttr.SerializerBaseTypeName) == typeof (CodeDomSerializer)) {
attrType = designerAttr;
break;
}
}
// if there is metadata modification in the instance then create the specified serializer instead of the one
// in the Type.
if (attrType != null && attrInstance != null && attrType.SerializerTypeName != attrInstance.SerializerTypeName)
serializer = Activator.CreateInstance (manager.GetType (attrInstance.SerializerTypeName)) as CodeDomSerializer;
else
serializer = this.GetSerializer (manager, value.GetType ());
}
return serializer;
}
protected CodeDomSerializer GetSerializer (IDesignerSerializationManager manager, Type valueType)
{
return manager.GetSerializer (valueType, typeof (CodeDomSerializer)) as CodeDomSerializer;
}
protected CodeExpression GetExpression (IDesignerSerializationManager manager, object value)
{
if (manager == null)
throw new ArgumentNullException ("manager");
if (value == null)
throw new ArgumentNullException ("value");
CodeExpression expression = null;
ExpressionTable expressions = manager.Context[typeof (ExpressionTable)] as ExpressionTable;
if (expressions != null) // 1st try: ExpressionTable
expression = expressions [value] as CodeExpression;
if (expression == null) { // 2nd try: RootContext
RootContext context = manager.Context[typeof (RootContext)] as RootContext;
if (context != null && context.Value == value)
expression = context.Expression;
}
if (expression == null) { // 3rd try: IReferenceService (instance.property.property.property
string name = manager.GetName (value);
if (name == null || name.IndexOf (".") == -1) {
IReferenceService service = manager.GetService (typeof (IReferenceService)) as IReferenceService;
if (service != null) {
name = service.GetName (value);
if (name != null && name.IndexOf (".") != -1) {
string[] parts = name.Split (new char[] { ',' });
value = manager.GetInstance (parts[0]);
if (value != null) {
expression = SerializeToExpression (manager, value);
if (expression != null) {
for (int i=1; i < parts.Length; i++)
expression = new CodePropertyReferenceExpression (expression, parts[i]);
}
}
}
}
}
}
return expression;
}
protected void SetExpression (IDesignerSerializationManager manager, object value, CodeExpression expression)
{
SetExpression (manager, value, expression, false);
}
// XXX: isPreset - what does this do when set?
//
protected void SetExpression (IDesignerSerializationManager manager, object value, CodeExpression expression, bool isPreset)
{
if (manager == null)
throw new ArgumentNullException ("manager");
if (value == null)
throw new ArgumentNullException ("value");
if (expression == null)
throw new ArgumentNullException ("expression");
ExpressionTable expressions = manager.Context[typeof (ExpressionTable)] as ExpressionTable;
if (expressions == null) {
expressions = new ExpressionTable ();
manager.Context.Append (expressions);
}
expressions[value] = expression;
}
protected bool IsSerialized (IDesignerSerializationManager manager, object value)
{
return this.IsSerialized (manager, value, false);
}
// XXX: What should honorPreset do?
protected bool IsSerialized (IDesignerSerializationManager manager, object value, bool honorPreset)
{
if (value == null)
throw new ArgumentNullException ("value");
if (manager == null)
throw new ArgumentNullException ("manager");
if (this.GetExpression (manager, value) != null)
return true;
else
return false;
}
protected CodeExpression SerializeCreationExpression (IDesignerSerializationManager manager, object value, out bool isComplete)
{
if (value == null)
throw new ArgumentNullException ("value");
if (manager == null)
throw new ArgumentNullException ("manager");
CodeExpression expression = null;
TypeConverter converter = TypeDescriptor.GetConverter (value);
if (converter != null && converter.CanConvertTo (typeof (InstanceDescriptor))) {
InstanceDescriptor descriptor = converter.ConvertTo (value, typeof (InstanceDescriptor)) as InstanceDescriptor;
isComplete = descriptor.IsComplete;
if (descriptor != null && descriptor.MemberInfo != null)
expression = this.SerializeInstanceDescriptor (manager, descriptor);
else
ReportError (manager, "Unable to serialize to InstanceDescriptor",
"Value Type: " + value.GetType ().Name + System.Environment.NewLine +
"Value (ToString): " + value.ToString ());
} else {
if (value.GetType().GetConstructor (Type.EmptyTypes) != null)
expression = new CodeObjectCreateExpression (value.GetType ().FullName, new CodeExpression[0]);
isComplete = false;
}
return expression;
}
private CodeExpression SerializeInstanceDescriptor (IDesignerSerializationManager manager, InstanceDescriptor descriptor)
{
CodeExpression expression = null;
MemberInfo member = descriptor.MemberInfo;
CodeExpression target = new CodeTypeReferenceExpression (member.DeclaringType);
if (member is PropertyInfo) {
expression = new CodePropertyReferenceExpression (target, member.Name);
} else if (member is FieldInfo) {
expression = new CodeFieldReferenceExpression (target, member.Name);
} else if (member is MethodInfo) {
CodeMethodInvokeExpression methodInvoke = new CodeMethodInvokeExpression (target, member.Name);
if (descriptor.Arguments != null && descriptor.Arguments.Count > 0)
methodInvoke.Parameters.AddRange (SerializeParameters (manager, descriptor.Arguments));
expression = methodInvoke;
} else if (member is ConstructorInfo) {
CodeObjectCreateExpression createExpr = new CodeObjectCreateExpression (member.DeclaringType);
if (descriptor.Arguments != null && descriptor.Arguments.Count > 0)
createExpr.Parameters.AddRange (SerializeParameters (manager, descriptor.Arguments));
expression = createExpr;
}
return expression;
}
private CodeExpression[] SerializeParameters (IDesignerSerializationManager manager, ICollection parameters)
{
CodeExpression[] expressions = null;
if (parameters != null && parameters.Count > 0) {
expressions = new CodeExpression[parameters.Count];
int i = 0;
foreach (object parameter in parameters) {
expressions[i] = this.SerializeToExpression (manager, parameter);
i++;
}
}
return expressions;
}
protected void SerializeEvent (IDesignerSerializationManager manager, CodeStatementCollection statements,
object value, EventDescriptor descriptor)
{
if (descriptor == null)
throw new ArgumentNullException ("descriptor");
if (value == null)
throw new ArgumentNullException ("value");
if (statements == null)
throw new ArgumentNullException ("statements");
if (manager == null)
throw new ArgumentNullException ("manager");
MemberCodeDomSerializer serializer = manager.GetSerializer (descriptor.GetType (), typeof (MemberCodeDomSerializer)) as MemberCodeDomSerializer;
if (serializer != null && serializer.ShouldSerialize (manager, value, descriptor))
serializer.Serialize (manager, value, descriptor, statements);
}
protected void SerializeEvents (IDesignerSerializationManager manager, CodeStatementCollection statements,
object value, params Attribute[] filter)
{
if (filter == null)
throw new ArgumentNullException ("filter");
if (value == null)
throw new ArgumentNullException ("value");
if (statements == null)
throw new ArgumentNullException ("statements");
if (manager == null)
throw new ArgumentNullException ("manager");
EventDescriptorCollection events = TypeDescriptor.GetEvents (value, filter);
foreach (EventDescriptor e in events)
this.SerializeEvent (manager, statements, value, e);
}
protected void SerializeProperty (IDesignerSerializationManager manager, CodeStatementCollection statements,
object value, PropertyDescriptor propertyToSerialize)
{
if (propertyToSerialize == null)
throw new ArgumentNullException ("propertyToSerialize");
if (value == null)
throw new ArgumentNullException ("value");
if (statements == null)
throw new ArgumentNullException ("statements");
if (manager == null)
throw new ArgumentNullException ("manager");
MemberCodeDomSerializer serializer = manager.GetSerializer (propertyToSerialize.GetType (),
typeof (MemberCodeDomSerializer)) as MemberCodeDomSerializer;
if (serializer != null && serializer.ShouldSerialize (manager, value, propertyToSerialize))
serializer.Serialize (manager, value, propertyToSerialize, statements);
}
protected void SerializeProperties (IDesignerSerializationManager manager, CodeStatementCollection statements,
object value, Attribute[] filter)
{
if (filter == null)
throw new ArgumentNullException ("filter");
if (value == null)
throw new ArgumentNullException ("value");
if (statements == null)
throw new ArgumentNullException ("statements");
if (manager == null)
throw new ArgumentNullException ("manager");
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (value, filter);
foreach (PropertyDescriptor property in properties) {
if (!property.Attributes.Contains (DesignerSerializationVisibilityAttribute.Hidden))
this.SerializeProperty (manager, statements, value, property);
}
}
protected virtual object DeserializeInstance (IDesignerSerializationManager manager, Type type,
object[] parameters, string name, bool addToContainer)
{
if (type == null)
throw new ArgumentNullException ("type");
if (manager == null)
throw new ArgumentNullException ("manager");
return manager.CreateInstance (type, parameters, name, addToContainer);
}
protected string GetUniqueName (IDesignerSerializationManager manager, object value)
{
if (value == null)
throw new ArgumentNullException ("value");
if (manager == null)
throw new ArgumentNullException ("manager");
string name = manager.GetName (value);
if (name == null) {
INameCreationService service = manager.GetService (typeof (INameCreationService)) as INameCreationService;
name = service.CreateName (null, value.GetType ());
if (name == null)
name = value.GetType ().Name.ToLower ();
manager.SetName (value, name);
}
return name;
}
protected object DeserializeExpression (IDesignerSerializationManager manager, string name, CodeExpression expression)
{
if (expression == null)
throw new ArgumentNullException ("expression");
if (manager == null)
throw new ArgumentNullException ("manager");
bool errorOccurred = false;
object deserialized = null;
// CodeThisReferenceExpression
//
CodeThisReferenceExpression thisExpr = expression as CodeThisReferenceExpression;
if (thisExpr != null) {
RootContext context = manager.Context[typeof (RootContext)] as RootContext;
if (context != null) {
deserialized = context.Value;
} else {
IDesignerHost host = manager.GetService (typeof (IDesignerHost)) as IDesignerHost;
if (host != null)
deserialized = host.RootComponent;
}
}
// CodeVariableReferenceExpression
//
CodeVariableReferenceExpression varRef = expression as CodeVariableReferenceExpression;
if (deserialized == null && varRef != null) {
deserialized = manager.GetInstance (varRef.VariableName);
if (deserialized == null) {
ReportError (manager, "Variable '" + varRef.VariableName + "' not initialized prior to reference");
errorOccurred = true;
}
}
// CodeFieldReferenceExpression (used for Enum references as well)
//
CodeFieldReferenceExpression fieldRef = expression as CodeFieldReferenceExpression;
if (deserialized == null && fieldRef != null) {
deserialized = manager.GetInstance (fieldRef.FieldName);
if (deserialized == null) {
object fieldHolder = DeserializeExpression (manager, null, fieldRef.TargetObject);
FieldInfo field = null;
if (fieldHolder is Type) // static field
field = ((Type)fieldHolder).GetField (fieldRef.FieldName,
BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static);
else // instance field
field = fieldHolder.GetType().GetField (fieldRef.FieldName,
BindingFlags.GetField | BindingFlags.Public | BindingFlags.Instance);
if (field != null)
deserialized = field.GetValue (fieldHolder);
}
if (deserialized == null)
ReportError (manager, "Field '" + fieldRef.FieldName + "' not initialized prior to reference");
}
// CodePrimitiveExpression
//
CodePrimitiveExpression primitiveExp = expression as CodePrimitiveExpression;
if (deserialized == null && primitiveExp != null)
deserialized = primitiveExp.Value;
// CodePropertyReferenceExpression
//
CodePropertyReferenceExpression propRef = expression as CodePropertyReferenceExpression;
if (deserialized == null && propRef != null) {
object target = DeserializeExpression (manager, null, propRef.TargetObject);
if (target != null && target != _errorMarker) {
bool found = false;
if (target is Type) {
PropertyInfo property = ((Type)target).GetProperty (propRef.PropertyName,
BindingFlags.GetProperty |
BindingFlags.Public | BindingFlags.Static);
if (property != null) {
deserialized = property.GetValue (null, null);
found = true;
}
// NRefactory seems to produce PropertyReferences to reference some fields and enums
//
FieldInfo field = ((Type)target).GetField (propRef.PropertyName,
BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static);
if (field != null) {
deserialized = field.GetValue (null);
found = true;
}
} else {
PropertyDescriptor property = TypeDescriptor.GetProperties (target)[propRef.PropertyName];
if (property != null) {
deserialized = property.GetValue (target);
found = true;
}
FieldInfo field = target.GetType().GetField (propRef.PropertyName,
BindingFlags.GetField | BindingFlags.Public | BindingFlags.Instance);
if (field != null) {
deserialized = field.GetValue (null);
found = true;
}
}
if (!found) {
ReportError (manager, "Missing field '" + propRef.PropertyName + " 'in type " +
(target is Type ? ((Type)target).Name : target.GetType ().Name) + "'");
errorOccurred = true;
}
}
}
// CodeObjectCreateExpression
//
CodeObjectCreateExpression createExpr = expression as CodeObjectCreateExpression;
if (deserialized == null && createExpr != null) {
Type type = manager.GetType (createExpr.CreateType.BaseType);
if (type == null) {
ReportError (manager, "Type '" + createExpr.CreateType.BaseType + "' not found." +
"Are you missing a reference?");
errorOccurred = true;
} else {
object[] arguments = new object[createExpr.Parameters.Count];
for (int i=0; i < createExpr.Parameters.Count; i++) {
arguments[i] = this.DeserializeExpression (manager, null, createExpr.Parameters[i]);
if (arguments[i] == _errorMarker) {
errorOccurred = true;
break;
}
}
if (!errorOccurred) {
bool addToContainer = false;
if (typeof(IComponent).IsAssignableFrom (type))
addToContainer = true;
deserialized = this.DeserializeInstance (manager, type, arguments, name, addToContainer);
if (deserialized == _errorMarker || deserialized == null) {
string info = "Type to create: " + createExpr.CreateType.BaseType + System.Environment.NewLine +
"Name: " + name + System.Environment.NewLine +
"addToContainer: " + addToContainer.ToString () + System.Environment.NewLine +
"Parameters Count: " + createExpr.Parameters.Count + System.Environment.NewLine;
for (int i=0; i < arguments.Length; i++) {
info += "Parameter Number: " + i.ToString () + System.Environment.NewLine +
"Parameter Type: " + (arguments[i] == null ? "null" : arguments[i].GetType ().Name) +
System.Environment.NewLine +
"Parameter '" + i.ToString () + "' Value: " + arguments[i].ToString () + System.Environment.NewLine;
}
ReportError (manager,
"Unable to create an instance of type '" + createExpr.CreateType.BaseType + "'",
info);
errorOccurred = true;
}
}
}
}
// CodeArrayCreateExpression
//
CodeArrayCreateExpression arrayCreateExpr = expression as CodeArrayCreateExpression;
if (deserialized == null && arrayCreateExpr != null) {
Type arrayType = manager.GetType (arrayCreateExpr.CreateType.BaseType);
if (arrayType == null) {
ReportError (manager, "Type '" + arrayCreateExpr.CreateType.BaseType + "' not found." +
"Are you missing a reference?");
errorOccurred = true;
} else {
ArrayList initializers = new ArrayList ();
Type elementType = arrayType.GetElementType ();
deserialized = Array.CreateInstance (arrayType, arrayCreateExpr.Initializers.Count);
for (int i = 0; i < arrayCreateExpr.Initializers.Count; i++) {
object element = this.DeserializeExpression (manager, null, arrayCreateExpr.Initializers[i]);
errorOccurred = (element == _errorMarker);
if (!errorOccurred) {
if (arrayType.IsInstanceOfType (element)) {
initializers.Add (element);
} else {
ReportError (manager,
"Array initializer element type incompatible with array type.",
"Array Type: " + arrayType.Name + System.Environment.NewLine +
"Array Element Type: " + elementType + System.Environment.NewLine +
"Initializer Type: " + (element == null ? "null" : element.GetType ().Name));
errorOccurred = true;
}
}
}
if (!errorOccurred)
initializers.CopyTo ((Array)deserialized, 0);
}
}
// CodeMethodInvokeExpression
//
CodeMethodInvokeExpression methodExpr = expression as CodeMethodInvokeExpression;
if (deserialized == null && methodExpr != null) {
object target = this.DeserializeExpression (manager, null, methodExpr.Method.TargetObject);
object[] parameters = null;
if (target == _errorMarker || target == null) {
errorOccurred = true;
} else {
parameters = new object[methodExpr.Parameters.Count];
for (int i=0; i < methodExpr.Parameters.Count; i++) {
parameters[i] = this.DeserializeExpression (manager, null, methodExpr.Parameters[i]);
if (parameters[i] == _errorMarker) {
errorOccurred = true;
break;
}
}
}
if (!errorOccurred) {
MethodInfo method = null;
if (target is Type) {
method = GetExactMethod ((Type)target, methodExpr.Method.MethodName,
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
parameters);
} else {
method = GetExactMethod (target.GetType(), methodExpr.Method.MethodName,
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
parameters);
}
if (method != null) {
deserialized = method.Invoke (target, parameters);
} else {
string info =
"Method Name: " + methodExpr.Method.MethodName + System.Environment.NewLine +
"Method is: " + (target is Type ? "static" : "instance") + System.Environment.NewLine +
"Method Holder Type: " + (target is Type ? ((Type)target).Name : target.GetType ().Name) + System.Environment.NewLine +
"Parameters Count: " + methodExpr.Parameters.Count + System.Environment.NewLine +
System.Environment.NewLine;
for (int i = 0; i < parameters.Length; i++) {
info += "Parameter Number: " + i.ToString () + System.Environment.NewLine +
"Parameter Type: " + (parameters[i] == null ? "null" : parameters[i].GetType ().Name) +
System.Environment.NewLine +
"Parameter " + i.ToString () + " Value: " + parameters[i].ToString () + System.Environment.NewLine;
}
ReportError (manager,
"Method '" + methodExpr.Method.MethodName + "' missing in type '" +
(target is Type ? ((Type)target).Name : target.GetType ().Name + "'"),
info);
errorOccurred = true;
}
}
}
// CodeTypeReferenceExpression
//
CodeTypeReferenceExpression typeRef = expression as CodeTypeReferenceExpression;
if (deserialized == null && typeRef != null) {
deserialized = manager.GetType (typeRef.Type.BaseType);
if (deserialized == null) {
ReportError (manager, "Type '" + typeRef.Type.BaseType + "' not found." +
"Are you missing a reference?");
errorOccurred = true;
}
}
// CodeCastExpression
//
CodeCastExpression castExpr = expression as CodeCastExpression;
if (deserialized == null && castExpr != null) {
Type targetType = manager.GetType (castExpr.TargetType.BaseType);
object instance = DeserializeExpression (manager, null, castExpr.Expression);
if (instance != null && instance != _errorMarker && targetType != null) {
IConvertible convertible = instance as IConvertible;
if (convertible != null) {
try {
instance = convertible.ToType (targetType, null);
} catch {
errorOccurred = true;
}
} else {
errorOccurred = true;
}
if (errorOccurred) {
ReportError (manager, "Unable to convert type '" + instance.GetType ().Name +
"' to type '" + castExpr.TargetType.BaseType + "'",
"Target Type: " + castExpr.TargetType.BaseType + System.Environment.NewLine +
"Instance Type: " + (instance == null ? "null" : instance.GetType ().Name) + System.Environment.NewLine +
"Instance Value: " + (instance == null ? "null" : instance.ToString()) + System.Environment.NewLine +
"Instance is IConvertible: " + (instance is IConvertible).ToString());
}
deserialized = instance;
}
}
// CodeBinaryOperatorExpression
//
CodeBinaryOperatorExpression binOperator = expression as CodeBinaryOperatorExpression;
if (deserialized == null && binOperator != null) {
string errorText = null;
IConvertible left = null;
IConvertible right = null;
switch (binOperator.Operator) {
case CodeBinaryOperatorType.BitwiseOr:
left = DeserializeExpression (manager, null, binOperator.Left) as IConvertible;
right = DeserializeExpression (manager, null, binOperator.Right) as IConvertible;
if (left is Enum && right is Enum) {
deserialized = Enum.ToObject (left.GetType (), Convert.ToInt64 (left) | Convert.ToInt64 (right));
} else {
errorText = "CodeBinaryOperatorType.BitwiseOr allowed only on Enum types";
errorOccurred = true;
}
break;
default:
errorText = "Unsupported CodeBinaryOperatorType: " + binOperator.Operator.ToString ();
errorOccurred = true;
break;
}
if (errorOccurred) {
string info = "BinaryOperator Type: " + binOperator.Operator.ToString() + System.Environment.NewLine +
"Left Type: " + (left == null ? "null" : left.GetType().Name) + System.Environment.NewLine +
"Left Value: " + (left == null ? "null" : left.ToString ()) + System.Environment.NewLine +
"Left Expression Type: " + binOperator.Left.GetType ().Name + System.Environment.NewLine +
"Right Type: " + (right == null ? "null" : right.GetType().Name) + System.Environment.NewLine +
"Right Value: " + (right == null ? "null" : right.ToString ()) + System.Environment.NewLine +
"Right Expression Type: " + binOperator.Right.GetType ().Name;
ReportError (manager, errorText, info);
}
}
if (!errorOccurred) {
if (deserialized == null && !(expression is CodePrimitiveExpression) && !(expression is CodeMethodInvokeExpression)) {
ReportError (manager, "Unsupported Expression Type: " + expression.GetType ().Name);
errorOccurred = true;
}
}
if (errorOccurred)
deserialized = _errorMarker;
return deserialized;
}
// Searches for a method on type that matches argument types
//
private MethodInfo GetExactMethod (Type type, string methodName, BindingFlags flags, ICollection argsCollection)
{
object[] arguments = null;
Type[] types = Type.EmptyTypes;
if (argsCollection != null) {
arguments = new object[argsCollection.Count];
types = new Type[argsCollection.Count];
argsCollection.CopyTo (arguments, 0);
for (int i=0; i < arguments.Length; i++) {
if (arguments[i] == null)
types[i] = null;
else
types[i] = arguments[i].GetType ();
}
}
return type.GetMethod (methodName, flags, null, types, null);
}
protected void DeserializeStatement (IDesignerSerializationManager manager, CodeStatement statement)
{
if (statement == null)
throw new ArgumentNullException ("statement");
if (manager == null)
throw new ArgumentNullException ("manager");
// CodeAssignStatement
//
CodeAssignStatement assignment = statement as CodeAssignStatement;
if (assignment != null)
DeserializeAssignmentStatement (manager, assignment);
// CodeExpressionStatement
//
CodeExpressionStatement expression = statement as CodeExpressionStatement;
if (expression != null)
this.DeserializeExpression (manager, null, expression.Expression);
// CodeAttachEventStatement
//
CodeAttachEventStatement attachStatement = statement as CodeAttachEventStatement;
if (attachStatement != null) {
string methodName = null;
CodeObjectCreateExpression createExpr = attachStatement.Listener as CodeObjectCreateExpression;
if (createExpr != null && createExpr.Parameters.Count == 1 ) { // += new EventType (method)
CodeMethodReferenceExpression handlerRef = createExpr.Parameters[0] as CodeMethodReferenceExpression;
if (handlerRef != null)
methodName = handlerRef.MethodName;
}
CodeDelegateCreateExpression delegateCreateExpr = attachStatement.Listener as CodeDelegateCreateExpression;
if (delegateCreateExpr != null)// += new EventType (method)
methodName = delegateCreateExpr.MethodName;
CodeMethodReferenceExpression methodRef = attachStatement.Listener as CodeMethodReferenceExpression;
if (methodRef != null) // += method
methodName = methodRef.MethodName;
object component = DeserializeExpression (manager, null, attachStatement.Event.TargetObject);
if (component != null && component != _errorMarker && methodName != null) {
string error = null;
EventDescriptor eventDescriptor = TypeDescriptor.GetEvents (component)[attachStatement.Event.EventName];
if (eventDescriptor != null) {
IEventBindingService service = manager.GetService (typeof (IEventBindingService)) as IEventBindingService;
if (service != null)
service.GetEventProperty (eventDescriptor).SetValue (component, methodName);
else
error = "IEventBindingService missing";
} else {
error = "No event '" + attachStatement.Event.EventName +
"' found in type '" + component.GetType ().Name + "'";
}
if (error != null) {
ReportError (manager, error, "Method Name: " + methodName + System.Environment.NewLine +
"Event Name: " + attachStatement.Event.EventName + System.Environment.NewLine +
"Listener Expression Type: " + methodRef.GetType ().Name + System.Environment.NewLine +
"Event Holder Type: " + component.GetType ().Name + System.Environment.NewLine +
"Event Holder Expression Type: " + attachStatement.Event.TargetObject.GetType ().Name);
}
}
}
}
private void DeserializeAssignmentStatement (IDesignerSerializationManager manager, CodeAssignStatement statement)
{
CodeExpression leftExpr = statement.Left;
// Assign to a Property
//
CodePropertyReferenceExpression propRef = leftExpr as CodePropertyReferenceExpression;
if (propRef != null) {
object propertyHolder = DeserializeExpression (manager, null, propRef.TargetObject);
object value = null;
if (propertyHolder != null && propertyHolder != _errorMarker)
value = DeserializeExpression (manager, null, statement.Right);
if (value != null && value != _errorMarker && propertyHolder != null) {
PropertyDescriptor property = TypeDescriptor.GetProperties (propertyHolder)[propRef.PropertyName];
if (property != null) {
property.SetValue (propertyHolder, value);
} else {
ReportError (manager,
"Missing property '" + propRef.PropertyName +
"' in type '" + propertyHolder.GetType ().Name + "'");
}
}
}
// Assign to a Field
//
CodeFieldReferenceExpression fieldRef = leftExpr as CodeFieldReferenceExpression;
if (fieldRef != null && fieldRef.FieldName != null) {
// Note that if the Right expression is a CodeCreationExpression the component will be created in this call
//
object fieldHolder = DeserializeExpression (manager, null, fieldRef.TargetObject);
object value = null;
if (fieldHolder != null && fieldHolder != _errorMarker)
value = DeserializeExpression (manager, fieldRef.FieldName, statement.Right);
FieldInfo field = null;
RootContext context = manager.Context[typeof (RootContext)] as RootContext;
if (fieldHolder != null && fieldHolder != _errorMarker && value != _errorMarker) {
if (fieldRef.TargetObject is CodeThisReferenceExpression && context != null && context.Value == fieldHolder) {
// Do not deserialize the fields of the root component, because the root component type
// is actually an instance of the its parent type, e.g: CustomControl : _UserControl_
// and thus it doesn't contain the fields. The trick is that once DeserializeExpression
// is called on a CodeObjectCreateExpression the component is created and is added to the name-instance
// table.
//
} else {
if (fieldHolder is Type) // static field
field = ((Type)fieldHolder).GetField (fieldRef.FieldName,
BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static);
else // instance field
field = fieldHolder.GetType().GetField (fieldRef.FieldName,
BindingFlags.GetField | BindingFlags.Public | BindingFlags.Instance);
if (field != null)
field.SetValue (fieldHolder, value);
else {
ReportError (manager, "Field '" + fieldRef.FieldName + "' missing in type '" +
fieldHolder.GetType ().Name + "'",
"Field Name: " + fieldRef.FieldName + System.Environment.NewLine +
"Field is: " + (fieldHolder is Type ? "static" : "instance") + System.Environment.NewLine +
"Field Value: " + (value == null ? "null" : value.ToString()) + System.Environment.NewLine +
"Field Holder Type: " + fieldHolder.GetType ().Name + System.Environment.NewLine +
"Field Holder Expression Type: " + fieldRef.TargetObject.GetType ().Name);
}
}
}
}
// Assign to a Variable
//
CodeVariableReferenceExpression varRef = leftExpr as CodeVariableReferenceExpression;
if (varRef != null && varRef.VariableName != null) {
object value = DeserializeExpression (manager, varRef.VariableName, statement.Right);
// If .Right is not CodeObjectCreateExpression the instance won't be assigned a name,
// so do it ourselves
if (value != _errorMarker && manager.GetName (value) == null)
manager.SetName (value, varRef.VariableName);
}
}
internal void ReportError (IDesignerSerializationManager manager, string message)
{
this.ReportError (manager, message, String.Empty);
}
internal void ReportError (IDesignerSerializationManager manager, string message, string details)
{
try {
throw new Exception (message);
} catch (Exception e) {
e.Data["Details"] = message + Environment.NewLine + Environment.NewLine + details;
manager.ReportError (e);
}
}
#region Resource Serialization - TODO
protected CodeExpression SerializeToResourceExpression (IDesignerSerializationManager manager, object value)
{
throw new NotImplementedException ();
}
protected CodeExpression SerializeToResourceExpression (IDesignerSerializationManager manager, object value,
bool ensureInvariant)
{
throw new NotImplementedException ();
}
protected void SerializePropertiesToResources (IDesignerSerializationManager manager, CodeStatementCollection statements,
object value, Attribute[] filter)
{
throw new NotImplementedException ();
}
protected void SerializeResource (IDesignerSerializationManager manager, string resourceName, object value)
{
throw new NotImplementedException ();
}
protected void SerializeResourceInvariant (IDesignerSerializationManager manager, string resourceName, object value)
{
throw new NotImplementedException ();
}
protected void DeserializePropertiesFromResources (IDesignerSerializationManager manager, object value, Attribute[] filter)
{
throw new NotImplementedException ();
}
#endregion
}
}
|