public static object?TransformClass(object window, Type newType) { var oldTypeHandler = (XMLComplexBaseTypeHandler?)XMLHelpers.GetTypeHandler(window.GetType()); var newTypeHandler = (XMLComplexBaseTypeHandler?)XMLHelpers.GetTypeHandler(newType); if (oldTypeHandler == null || newTypeHandler == null) { return(null); } object?newTypeObject = Activator.CreateInstance(newType, true); Debug.Assert(newTypeObject != null); IEnumerator <XMLFieldHandler>?oldFields = oldTypeHandler.EnumFields(); while (oldFields.MoveNext()) { XMLFieldHandler oldField = oldFields.Current; XMLFieldHandler?newFieldsMatch = null; IEnumerator <XMLFieldHandler>?newFields = newTypeHandler.EnumFields(); while (newFields.MoveNext()) { XMLFieldHandler newField = newFields.Current; if (newField.Name != oldField.Name || newField.TypeHandler != oldField.TypeHandler) { continue; } newFieldsMatch = newField; break; } if (newFieldsMatch == null) { continue; } object value = oldField.ReflectionInfo.GetValue(window); newFieldsMatch.ReflectionInfo.SetValue(newTypeObject, value); } return(newTypeObject); }
public XMLComplexTypeHandler(Type type) : base(type) { // Check if inheriting anything. If so copy its excluded members as well. if (Type.BaseType != null && Type.BaseType != typeof(object)) { var baseClass = (XMLComplexTypeHandler)XMLHelpers.GetTypeHandler(Type.BaseType); HashSet <string> baseTypeExcludedMembers = baseClass?._excludedMembers; if (baseTypeExcludedMembers != null) { if (_excludedMembers == null) { _excludedMembers = baseTypeExcludedMembers; } else { // Copy hashset as not to modify reference of attribute. var newHashSet = new HashSet <string>(); foreach (string excludedMember in _excludedMembers) { newHashSet.Add(excludedMember); } // Add values from base type. foreach (string excludedMember in baseTypeExcludedMembers) { newHashSet.Add(excludedMember); } _excludedMembers = newHashSet; } } } // Create default value reference. _defaultConstruct = type.IsInterface || type.IsAbstract ? null : Activator.CreateInstance(type, true); }
public XMLArrayTypeHandler(Type type, Type elementType) : base(type) { _elementTypeHandler = XMLHelpers.GetTypeHandler(elementType); _elementType = elementType; // non-opaque }