protected string GetUniqueName(IDesignerSerializationManager manager, object instance) { if (instance == null) { throw new ArgumentNullException("instance"); } if (manager == null) { throw new ArgumentNullException("manager"); } string name = manager.GetName(instance); if (name == null) { INameCreationService service = manager.GetService(typeof(INameCreationService)) as INameCreationService; name = service.CreateName(null, instance.GetType()); if (name == null) { name = instance.GetType().Name.ToLower(); } manager.SetName(instance, name); } return(name); }
private SerializationResourceManager CreateResourceManager(IDesignerSerializationManager manager) { SerializationResourceManager resourceManager = this.GetResourceManager(manager); if (!resourceManager.DeclarationAdded) { resourceManager.DeclarationAdded = true; manager.SetName(resourceManager, this.ResourceManagerName); } return resourceManager; }
/// <include file='doc\InstanceDescriptorCodeDomSerializer.uex' path='docs/doc[@for="InstanceDescriptorCodeDomSerializer.Deserialize"]/*' /> /// <devdoc> /// Deserilizes the given CodeDom object into a real object. This /// will use the serialization manager to create objects and resolve /// data types. The root of the object graph is returned. /// </devdoc> public override object Deserialize(IDesignerSerializationManager manager, object codeObject) { if (manager == null || codeObject == null) { throw new ArgumentNullException(manager == null ? "manager" : "codeObject"); } if (!(codeObject is CodeStatementCollection)) { Debug.Fail("ComponentCodeDomSerializer::Deserialize requires a CodeStatementCollection to parse"); throw new ArgumentException(SR.GetString(SR.SerializerBadElementType, typeof(CodeStatementCollection).FullName)); } Debug.WriteLineIf(traceSerialization.TraceVerbose, "InstanceDescriptorCodeDomSerializer::Deserialize"); Debug.Indent(); object instance = null; if (manager.Context[typeof(CodeExpression)] != null) { Debug.WriteLineIf(traceSerialization.TraceVerbose, "Retrieving instance from context stack"); instance = DeserializeExpression(manager, null, (CodeExpression)manager.Context[typeof(CodeExpression)]); Debug.WriteLineIf(traceSerialization.TraceWarning && instance == null, "WARNING: CodeExpression on stack did not return an instance."); } // Now look for things we understand. // foreach (CodeStatement statement in (CodeStatementCollection)codeObject) { if (statement is CodeVariableDeclarationStatement) { CodeVariableDeclarationStatement localRef = (CodeVariableDeclarationStatement)statement; Debug.WriteLineIf(traceSerialization.TraceVerbose, "Creating instance of object: " + localRef.Name); Debug.WriteLineIf(traceSerialization.TraceWarning && instance != null, "WARNING: Instance has already been established."); instance = DeserializeExpression(manager, localRef.Name, localRef.InitExpression); // make sure we pushed in the value of the variable // if (instance != null && null == manager.GetInstance(localRef.Name)) { manager.SetName(instance, localRef.Name); } // Now that the object has been created, deserialize its design time properties. // DeserializePropertiesFromResources(manager, instance, designTimeProperties); } else { DeserializeStatement(manager, statement); } } Debug.Unindent(); return(instance); }
private SerializationResourceManager CreateResourceManager(IDesignerSerializationManager manager) { SerializationResourceManager resourceManager = this.GetResourceManager(manager); if (!resourceManager.DeclarationAdded) { resourceManager.DeclarationAdded = true; manager.SetName(resourceManager, this.ResourceManagerName); } return(resourceManager); }
protected override object DeserializeInstance(IDesignerSerializationManager manager, Type type, object[] parameters, string name, bool addToContainer) { if (typeof(IContainer).IsAssignableFrom(type)) { object service = manager.GetService(typeof(IContainer)); if (service != null) { manager.SetName(service, name); return service; } } return base.DeserializeInstance(manager, type, parameters, name, addToContainer); }
protected override object DeserializeInstance(IDesignerSerializationManager manager, Type type, object[] parameters, string name, bool addToContainer) { if (typeof(IContainer).IsAssignableFrom(type)) { object service = manager.GetService(typeof(IContainer)); if (service != null) { manager.SetName(service, name); return(service); } } return(base.DeserializeInstance(manager, type, parameters, name, addToContainer)); }
/// <summary> /// We override this so we can always provide the correct container as a reference. /// </summary> protected override object DeserializeInstance(IDesignerSerializationManager manager, Type type, object[] parameters, string name, bool addToContainer) { if (typeof(IContainer).IsAssignableFrom(type)) { object obj = manager.GetService(typeof(IContainer)); if (obj != null) { Trace("Returning IContainer service as container"); manager.SetName(obj, name); return(obj); } } Trace("No IContainer service, creating default container."); return(base.DeserializeInstance(manager, type, parameters, name, addToContainer)); }
public override object Deserialize(IDesignerSerializationManager manager, object codeObject) { object instance = null; // look for the creation statement. // foreach (CodeStatement element in (CodeStatementCollection)codeObject) { if (element is CodeVariableDeclarationStatement) { CodeVariableDeclarationStatement statement = (CodeVariableDeclarationStatement)element; // We create the resource manager ouselves here because it's not just a straight // parse of the code. // instance = owner.resourceManager; manager.SetName(instance, statement.Name); codeObject = new CodeStatementCollection((CodeStatementCollection)codeObject); ((CodeStatementCollection)codeObject).Remove(element); } } innerSerializer.Deserialize(manager, codeObject); return(instance); }
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); } }
protected string GetUniqueName (IDesignerSerializationManager manager, object instance) { if (instance == null) throw new ArgumentNullException ("instance"); if (manager == null) throw new ArgumentNullException ("manager"); string name = manager.GetName (instance); if (name == null) { INameCreationService service = manager.GetService (typeof (INameCreationService)) as INameCreationService; name = service.CreateName (null, instance.GetType ()); if (name == null) name = instance.GetType ().Name.ToLower (); manager.SetName (instance, name); } return name; }
/// <include file='doc\InstanceDescriptorCodeDomSerializer.uex' path='docs/doc[@for="InstanceDescriptorCodeDomSerializer.Serialize"]/*' /> /// <devdoc> /// Serializes the given object into a CodeDom object. /// </devdoc> public override object Serialize(IDesignerSerializationManager manager, object value) { object expression = null; Debug.WriteLineIf(traceSerialization.TraceVerbose, "InstanceDescriptorCodeDomSerializer::Serialize"); Debug.Indent(); // To serialize a primitive type, we must assign its value to the current statement. We get the current // statement by asking the context. object statement = manager.Context.Current; Debug.Assert(statement != null, "Statement is null -- we need a context to be pushed for instance descriptors to serialize"); Debug.WriteLineIf(traceSerialization.TraceVerbose, "Value: " + value.ToString()); Debug.WriteLineIf(traceSerialization.TraceVerbose && statement != null, "Statement: " + statement.GetType().Name); TypeConverter converter = TypeDescriptor.GetConverter(value); InstanceDescriptor descriptor = (InstanceDescriptor)converter.ConvertTo(value, typeof(InstanceDescriptor)); if (descriptor != null) { expression = SerializeInstanceDescriptor(manager, value, descriptor); } else { Debug.WriteLineIf(traceSerialization.TraceError, "*** Converter + " + converter.GetType().Name + " failed to give us an instance descriptor"); } // Ok, we have the "new Foo(arg, arg, arg)" done. Next, check to see if the instance // descriptor has given us a complete representation of the object. If not, we must // go through the additional work of creating a local variable and saving properties. // if (descriptor != null && !descriptor.IsComplete) { Debug.WriteLineIf(traceSerialization.TraceVerbose, "Incomplete instance descriptor; creating local variable declaration and serializing properties."); CodeStatementCollection statements = (CodeStatementCollection)manager.Context[typeof(CodeStatementCollection)]; Debug.WriteLineIf(traceSerialization.TraceError && statements == null, "*** No CodeStatementCollection on context stack so we can generate a local variable statement."); if (statements != null) { MemberInfo mi = descriptor.MemberInfo; Type targetType; if (mi is PropertyInfo) { targetType = ((PropertyInfo)mi).PropertyType; } else if (mi is MethodInfo) { targetType = ((MethodInfo)mi).ReturnType; } else { targetType = mi.DeclaringType; } string localName = manager.GetName(value); if (localName == null) { string baseName; INameCreationService ns = (INameCreationService)manager.GetService(typeof(INameCreationService)); Debug.WriteLineIf(traceSerialization.TraceWarning && (ns == null), "WARNING: Need to generate name for local variable but we have no service."); if (ns != null) { baseName = ns.CreateName(null, targetType); } else { baseName = targetType.Name.ToLower(CultureInfo.InvariantCulture); } int suffixIndex = 1; // Declare this name to the serializer. If there is already a name defined, // keep trying. // while (true) { localName = baseName + suffixIndex.ToString(); if (manager.GetInstance(localName) == null) { manager.SetName(value, localName); break; } suffixIndex++; } } Debug.WriteLineIf(traceSerialization.TraceVerbose, "Named local variable " + localName); CodeVariableDeclarationStatement localStatement = new CodeVariableDeclarationStatement(targetType, localName); localStatement.InitExpression = (CodeExpression)expression; statements.Add(localStatement); expression = new CodeVariableReferenceExpression(localName); // Create a CodeValueExpression to place on the context stack. CodeValueExpression cve = new CodeValueExpression((CodeExpression)expression, value); manager.Context.Push(cve); try { // Now that we have hooked the return expression up and declared the local, // it's time to save off the properties for the object. // SerializeProperties(manager, statements, value, runTimeProperties); } finally { Debug.Assert(manager.Context.Current == cve, "Context stack corrupted"); manager.Context.Pop(); } } } Debug.Unindent(); return(expression); }
private bool ResolveName(IDesignerSerializationManager manager, string name, bool canInvokeManager) { bool flag = false; CodeDomSerializerBase.OrderedCodeStatementCollection codeObject = this._statementsTable[name] as CodeDomSerializerBase.OrderedCodeStatementCollection; object[] objArray = (object[]) this._objectState[name]; if (name.IndexOf('.') > 0) { string outerComponent = null; IComponent instance = this.ResolveNestedName(manager, name, ref outerComponent); if ((instance != null) && (outerComponent != null)) { manager.SetName(instance, name); this.ResolveName(manager, outerComponent, canInvokeManager); } } if (codeObject == null) { flag = this._statementsTable[name] != null; if (!flag) { if (this._expressions.ContainsKey(name)) { ArrayList list2 = this._expressions[name]; foreach (CodeExpression expression2 in list2) { object obj3 = base.DeserializeExpression(manager, name, expression2); if (((obj3 != null) && !flag) && (canInvokeManager && (manager.GetInstance(name) == null))) { manager.SetName(obj3, name); flag = true; } } } if (!flag && canInvokeManager) { flag = manager.GetInstance(name) != null; } if ((flag && (objArray != null)) && (objArray[2] != null)) { this.DeserializeDefaultProperties(manager, name, objArray[2]); } if ((flag && (objArray != null)) && (objArray[3] != null)) { this.DeserializeDesignTimeProperties(manager, name, objArray[3]); } if ((flag && (objArray != null)) && (objArray[4] != null)) { this.DeserializeEventResets(manager, name, objArray[4]); } if ((flag && (objArray != null)) && (objArray[5] != null)) { DeserializeModifier(manager, name, objArray[5]); } } if (!flag && (flag || canInvokeManager)) { manager.ReportError(new CodeDomSerializerException(System.Design.SR.GetString("CodeDomComponentSerializationServiceDeserializationError", new object[] { name }), manager)); } return flag; } this._objectState[name] = null; this._statementsTable[name] = null; string typeName = null; foreach (CodeStatement statement in codeObject) { CodeVariableDeclarationStatement statement2 = statement as CodeVariableDeclarationStatement; if (statement2 != null) { typeName = statement2.Type.BaseType; break; } } if (typeName != null) { Type valueType = manager.GetType(typeName); if (valueType == null) { manager.ReportError(new CodeDomSerializerException(System.Design.SR.GetString("SerializerTypeNotFound", new object[] { typeName }), manager)); goto Label_01DA; } if ((codeObject == null) || (codeObject.Count <= 0)) { goto Label_01DA; } CodeDomSerializer serializer = base.GetSerializer(manager, valueType); if (serializer == null) { manager.ReportError(new CodeDomSerializerException(System.Design.SR.GetString("SerializerNoSerializerForComponent", new object[] { valueType.FullName }), manager)); goto Label_01DA; } try { object obj2 = serializer.Deserialize(manager, codeObject); flag = obj2 != null; if (flag) { this._statementsTable[name] = obj2; } goto Label_01DA; } catch (Exception exception) { manager.ReportError(exception); goto Label_01DA; } } foreach (CodeStatement statement3 in codeObject) { base.DeserializeStatement(manager, statement3); } flag = true; Label_01DA: if ((objArray != null) && (objArray[2] != null)) { this.DeserializeDefaultProperties(manager, name, objArray[2]); } if ((objArray != null) && (objArray[3] != null)) { this.DeserializeDesignTimeProperties(manager, name, objArray[3]); } if ((objArray != null) && (objArray[4] != null)) { this.DeserializeEventResets(manager, name, objArray[4]); } if ((objArray != null) && (objArray[5] != null)) { DeserializeModifier(manager, name, objArray[5]); } if (!this._expressions.ContainsKey(name)) { return flag; } ArrayList list = this._expressions[name]; foreach (CodeExpression expression in list) { base.DeserializeExpression(manager, name, expression); } this._expressions.Remove(name); return true; }
protected object DeserializeExpression(IDesignerSerializationManager manager, string name, CodeExpression expression) { object instance = expression; using (TraceScope("CodeDomSerializerBase::DeserializeExpression")) { while (instance != null) { CodePrimitiveExpression expression2 = instance as CodePrimitiveExpression; if (expression2 != null) { return expression2.Value; } CodePropertyReferenceExpression propertyReferenceEx = instance as CodePropertyReferenceExpression; if (propertyReferenceEx != null) { return this.DeserializePropertyReferenceExpression(manager, propertyReferenceEx, true); } if (instance is CodeThisReferenceExpression) { RootContext context = (RootContext) manager.Context[typeof(RootContext)]; if (context != null) { instance = context.Value; } else { IDesignerHost host = manager.GetService(typeof(IDesignerHost)) as IDesignerHost; if (host != null) { instance = host.RootComponent; } } if (instance == null) { Error(manager, System.Design.SR.GetString("SerializerNoRootExpression"), "SerializerNoRootExpression"); } return instance; } CodeTypeReferenceExpression expression4 = instance as CodeTypeReferenceExpression; if (expression4 != null) { return manager.GetType(GetTypeNameFromCodeTypeReference(manager, expression4.Type)); } CodeObjectCreateExpression expression5 = instance as CodeObjectCreateExpression; if (expression5 != null) { instance = null; Type c = manager.GetType(GetTypeNameFromCodeTypeReference(manager, expression5.CreateType)); if (c != null) { object[] parameters = new object[expression5.Parameters.Count]; bool flag = true; for (int i = 0; i < parameters.Length; i++) { parameters[i] = this.DeserializeExpression(manager, null, expression5.Parameters[i]); if (parameters[i] is CodeExpression) { if ((typeof(Delegate).IsAssignableFrom(c) && (parameters.Length == 1)) && (parameters[i] is CodeMethodReferenceExpression)) { CodeMethodReferenceExpression expression19 = (CodeMethodReferenceExpression) parameters[i]; if (!(expression19.TargetObject is CodeThisReferenceExpression)) { object obj3 = this.DeserializeExpression(manager, null, expression19.TargetObject); if (!(obj3 is CodeExpression)) { MethodInfo method = c.GetMethod("Invoke"); if (method != null) { ParameterInfo[] infoArray = method.GetParameters(); Type[] types = new Type[infoArray.Length]; for (int j = 0; j < types.Length; j++) { types[j] = infoArray[i].ParameterType; } if (GetReflectionTypeHelper(manager, obj3).GetMethod(expression19.MethodName, types) != null) { MethodInfo info2 = obj3.GetType().GetMethod(expression19.MethodName, types); instance = Activator.CreateInstance(c, new object[] { obj3, info2.MethodHandle.GetFunctionPointer() }); } } } } } flag = false; break; } } if (flag) { instance = this.DeserializeInstance(manager, c, parameters, name, name != null); } return instance; } Error(manager, System.Design.SR.GetString("SerializerTypeNotFound", new object[] { expression5.CreateType.BaseType }), "SerializerTypeNotFound"); return instance; } CodeArgumentReferenceExpression expression6 = instance as CodeArgumentReferenceExpression; if (expression6 != null) { instance = manager.GetInstance(expression6.ParameterName); if (instance == null) { Error(manager, System.Design.SR.GetString("SerializerUndeclaredName", new object[] { expression6.ParameterName }), "SerializerUndeclaredName"); } return instance; } CodeFieldReferenceExpression expression7 = instance as CodeFieldReferenceExpression; if (expression7 != null) { object obj4 = this.DeserializeExpression(manager, null, expression7.TargetObject); if ((obj4 != null) && !(obj4 is CodeExpression)) { FieldInfo field; object obj6; RootContext context2 = (RootContext) manager.Context[typeof(RootContext)]; if ((context2 != null) && (context2.Value == obj4)) { object obj5 = manager.GetInstance(expression7.FieldName); if (obj5 != null) { return obj5; } Error(manager, System.Design.SR.GetString("SerializerUndeclaredName", new object[] { expression7.FieldName }), "SerializerUndeclaredName"); return instance; } Type type = obj4 as Type; if (type != null) { obj6 = null; field = GetReflectionTypeFromTypeHelper(manager, type).GetField(expression7.FieldName, BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static); } else { obj6 = obj4; field = GetReflectionTypeHelper(manager, obj4).GetField(expression7.FieldName, BindingFlags.GetField | BindingFlags.Public | BindingFlags.Instance); } if (field != null) { return field.GetValue(obj6); } CodePropertyReferenceExpression expression20 = new CodePropertyReferenceExpression { TargetObject = expression7.TargetObject, PropertyName = expression7.FieldName }; instance = this.DeserializePropertyReferenceExpression(manager, expression20, false); if (instance == expression7) { Error(manager, System.Design.SR.GetString("SerializerUndeclaredName", new object[] { expression7.FieldName }), "SerializerUndeclaredName"); } return instance; } Error(manager, System.Design.SR.GetString("SerializerFieldTargetEvalFailed", new object[] { expression7.FieldName }), "SerializerFieldTargetEvalFailed"); return instance; } CodeMethodInvokeExpression expression8 = instance as CodeMethodInvokeExpression; if (expression8 != null) { object component = this.DeserializeExpression(manager, null, expression8.Method.TargetObject); if (component != null) { object[] args = new object[expression8.Parameters.Count]; bool flag2 = true; for (int k = 0; k < args.Length; k++) { args[k] = this.DeserializeExpression(manager, null, expression8.Parameters[k]); if (args[k] is CodeExpression) { flag2 = false; break; } } if (flag2) { IComponentChangeService service = (IComponentChangeService) manager.GetService(typeof(IComponentChangeService)); Type type3 = component as Type; if (type3 != null) { return GetReflectionTypeFromTypeHelper(manager, type3).InvokeMember(expression8.Method.MethodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, args, null, null, null); } if (service != null) { service.OnComponentChanging(component, null); } try { instance = GetReflectionTypeHelper(manager, component).InvokeMember(expression8.Method.MethodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, component, args, null, null, null); } catch (MissingMethodException) { CodeCastExpression targetObject = expression8.Method.TargetObject as CodeCastExpression; if (targetObject == null) { throw; } Type type4 = manager.GetType(GetTypeNameFromCodeTypeReference(manager, targetObject.TargetType)); if ((type4 == null) || !type4.IsInterface) { throw; } instance = GetReflectionTypeFromTypeHelper(manager, type4).InvokeMember(expression8.Method.MethodName, BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, component, args, null, null, null); } if (service != null) { service.OnComponentChanged(component, null, null, null); } return instance; } if ((args.Length == 1) && (args[0] is CodeDelegateCreateExpression)) { string methodName = expression8.Method.MethodName; if (methodName.StartsWith("add_")) { methodName = methodName.Substring(4); this.DeserializeAttachEventStatement(manager, new CodeAttachEventStatement(expression8.Method.TargetObject, methodName, (CodeExpression) args[0])); instance = null; } } } return instance; } CodeVariableReferenceExpression expression9 = instance as CodeVariableReferenceExpression; if (expression9 != null) { instance = manager.GetInstance(expression9.VariableName); if (instance == null) { Error(manager, System.Design.SR.GetString("SerializerUndeclaredName", new object[] { expression9.VariableName }), "SerializerUndeclaredName"); } return instance; } CodeCastExpression expression10 = instance as CodeCastExpression; if (expression10 != null) { instance = this.DeserializeExpression(manager, name, expression10.Expression); IConvertible convertible = instance as IConvertible; if (convertible != null) { Type conversionType = manager.GetType(GetTypeNameFromCodeTypeReference(manager, expression10.TargetType)); if (conversionType != null) { instance = convertible.ToType(conversionType, null); } } return instance; } if (instance is CodeBaseReferenceExpression) { RootContext context3 = (RootContext) manager.Context[typeof(RootContext)]; if (context3 != null) { return context3.Value; } return null; } CodeArrayCreateExpression expression11 = instance as CodeArrayCreateExpression; if (expression11 != null) { Type type6 = manager.GetType(GetTypeNameFromCodeTypeReference(manager, expression11.CreateType)); Array array = null; if (type6 != null) { if (expression11.Initializers.Count > 0) { ArrayList list = new ArrayList(expression11.Initializers.Count); foreach (CodeExpression expression22 in expression11.Initializers) { try { object o = this.DeserializeExpression(manager, null, expression22); if (!(o is CodeExpression)) { if (!type6.IsInstanceOfType(o)) { o = Convert.ChangeType(o, type6, CultureInfo.InvariantCulture); } list.Add(o); } } catch (Exception exception) { manager.ReportError(exception); } } array = Array.CreateInstance(type6, list.Count); list.CopyTo(array, 0); } else if (expression11.SizeExpression != null) { IConvertible convertible2 = this.DeserializeExpression(manager, name, expression11.SizeExpression) as IConvertible; if (convertible2 != null) { int length = convertible2.ToInt32(null); array = Array.CreateInstance(type6, length); } } else { array = Array.CreateInstance(type6, expression11.Size); } } else { Error(manager, System.Design.SR.GetString("SerializerTypeNotFound", new object[] { expression11.CreateType.BaseType }), "SerializerTypeNotFound"); } instance = array; if ((instance != null) && (name != null)) { manager.SetName(instance, name); } return instance; } CodeArrayIndexerExpression expression12 = instance as CodeArrayIndexerExpression; if (expression12 != null) { instance = null; Array array2 = this.DeserializeExpression(manager, name, expression12.TargetObject) as Array; if (array2 != null) { int[] indices = new int[expression12.Indices.Count]; bool flag3 = true; for (int m = 0; m < indices.Length; m++) { IConvertible convertible3 = this.DeserializeExpression(manager, name, expression12.Indices[m]) as IConvertible; if (convertible3 != null) { indices[m] = convertible3.ToInt32(null); } else { flag3 = false; break; } } if (flag3) { instance = array2.GetValue(indices); } } return instance; } CodeBinaryOperatorExpression expression13 = instance as CodeBinaryOperatorExpression; if (expression13 != null) { object obj10 = this.DeserializeExpression(manager, null, expression13.Left); object obj11 = this.DeserializeExpression(manager, null, expression13.Right); instance = obj10; IConvertible left = obj10 as IConvertible; IConvertible right = obj11 as IConvertible; if ((left != null) && (right != null)) { instance = this.ExecuteBinaryExpression(left, right, expression13.Operator); } return instance; } CodeDelegateInvokeExpression expression14 = instance as CodeDelegateInvokeExpression; if (expression14 != null) { Delegate delegate2 = this.DeserializeExpression(manager, null, expression14.TargetObject) as Delegate; if (delegate2 != null) { object[] objArray3 = new object[expression14.Parameters.Count]; bool flag4 = true; for (int n = 0; n < objArray3.Length; n++) { objArray3[n] = this.DeserializeExpression(manager, null, expression14.Parameters[n]); if (objArray3[n] is CodeExpression) { flag4 = false; break; } } if (flag4) { delegate2.DynamicInvoke(objArray3); } } return instance; } CodeDirectionExpression expression15 = instance as CodeDirectionExpression; if (expression15 != null) { return this.DeserializeExpression(manager, name, expression15.Expression); } CodeIndexerExpression expression16 = instance as CodeIndexerExpression; if (expression16 != null) { instance = null; object target = this.DeserializeExpression(manager, null, expression16.TargetObject); if (target != null) { object[] objArray4 = new object[expression16.Indices.Count]; bool flag5 = true; for (int num7 = 0; num7 < objArray4.Length; num7++) { objArray4[num7] = this.DeserializeExpression(manager, null, expression16.Indices[num7]); if (objArray4[num7] is CodeExpression) { flag5 = false; break; } } if (flag5) { instance = GetReflectionTypeHelper(manager, target).InvokeMember("Item", BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance, null, target, objArray4, null, null, null); } } return instance; } if (instance is CodeSnippetExpression) { return null; } CodeParameterDeclarationExpression expression17 = instance as CodeParameterDeclarationExpression; if (expression17 != null) { return manager.GetType(GetTypeNameFromCodeTypeReference(manager, expression17.Type)); } CodeTypeOfExpression expression18 = instance as CodeTypeOfExpression; if (expression18 != null) { string typeNameFromCodeTypeReference = GetTypeNameFromCodeTypeReference(manager, expression18.Type); for (int num8 = 0; num8 < expression18.Type.ArrayRank; num8++) { typeNameFromCodeTypeReference = typeNameFromCodeTypeReference + "[]"; } instance = manager.GetType(typeNameFromCodeTypeReference); if (instance == null) { Error(manager, System.Design.SR.GetString("SerializerTypeNotFound", new object[] { typeNameFromCodeTypeReference }), "SerializerTypeNotFound"); } return instance; } if (((instance is CodeEventReferenceExpression) || (instance is CodeMethodReferenceExpression)) || !(instance is CodeDelegateCreateExpression)) { } return instance; } } return instance; }
private void DeserializeAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement) { using (TraceScope("CodeDomSerializerBase::DeserializeAssignStatement")) { CodeExpression left = statement.Left; CodePropertyReferenceExpression propertyReferenceEx = left as CodePropertyReferenceExpression; if (propertyReferenceEx != null) { this.DeserializePropertyAssignStatement(manager, statement, propertyReferenceEx, true); } else { CodeFieldReferenceExpression expression3 = left as CodeFieldReferenceExpression; if (expression3 != null) { object instance = this.DeserializeExpression(manager, expression3.FieldName, expression3.TargetObject); if (instance != null) { RootContext context = (RootContext) manager.Context[typeof(RootContext)]; if ((context != null) && (context.Value == instance)) { if (this.DeserializeExpression(manager, expression3.FieldName, statement.Right) is CodeExpression) { } } else { FieldInfo field; object obj4; Type type = instance as Type; if (type != null) { obj4 = null; field = GetReflectionTypeFromTypeHelper(manager, type).GetField(expression3.FieldName, BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static); } else { obj4 = instance; field = GetReflectionTypeHelper(manager, instance).GetField(expression3.FieldName, BindingFlags.GetField | BindingFlags.Public | BindingFlags.Instance); } if (field != null) { object obj5 = this.DeserializeExpression(manager, expression3.FieldName, statement.Right); if (!(obj5 is CodeExpression)) { IConvertible convertible = obj5 as IConvertible; if (convertible != null) { Type fieldType = field.FieldType; TypeDescriptionProvider targetFrameworkProviderForType = GetTargetFrameworkProviderForType(manager, fieldType); if (targetFrameworkProviderForType != null) { fieldType = targetFrameworkProviderForType.GetRuntimeType(fieldType); } if (fieldType != obj5.GetType()) { try { obj5 = convertible.ToType(fieldType, null); } catch { } } } field.SetValue(obj4, obj5); } } else { CodePropertyReferenceExpression expression6 = new CodePropertyReferenceExpression { TargetObject = expression3.TargetObject, PropertyName = expression3.FieldName }; if (!this.DeserializePropertyAssignStatement(manager, statement, expression6, false)) { Error(manager, System.Design.SR.GetString("SerializerNoSuchField", new object[] { instance.GetType().FullName, expression3.FieldName }), "SerializerNoSuchField"); } } } } } else { CodeVariableReferenceExpression expression4 = left as CodeVariableReferenceExpression; if (expression4 != null) { object obj6 = this.DeserializeExpression(manager, expression4.VariableName, statement.Right); if (!(obj6 is CodeExpression)) { manager.SetName(obj6, expression4.VariableName); } } else { CodeArrayIndexerExpression expression5 = left as CodeArrayIndexerExpression; if (expression5 != null) { int[] indices = new int[expression5.Indices.Count]; object obj7 = this.DeserializeExpression(manager, null, expression5.TargetObject); bool flag = true; for (int i = 0; i < indices.Length; i++) { IConvertible convertible2 = this.DeserializeExpression(manager, null, expression5.Indices[i]) as IConvertible; if (convertible2 != null) { indices[i] = convertible2.ToInt32(null); } else { flag = false; break; } } Array array = obj7 as Array; if ((array != null) && flag) { object obj9 = this.DeserializeExpression(manager, null, statement.Right); if (!(obj9 is CodeExpression)) { array.SetValue(obj9, indices); } } } } } } } }
protected string GetUniqueName(IDesignerSerializationManager manager, object value) { string str2; if (manager == null) { throw new ArgumentNullException("manager"); } if (value == null) { throw new ArgumentNullException("value"); } string name = manager.GetName(value); if (name != null) { return name; } Type reflectionTypeHelper = GetReflectionTypeHelper(manager, value); INameCreationService service = manager.GetService(typeof(INameCreationService)) as INameCreationService; if (service != null) { str2 = service.CreateName(null, reflectionTypeHelper); } else { str2 = reflectionTypeHelper.Name.ToLower(CultureInfo.InvariantCulture); } int num = 1; ComponentCache cache = manager.Context[typeof(ComponentCache)] as ComponentCache; while (true) { name = string.Format(CultureInfo.CurrentCulture, "{0}{1}", new object[] { str2, num }); if ((manager.GetInstance(name) == null) && ((cache == null) || !cache.ContainsLocalName(name))) { manager.SetName(value, name); ComponentCache.Entry entry = manager.Context[typeof(ComponentCache.Entry)] as ComponentCache.Entry; if (entry != null) { entry.AddLocalName(name); } return name; } num++; } }